On QML attribute binding and Assignment property assignment
Assign a value to a property in the literal sense
Rectangle { id:rect Component.onCompeleted:{ 10// 赋值 10// 赋值 rect.color"red"// 赋值 }}
When a property is assigned a value, a signal handler can be used to add a processing function to the signal, and for the signal processor, which is the queue that connects to the slot function of the same signal, each time the processor is written for the signal handler, it does not overwrite the last written handler. The latest processing function is added to the end of the processing queue of the signal processor, when the signal handler is triggered, according to the queue, advanced first execution. (similar to decorated mode).
Binding a property binding to an expression
Property binding allows an attribute to be bound to an expression, such as a number, a string, a function, an expression of an operation, or a block of code that has a return value.
Rectangle { id:rect width:10// 绑定到一个数字 height:1010//绑定到表达式 radius:{ if10return2; elsereturn3; // 绑定到一个代码块 color:Qt.rgba(10.90.81// 绑定到一个函数}
Binding properties via Binding
Rectangle{ id: root width: 100 height: 100 Text{ id: showText } Binding{ target:id:showText // 被绑定的对象,可以是通过c++注册的单例对象 property:"text" // 通过字符串来找到属性 value: root.width // 绑定到root.width // Binding 相当于 // bool QObject::?setProperty(const char * name, const QVariant & value); }}
Bind by Propertychanges
Import QtQuick2. 0Item { ID: container width: height: Rectangle {id:rect width: +; height: color: "Red" Mousearea {Id:mousearea Anchors.fill:parent }states: State { name: "resized"; When : mousearea.pressed propertychanges {target:rect; color: "Blue"; height: container.height } } }}
Propertychanges generally with state to use
Use Qt.binding (function) to specify a function to bind to a property
Rectangle { id: rect 100 100 Component.onCompeleted: { 200// 仅仅只是赋值操作 function(){ return width*1.5; // 直接绑定到一个函数 }}
The bound expression, which contains the properties of the object, is signaled whenever the property changes, and the property of the binding expression updates itself according to the expression.
QML Core functionality-Attribute binding
Property bindings and assignments in QML