日志
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!). |
下一篇: 无
上一篇: QFlags and enum 用法