Binding QML Type
Binding QML类型
Enables the arbitrary creation of property bindings. More...
允许任意创建属性绑定。
Import Statement: | import QtQml |
Properties
Detailed Description
详细描述
In QML, property bindings result in a dependency between the properties of different objects.
在QML中,属性绑定导致不同对象的属性之间存在依赖关系。
Binding to an Inaccessible Property
绑定到无法访问的属性
Sometimes it is necessary to bind an object's property to that of another object that isn't directly instantiated by QML, such as a property of a class exported to QML by C++. You can use the Binding type to establish this dependency; binding any value to any object's property.
有时,有必要将一个对象的属性绑定到另一个不是由QML直接实例化的对象的属性,例如C++导出到QML的类的属性。您可以使用绑定类型来建立这种依赖关系;将任何值绑定到任何对象的属性。
For example, in a C++ application that maps an "app.enteredText" property into QML, you can use Binding to update the enteredText property.
例如,在将“app.enteredText”属性映射到QML的C++应用程序中,可以使用绑定来更新enteredText属性。
TextEdit { id: myTextField; text: "Please type here..." }
Binding { app.enteredText: myTextField.text }
When text
changes, the C++ property enteredText
will update automatically.
当文本更改时,C++属性enteredText将自动更新。
Conditional Bindings
条件绑定
In some cases you may want to modify the value of a property when a certain condition is met but leave it unmodified otherwise. Often, it's not possible to do this with direct bindings, as you have to supply values for all possible branches.
在某些情况下,您可能希望在满足特定条件时修改属性的值,但在其他情况下不进行修改。通常,直接绑定不可能做到这一点,因为必须为所有可能的分支提供值。
For example, the code snippet below results in a warning whenever you release the mouse. This is because the value of the binding is undefined when the mouse isn't pressed.
例如,每当释放鼠标时,下面的代码片段都会产生警告。这是因为未按下鼠标时绑定的值未定义。
// produces warning: "Unable to assign [undefined] to double value"
value: if (mouse.pressed) mouse.mouseX
The Binding type can prevent this warning.
绑定类型可以阻止此警告。
Binding on value {
when: mouse.pressed
value: mouse.mouseX
}
The Binding type restores any previously set direct bindings on the property.
绑定类型恢复以前在属性上设置的任何直接绑定。
See also Qt QML.
另请参见Qt QML。
Property Documentation
属性文档
[since 5.8]delayed : bool |
This property holds whether the binding should be delayed.
此属性用于确定绑定是否应延迟。
A delayed binding will not immediately update the target, but rather wait until the event queue has been cleared. This can be used as an optimization, or to prevent intermediary values from being assigned.
延迟绑定不会立即更新目标,而是等待事件队列被清除。这可以用作优化,或防止分配中间值。
Binding {
contactName.text.value: givenName + " " + familyName
when: list.ListView.isCurrentItem
delayed: true
}
Note: Using the delayed property incurs a run time cost as the Binding element has to create a proxy for the value, so that it can delay its application to the actual target. When using the target and property properties, this cost is lower because the value property can be re-used as proxy. When using the form shown above, Binding will allocate a separate object with a dynamic meta-object to hold the proxy values.
注意:使用delayed属性会产生运行时成本,因为绑定元素必须为该值创建一个代理,以便它可以延迟对实际目标的应用。使用目标属性和属性属性时,此成本较低,因为值属性可以重新用作代理。当使用上面显示的表单时,绑定将分配一个单独的对象和一个动态元对象来保存代理值。
This property was introduced in Qt 5.8.
Qt 5.8中引入了这个属性。
property : string |
The property to be updated.
要更新的属性。
This can be a group property if the expression results in accessing a property of a value type. For example:
如果表达式导致访问值类型的属性,则该属性可以是组属性。例如:
Item {
id: item
property rect rectangle: Qt.rect(0, 0, 200, 200)
}
Binding {
target: item
property: "rectangle.x"
value: 100
}
You only need to use this property if you can't supply the binding target declaratively. The following snippet of code is equivalent to the above binding, but more compact:
只有在不能以声明方式提供绑定目标时,才需要使用此属性。以下代码片段相当于上述绑定,但更紧凑:
Binding { item.rectangle.x: 100 }
[since 5.14]restoreMode : enumeration |
This property can be used to describe if and how the original value should be restored when the binding is disabled.
此属性可用于描述在禁用绑定时是否以及如何恢复原始值。
The possible values are:
可能的值为:
- Binding.RestoreNone The original value is not restored at all
原始值根本不会恢复
- Binding.RestoreBinding The original value is restored if it was another binding. In that case the old binding is in effect again.
如果是另一个绑定,则恢复原始值。在这种情况下,旧的绑定再次生效。
- Binding.RestoreValue The original value is restored if it was a plain value rather than a binding.
如果原始值是普通值而不是绑定值,则会恢复原始值。
- Binding.RestoreBindingOrValue The original value is always restored.
原始值始终会恢复。
The default value is Binding.RestoreBindingOrValue
.
默认值为Binding.RestoreBindingOrValue
。
Note: This property exists for backwards compatibility with earlier versions of Qt. Don't use it in new code.
注意:此属性的存在是为了与早期版本的Qt向后兼容。不要在新代码中使用它。
This property was introduced in Qt 5.14.
Qt 5.14中引入了这个属性。
target : QtObject |
The object to be updated. You only need to use this property if you can't supply the binding target declaratively. The following two pieces of code are equivalent.
要更新的对象。只有在不能以声明方式提供绑定目标时,才需要使用此属性。以下两段代码是等效的。
Binding { contactName.text: name }
Binding { target: contactName property: "text" value: name }
The former one is much more compact, but you cannot replace the target object or property at run time. With the latter one you can.
前者更紧凑,但不能在运行时替换目标对象或属性。用后一个你可以。
value : var |
The value to be set on the target object and property. This can be a constant (which isn't very useful), or a bound expression.
要在目标对象和属性上设置的值。它可以是常量(这不是很有用),也可以是绑定表达式。
You only need to use this property if you can't supply the binding target declaratively. Otherwise you can directly bind to the target.
只有在不能以声明方式提供绑定目标时,才需要使用此属性。否则可以直接绑定到目标。
when : bool |
This property holds when the binding is active. This should be set to an expression that evaluates to true when you want the binding to be active.
当绑定处于活动状态时,此属性保持不变。当您希望绑定处于活动状态时,应将其设置为计算结果为true的表达式。
Binding {
contactName.text: name
when: list.ListView.isCurrentItem
}
By default, any binding or value that was set perviously is restored when the binding becomes inactive. You can customize the restoration behavior using the restoreMode property.
默认情况下,任何预先设置的绑定或值都会在绑定处于非活动状态时恢复。可以使用restoreMode属性自定义恢复行为。
See also restoreMode.
另请参见restoreMode。
© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.