Binding QML 类型

使能够创建任意特性绑定。 更多...

import 语句: import QtQml 2.15

特性

详细描述

在 QML 中,特性绑定会导致不同对象特性之间的依赖。

绑定到不可访问特性

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.

For example, in a C++ application that maps an "app.enteredText" property into QML, you can use Binding to update the enteredText property.

TextEdit { id: myTextField; text: "Please type here..." }
Binding { target: app; property: "enteredText"; value: myTextField.text }
					

text 变化,C++ 特性 enteredText 会自动更新。

条件绑定

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
					

Binding 类型可以阻止此警告。

Binding on value {
    when: mouse.pressed
    value: mouse.mouseX
}
					

The Binding type restores any previously set direct bindings on the property.

另请参阅 Qt QML .

特性文档编制

delayed : bool

此特性保持 Binding 是否应延迟。

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 {
    target: contactName; property: 'text'
    value: givenName + " " + familyName; when: list.ListView.isCurrentItem
    delayed: true
}
					

该特性在 Qt 5.8 引入。

property : string

要更新的特性。

This can be a group property if the expression results in accessing a property of a 值类型 。例如:

Item {
    id: item
    property rect rectangle: Qt.rect(0, 0, 200, 200)
}
Binding {
    target: item
    property: "rectangle.x"
    value: 100
}
					

restoreMode : enumeration

This property can be used to describe if and how the original value should be restored when the binding is disabled.

可能的值包括:

  • 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.RestoreBinding. This will change in Qt 6.0 to Binding.RestoreBindingOrValue.

If you rely on any specific behavior regarding the restoration of plain values when bindings get disabled you should migrate to explicitly set the restoreMode.

Reliance on a restoreMode that doesn't restore the previous binding or value for a specific property results in a run-time warning.

该特性在 Qt 5.14 引入。

target : 对象

要更新的对象。

value : any

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.

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.

Binding {
    target: contactName; property: 'text'
    value: name; when: list.ListView.isCurrentItem
}
					

When the binding becomes inactive again, any direct bindings that were previously set on the property will be restored.

注意: By default, a previously set literal value is not restored when the Binding becomes inactive. Rather, the last value set by the now inactive Binding is retained. You can customize the restoration behavior for literal values as well as bindings using the restoreMode 特性。默认值将在 Qt 6.0 改变。

另请参阅 restoreMode .