source: trunk/doc/src/declarative/qdeclarativestates.qdoc@ 1147

Last change on this file since 1147 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: 7.3 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation (qt-info@nokia.com)
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 qt-info@nokia.com.
24** $QT_END_LICENSE$
25**
26****************************************************************************/
27
28/*!
29\page qdeclarativestates.html
30\target qmlstates
31\title QML States
32
33\section1 Overview
34
35User interfaces are designed to present different interface configurations in
36different scenarios, or to modify their appearances in response to user
37interaction. Often, there are a set of changes that are made concurrently, such
38that the interface could be seen to be internally changing from one \e state to
39another.
40
41This applies generally to interface elements regardless of their complexity.
42A photo viewer may initially present images in a grid, and when an image is
43clicked, change to a "detailed" state where the individual image is expanded
44and the interface is changed to present new options for image editing. On the
45other end of the scale, when a simple button is pressed, it may change to a
46"pressed" state in which its color and position is modified to give a pressed
47appearance.
48
49In QML, any object can change between different \e states to apply sets of
50changes that modify the properties of relevant items. Each \e state could
51present a different configuration that could, for example:
52
53\list
54\o Show some UI elements and hide others
55\o Present different available actions to the user
56\o Start, stop or pause animations
57\o Execute some script required in the new state
58\o Change a property value for a particular item
59\o Show a different view or "screen"
60\endlist
61
62Changes between states can be animated using \l {Transitions}{transitions}, as
63discussed further below.
64
65All \l {Item}-based objects have a \e {default state}, and can specify additional
66states by adding new \l State objects to the item's \l {Item::}{states}
67property. Each state has a \e name that is unique for all states within that
68item; the default state's name is an empty string. To change the current state
69of an item, set the \l {Item::}{state} property to the name of the state.
70
71Non-Item objects can use states through the StateGroup element.
72
73
74\section1 Creating states
75
76To create a state, add a \l State object to the item's \l {Item::}{states} property,
77which holds a list of states for that item.
78
79Following is an example. Here, the \l Rectangle is initially placed in the
80default (0, 0) position. It has defined an additional state named "moved", in
81which a PropertyChanges object repositions the rectangle to (50, 50). Clicking
82within the MouseArea changes the state to the "moved" state, thus moving the \l
83Rectangle.
84
85\snippet doc/src/snippets/declarative/states.qml 0
86
87The \l State item defines all the changes to be made in the new state. It
88could specify additional properties to be changed, or create additional
89PropertyChanges for other objects. It can also modify the properties of other
90objects, not just the object that owns the state. For example:
91
92\qml
93Rectangle {
94 ...
95 states: [
96 State {
97 name: "moved"
98 PropertyChanges { target: myRect; x: 50; y: 50; color: "blue" }
99 PropertyChanges { target: someOtherItem; width: 1000 }
100 }
101 ]
102}
103\endqml
104
105As a convenience, if an item only has one state, its \l {Item::}{states}
106property can be defined as a single \l State, without the square-brace list
107syntax:
108
109\qml
110Item {
111 ...
112 states: State {
113 ...
114 }
115}
116\endqml
117
118A \l State is not limited to performing modifications on property values. It
119can also:
120
121\list
122\o Run some script using StateChangeScript
123\o Override an existing signal handler for an object using PropertyChanges
124\o Re-parent an \l Item using ParentChanges
125\o Modify anchor values using AnchorChanges
126\endlist
127
128The \l {declarative/animation/states}{States and Transitions example}
129demonstrates how to declare a basic set of states and apply animated
130transitions between them.
131
132
133\section1 The default state
134
135Of course, the \l Rectangle in the example above could have simply been moved
136by setting its position to (50, 50) in the mouse area's \c onClicked handler.
137However, aside from enabling batched property changes, one of the features of
138QML states is the ability of an item to revert to its \e {default state}.
139The default state contains all of an item's initial property values before
140they were modified in a state change.
141
142For example, suppose the \l Rectangle should move to (50,50) when the mouse is
143pressed, and then move back to its original position when the mouse is
144released. This can be achieved by using the \l {State::}{when} property,
145like this:
146
147\qml
148Rectangle {
149 ...
150
151 MouseArea {
152 id: mouseArea
153 anchors.fill: parent
154 }
155
156 states: State {
157 name: "moved"; when: mouseArea.pressed
158 ...
159 }
160}
161\endqml
162
163The \l {State::}{when} property is set to an expression that evaluates to
164\c true when the item should be set to that state. When the mouse is pressed,
165the state is changed to \e moved. When it is released, the item reverts to its
166\e default state, which defines all of the item's original property values.
167
168Alternatively, an item can be explicitly set to its default state by setting its
169\l {Item::}{state} property to an empty string (""). For example, instead of
170using the \l {State::}{when} property, the above code could be changed to:
171
172\qml
173Rectangle {
174 ...
175
176 MouseArea {
177 anchors.fill: parent
178 onPressed: myRect.state = 'moved';
179 onReleased: myRect.state = '';
180 }
181
182 states: State {
183 name: "moved"
184 ...
185 }
186}
187\endqml
188
189Obviously it makes sense to use the \l {State::}{when} property when possible
190as it provides a simpler (and a better, more declarative) solution than
191assigning the state from signal handlers.
192
193
194\section1 Animating state changes
195
196
197State changes can be easily animated through \l {Transitions}{transitions}. A
198\l Transition defines the animations that should be applied when an item
199changes from one state to another.
200
201If the above example was modified to include the following \l Transition, the
202movement of the \l Rectangle would be animated:
203
204\qml
205Rectangle {
206 ...
207
208 MouseArea { ... }
209
210 states: [
211 ...
212 ]
213
214 transitions: [
215 Transition {
216 NumberAnimation { properties: "x,y"; duration: 500 }
217 }
218 ]
219 }
220\endqml
221
222This \l Transition defines that if any \c x or \c y properties have changed
223during a state change within this item, their values should be animated over 500
224milliseconds.
225
226See the \l Transitions documentation for more information.
227
228*/
Note: See TracBrowser for help on using the repository browser.