IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)

Binding QML Type

Enables the arbitrary creation of property bindings.

Article lu   fois.

L'auteur

Liens sociaux

Viadeo Twitter Facebook Share on Google+   

Binding QML Type

  • Import Statement: import QtQml 2.13

  • Group: Binding is part of qtquick-interceptors

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.

 
Sélectionnez
TextEdit { id: myTextField; text: "Please type here..." }
Binding { target: app; property: "enteredText"; value: 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.

 
Sélectionnez
// produces warning: "Unable to assign [undefined] to double value"
value: if (mouse.pressed) mouse.mouseX

The Binding type can prevent this warning.

 
Sélectionnez
Binding on value {
    when: mouse.pressed
    value: mouse.mouseX
}

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

See Also

See also 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.

 
Sélectionnez
Binding {
    target: contactName; property: 'text'
    value: givenName + " " + familyName; when: list.ListView.isCurrentItem
    delayed: true
}

This property was introduced in 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:

 
Sélectionnez
Item {
    id: item

    property rect rectangle: Qt.rect(0, 0, 200, 200)
}

Binding {
    target: item
    property: "rectangle.x"
    value: 100
}

target : Object

The object to be updated.

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.

 
Sélectionnez
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.

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. This behavior will change in future versions of Qt.

Vous avez aimé ce tutoriel ? Alors partagez-le en cliquant sur les boutons suivants : Viadeo Twitter Facebook Share on Google+