Binding QML Type | Qt Qml | Qt 6.11.1
Description: Enables the arbitrary creation of property bindings.

- Qt 6.11
- Qt Qml
- QML Types
- Binding
- Qt 6.11.1 Reference Documentation

# Binding QML Type

Enables the arbitrary creation of property bindings. More...

Import Statement: | `import QtQml`

- List of all members, including inherited members

## Properties

- delayed : bool
- property : string
- restoreMode : enumeration
- target : QtObject
- value : var
- when : bool

## Detailed Description

In QML, property bindings result in a dependency between the properties of different objects.

## 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.

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 { app.enteredText: myTextField.text }

When `text` changes, the C++ property `enteredText` will update automatically.

## 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"
     targetProperty: if (mouse.pressed) mouse.mouseX

The Binding type can prevent this warning.

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

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

However, the binding to `Binding`'s value property is still evaluated unconditionally, even when `when` is `false`.

This might cause warnings when the Binding is for example used to guard against a value being `null`:

     Binding on targetProperty {
         when: root.object !== null
         //  produces warning: "TypeError: Cannot read property 'someProperty' of null"
         value: root.object.someProperty
     }

There are to ways to avoid this issue:

- If the target has an `id`, you can use the syntax described in Multiple targets in one Binding, which also causes the binding expression to only run when the `Binding` is enabled:

       Binding {
           when: root.object !== null
           root.targetProperty: root.object.someProperty
       }

- You can repeat the check in the binding and return early:

       Binding on targetProperty {
           when: root.object !== null
           value: {
               if (root.object === null)
                   return
               return root.object.someProperty
           }
       }

## Multiple targets in one Binding

You can specify multiple bindings to the same object in one Binding element:

     Text {
         id: t1
     }

     Binding {
         t1 {
             color: "#00FF00"
             text: "green text"
         }
     }

You can also specify several bindings with different target objects in a single Binding element:

     Text {
         id: t1
     }

     Text {
         id: t2
     }

     Binding {
         t1.text: "Foo"
         t2.text: "Bar"
     }

See also Qt Qml.

## Property Documentation

### 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.

### 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 }

### 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:

Constant | Description
--- | ---
`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`.

Note: This property exists for backwards compatibility with earlier versions of Qt. Don't use it in new code.

### target : QtObject

The object to be updated. You need to use this property if the binding target does not have an `id` attribute (for example, when the target is a singleton). Otherwise, 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.

     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.

See also restoreMode.