• 13128阅读
  • 4回复

[提问]QML Rectangle: Binding loop detected for property "y" 该如何解决? [复制链接]

上一主题 下一主题
离线oceanljp
 

只看楼主 倒序阅读 楼主  发表于: 2011-07-28


直接上代码:
  1. import QtQuick 1.0
  2. Item {
  3.     Rectangle {
  4.         id: buttonRect
  5.         width: 96
  6.         height: 96
  7.         color:"black"
  8.         Image {
  9.             id: img
  10.             source: "./tux.png"
  11.             anchors.centerIn:parent
  12.         }
  13.         MouseArea {
  14.             id:mouseArea
  15.             anchors.fill: parent
  16.         }
  17.         states: State {
  18.             name: "down"; when: mouseArea.pressed == true
  19.             PropertyChanges { target:buttonRect ; y: y + 3 ; x : x + 3; }
  20.         }
  21.     }
  22. }
上面代码目标实现一个按钮的功能。当点击按钮时,按钮会向右向下移动3像素,以模拟被按下的情况。目前程序运行正常,但是在按下按钮时,会打印如下警告:
QML Rectangle: Binding loop detected for property "y"
QML Rectangle: Binding loop detected for property "x"
求能消除这些警告的方法。
不考虑重定向错误输出,禁止waring的解决方法。


离线wxj120bw

只看该作者 1楼 发表于: 2011-07-29
回 楼主(oceanljp) 的帖子
楼主看下 http://permalink.gmane.org/gmane.comp.lib.qt.qml/1622 就能解决你的问题
离线oceanljp

只看该作者 2楼 发表于: 2011-07-30
楼上的连接给出了线索,不过具体解决办法要在官方文档中找。
相关连接如下:
http://doc.qt.nokia.com/4.7/qml-propertychanges.html


具体到代码中,就是将如下代码
  1. PropertyChanges { target:buttonRect ; y: y + 3 ; x : x + 3; }
改为:
  1. PropertyChanges { target:buttonRect ; explicit : true ; y: y + 3 ; x : x + 3; }
多谢楼上指点啊。
离线wxj120bw

只看该作者 3楼 发表于: 2011-07-31
回 2楼(oceanljp) 的帖子
先前我也解决了警告问题,不过代码比你上面多了许多。我先得到鼠标点击之前buttonRect的坐标,然后在点击时赋给。这样就没有绑定的警告了。
  1. Item {
  2.     Rectangle {
  3.         id: buttonRect
  4.         width: 96
  5.         height: 96
  6.         color:"black"
  7.         property int curX: 0
  8.         property int curY: 0
  9.         Image {
  10.             id: img
  11.             source: "states.png"
  12.             anchors.centerIn:parent
  13.         }
  14.         MouseArea {
  15.             id:mouseArea
  16.             anchors.fill:parent
  17.             hoverEnabled: true
  18.             onEntered: {
  19.                 curX:buttonRect.x;
  20.                 curY:buttonRect.y;
  21.             }
  22.         }
  23.         states: [
  24.             State {
  25.                 name:"down"
  26.                 when:mouseArea.pressed==true
  27.                 PropertyChanges
  28.                 {
  29.                     target:buttonRect;
  30.                     y:curY+1;
  31.                     x:curX+1;
  32.                 }
  33.             }
  34.         ]
  35.     }
  36. }
离线oceanljp

只看该作者 4楼 发表于: 2011-08-02
呵呵,条条大路通罗马。
快速回复
限100 字节
 
上一个 下一个