1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:FDL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in a
|
---|
14 | ** written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Free Documentation License
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Free
|
---|
18 | ** Documentation License version 1.3 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file included in the packaging of this
|
---|
20 | ** file.
|
---|
21 | **
|
---|
22 | ** If you have questions regarding the use of this file, please contact
|
---|
23 | ** Nokia at [email protected].
|
---|
24 | ** $QT_END_LICENSE$
|
---|
25 | **
|
---|
26 | ****************************************************************************/
|
---|
27 |
|
---|
28 | /*!
|
---|
29 | \page propertybinding.html
|
---|
30 | \title Property Binding
|
---|
31 |
|
---|
32 | Property binding is a declarative way of specifying the value of a property. Binding allows
|
---|
33 | a property's value to be expressed as an JavaScript expression that defines the value relative
|
---|
34 | to other property values or data accessible in the application. The property value is
|
---|
35 | automatically kept up to date if the other properties or data values change.
|
---|
36 |
|
---|
37 | Property bindings are created implicitly in QML whenever a property is assigned an JavaScript
|
---|
38 | expression. The following QML uses two property bindings to connect the size of the rectangle
|
---|
39 | to that of \c otherItem.
|
---|
40 |
|
---|
41 | \code
|
---|
42 | Rectangle {
|
---|
43 | width: otherItem.width
|
---|
44 | height: otherItem.height
|
---|
45 | }
|
---|
46 | \endcode
|
---|
47 |
|
---|
48 | QML extends a standards compliant JavaScript engine, so any valid JavaScript expression can be
|
---|
49 | used as a property binding. Bindings can access object properties, make function calls and even
|
---|
50 | use builtin JavaScript objects like \e {Date} and \e {Math}. Assigning a constant value to a
|
---|
51 | property can even be thought of as a binding - after all, a constant is a valid JavaScript
|
---|
52 | expression! Here are some examples of more complex bindings:
|
---|
53 |
|
---|
54 | \code
|
---|
55 | Rectangle {
|
---|
56 | function calculateMyHeight() {
|
---|
57 | return Math.max(otherItem.height, thirdItem.height);
|
---|
58 | }
|
---|
59 |
|
---|
60 | anchors.centerIn: parent
|
---|
61 | width: Math.min(otherItem.width, 10)
|
---|
62 | height: calculateMyHeight()
|
---|
63 | color: { if (width > 10) "blue"; else "red" }
|
---|
64 | }
|
---|
65 | \endcode
|
---|
66 |
|
---|
67 | While syntactically bindings can be of arbitrary complexity, if a binding starts to become
|
---|
68 | overly complex - such as involving multiple lines, or imperative loops - it may be better
|
---|
69 | to refactor the component entirely, or at least factor the binding out into a separate
|
---|
70 | function.
|
---|
71 |
|
---|
72 | \section1 Changing Bindings
|
---|
73 |
|
---|
74 | The \l PropertyChanges element can be used within a state change to modify the bindings on
|
---|
75 | properties.
|
---|
76 |
|
---|
77 | This example modifies the \l Rectangle's width property binding to be \c {otherItem.height}
|
---|
78 | when in the "square" state. When it returns to its default state, width's original property
|
---|
79 | binding will have been restored.
|
---|
80 |
|
---|
81 | \code
|
---|
82 | Rectangle {
|
---|
83 | id: rectangle
|
---|
84 | width: otherItem.width
|
---|
85 | height: otherItem.height
|
---|
86 |
|
---|
87 | states: State {
|
---|
88 | name: "square"
|
---|
89 | PropertyChanges {
|
---|
90 | target: rectangle
|
---|
91 | width: otherItem.height
|
---|
92 | }
|
---|
93 | }
|
---|
94 | }
|
---|
95 | \endcode
|
---|
96 |
|
---|
97 |
|
---|
98 | \section1 Effects of Property Assignment in JavaScript
|
---|
99 |
|
---|
100 | Assigning a property value from JavaScript does \e not create a property binding.
|
---|
101 | For example:
|
---|
102 |
|
---|
103 | \code
|
---|
104 | Rectangle {
|
---|
105 |
|
---|
106 | Component.onCompleted: {
|
---|
107 | width = otherItem.width;
|
---|
108 | }
|
---|
109 | }
|
---|
110 | \endcode
|
---|
111 |
|
---|
112 | Instead of creating a property binding, this simply sets the \c width of the \l Rectangle
|
---|
113 | to the value of \c other.width at the time the JavaScript code is invoked. See
|
---|
114 | \l {Property Assignment vs Property Binding} for more details.
|
---|
115 |
|
---|
116 | Also note that assigning a value to a property that is currently bound will remove the binding.
|
---|
117 | A property can only have one value at a time, and if any code explicitly sets
|
---|
118 | this value, the binding is removed. The \l Rectangle in the example below will have
|
---|
119 | a width of 13, regardless of the \c otherItem's width.
|
---|
120 |
|
---|
121 | \code
|
---|
122 | Rectangle {
|
---|
123 | width: otherItem.width
|
---|
124 |
|
---|
125 | Component.onCompleted: {
|
---|
126 | width = 13;
|
---|
127 | }
|
---|
128 | }
|
---|
129 | \endcode
|
---|
130 |
|
---|
131 | There is no way to create a property binding directly from imperative JavaScript code,
|
---|
132 | although it is possible to set up a \l Binding object (shown below).
|
---|
133 |
|
---|
134 |
|
---|
135 | \section1 Binding Element
|
---|
136 |
|
---|
137 | The implicit binding syntax shown previously is easy to use and works perfectly for most uses
|
---|
138 | of bindings. In some advanced cases, it is necessary to create bindings explicitly using the
|
---|
139 | \l Binding element.
|
---|
140 |
|
---|
141 | For example, to bind a property exposed from C++ (\c system.brightness) to a value
|
---|
142 | coming from QML (\c slider.value), you could use the Binding element as follows:
|
---|
143 | \qml
|
---|
144 | Binding {
|
---|
145 | target: system
|
---|
146 | property: "brightness"
|
---|
147 | value: slider.value
|
---|
148 | }
|
---|
149 | \endqml
|
---|
150 |
|
---|
151 |
|
---|
152 | */
|
---|
153 |
|
---|