Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Classes principales  ·  Annotées  ·  Classes groupées  ·  Modules  ·  Fonctions  · 

Binding

The Binding element allows arbitrary property bindings to be created. More...

Inherits QtObject

Properties

Detailed Description

Binding to an inaccessible property

Sometimes it is necessary to bind to a property of an object that wasn't directly instantiated by QML - generally a property of a class exported to QML by C++. In these cases, regular property binding doesn't work. Binding allows you to bind any value to any property.

For example, imagine a C++ application that maps an "app.enteredText" property into QML. You could use Binding to update the enteredText property like this.

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

Whenever the text in the TextEdit is updated, the C++ property will be updated also.

"Single-branch" conditional binding

In some circumstances you may want to control the value of a property only when a certain condition is true (and relinquish control in all other cirumstances). This often isn't possible to accomplish with a direct binding, as you need to supply values for all possible branches.

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

The above example will produce a warning whenever we release the mouse, as the value of the binding is undefined when the mouse isn't pressed. We can use the Binding element to rewrite the above code and avoid the warning.

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

The Binding element will also restore any previously set direct bindings on the property. In that sense, it functions much like a simplified State.

 // this is equivilant to the above Binding
 State {
     name: "pressed"
     when: mouse.pressed
     PropertyChanges {
         target: obj
         value: mouse.mouseX
     }
 }

If the binding target or binding property is changed, the bound value is immediately pushed onto the new target.

See also QtQml.

Property Documentation

property : string

The property to be updated.


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.

 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.


Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 5.0-snapshot
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web