source: trunk/doc/src/declarative/propertybinding.qdoc

Last change on this file was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 4.9 KB
Line 
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
32Property binding is a declarative way of specifying the value of a property. Binding allows
33a property's value to be expressed as an JavaScript expression that defines the value relative
34to other property values or data accessible in the application. The property value is
35automatically kept up to date if the other properties or data values change.
36
37Property bindings are created implicitly in QML whenever a property is assigned an JavaScript
38expression. The following QML uses two property bindings to connect the size of the rectangle
39to that of \c otherItem.
40
41\code
42Rectangle {
43 width: otherItem.width
44 height: otherItem.height
45}
46\endcode
47
48QML extends a standards compliant JavaScript engine, so any valid JavaScript expression can be
49used as a property binding. Bindings can access object properties, make function calls and even
50use builtin JavaScript objects like \e {Date} and \e {Math}. Assigning a constant value to a
51property can even be thought of as a binding - after all, a constant is a valid JavaScript
52expression! Here are some examples of more complex bindings:
53
54\code
55Rectangle {
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
67While syntactically bindings can be of arbitrary complexity, if a binding starts to become
68overly complex - such as involving multiple lines, or imperative loops - it may be better
69to refactor the component entirely, or at least factor the binding out into a separate
70function.
71
72\section1 Changing Bindings
73
74The \l PropertyChanges element can be used within a state change to modify the bindings on
75properties.
76
77This example modifies the \l Rectangle's width property binding to be \c {otherItem.height}
78when in the "square" state. When it returns to its default state, width's original property
79binding will have been restored.
80
81\code
82Rectangle {
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
100Assigning a property value from JavaScript does \e not create a property binding.
101For example:
102
103\code
104Rectangle {
105
106 Component.onCompleted: {
107 width = otherItem.width;
108 }
109}
110\endcode
111
112Instead of creating a property binding, this simply sets the \c width of the \l Rectangle
113to 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
116Also note that assigning a value to a property that is currently bound will remove the binding.
117A property can only have one value at a time, and if any code explicitly sets
118this value, the binding is removed. The \l Rectangle in the example below will have
119a width of 13, regardless of the \c otherItem's width.
120
121\code
122Rectangle {
123 width: otherItem.width
124
125 Component.onCompleted: {
126 width = 13;
127 }
128}
129\endcode
130
131There is no way to create a property binding directly from imperative JavaScript code,
132although it is possible to set up a \l Binding object (shown below).
133
134
135\section1 Binding Element
136
137The implicit binding syntax shown previously is easy to use and works perfectly for most uses
138of bindings. In some advanced cases, it is necessary to create bindings explicitly using the
139\l Binding element.
140
141For example, to bind a property exposed from C++ (\c system.brightness) to a value
142coming from QML (\c slider.value), you could use the Binding element as follows:
143\qml
144Binding {
145 target: system
146 property: "brightness"
147 value: slider.value
148}
149\endqml
150
151
152*/
153
Note: See TracBrowser for help on using the repository browser.