kaon的个人主页

http://www.qtcn.org/bbs/u/107506  [收藏] [复制]

kaon

  • 6

    关注

  • 20

    粉丝

  • 28

    访客

  • 等级:侠客
  • 总积分:267
  • 保密,2010-11-08

最后登录:2024-04-25

更多资料

日志

QML  冒号 等号 赋值 binding assignment 区别

2014-05-30 10:41
Text {
        id: label
        x: 24; y: 24
        // custom counter property for space presses
        property int spacePresses: 0
        text: "Space pressed: " + spacePresses + " times"
        // (1) handler for text changes
        onTextChanged: console.log("text changed to:", text)
        // need focus to receive key events
        focus: true
        // (2) handler with some JS
        Keys.onSpacePressed: {
            increment()
        }
        // clear the text on escape
        Keys.onEscapePressed: {
            label.text = ''
        }
        // (3) a JS function
        function increment() {
            spacePresses = spacePresses + 1
        }
    }

The difference between the QML : (binding) and the JavaScript = (assignment) is, that the binding is a contract and keeps true over the lifetime of the binding, whereas the JavaScript assignment (=) is a one time value assignment. The lifetime of a binding ends, when a new binding is set to the property or even when a JavaScript value is assigned is to the property. For example a key handler setting the text property to an empty string would destroy our increment display:
Keys.onEscapePressed: {
    label.text = ''
}
After pressing escape, pressing the space-bar will not update the display anymore as the previous binding of the text property (text: “Space pressed: ” + spacePresses + ” times”) was destroyed.
When you have conflicting strategies to change a property as in this case (text updated by a change to a property increment via a binding and text cleared by a JavaScript assignment) then you can’t use binding! You need to use assignment on both property change paths as the binding will be destroyed by the assignment (broken contract!).
分类:默认分类|回复:1|浏览:1466|全站可见|转载
 

下一篇:

上一篇: QFlags and enum 用法

 
删除

彩阳:绑定只在载入的时候有效,一旦使用了赋值,那么绑定失效。

2014-05-30 17:40 -

Powered by phpwind v8.7 Certificate Copyright Time now is:04-26 18:55
©2005-2016 QTCN开发网 版权所有 Gzip disabled