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 |
|
---|
35 | User interfaces are designed to present different interface configurations in
|
---|
36 | different scenarios, or to modify their appearances in response to user
|
---|
37 | interaction. Often, there are a set of changes that are made concurrently, such
|
---|
38 | that the interface could be seen to be internally changing from one \e state to
|
---|
39 | another.
|
---|
40 |
|
---|
41 | This applies generally to interface elements regardless of their complexity.
|
---|
42 | A photo viewer may initially present images in a grid, and when an image is
|
---|
43 | clicked, change to a "detailed" state where the individual image is expanded
|
---|
44 | and the interface is changed to present new options for image editing. On the
|
---|
45 | other 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
|
---|
47 | appearance.
|
---|
48 |
|
---|
49 | In QML, any object can change between different \e states to apply sets of
|
---|
50 | changes that modify the properties of relevant items. Each \e state could
|
---|
51 | present 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 |
|
---|
62 | Changes between states can be animated using \l {Transitions}{transitions}, as
|
---|
63 | discussed further below.
|
---|
64 |
|
---|
65 | All \l {Item}-based objects have a \e {default state}, and can specify additional
|
---|
66 | states by adding new \l State objects to the item's \l {Item::}{states}
|
---|
67 | property. Each state has a \e name that is unique for all states within that
|
---|
68 | item; the default state's name is an empty string. To change the current state
|
---|
69 | of an item, set the \l {Item::}{state} property to the name of the state.
|
---|
70 |
|
---|
71 | Non-Item objects can use states through the StateGroup element.
|
---|
72 |
|
---|
73 |
|
---|
74 | \section1 Creating states
|
---|
75 |
|
---|
76 | To create a state, add a \l State object to the item's \l {Item::}{states} property,
|
---|
77 | which holds a list of states for that item.
|
---|
78 |
|
---|
79 | Following is an example. Here, the \l Rectangle is initially placed in the
|
---|
80 | default (0, 0) position. It has defined an additional state named "moved", in
|
---|
81 | which a PropertyChanges object repositions the rectangle to (50, 50). Clicking
|
---|
82 | within the MouseArea changes the state to the "moved" state, thus moving the \l
|
---|
83 | Rectangle.
|
---|
84 |
|
---|
85 | \snippet doc/src/snippets/declarative/states.qml 0
|
---|
86 |
|
---|
87 | The \l State item defines all the changes to be made in the new state. It
|
---|
88 | could specify additional properties to be changed, or create additional
|
---|
89 | PropertyChanges for other objects. It can also modify the properties of other
|
---|
90 | objects, not just the object that owns the state. For example:
|
---|
91 |
|
---|
92 | \qml
|
---|
93 | Rectangle {
|
---|
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 |
|
---|
105 | As a convenience, if an item only has one state, its \l {Item::}{states}
|
---|
106 | property can be defined as a single \l State, without the square-brace list
|
---|
107 | syntax:
|
---|
108 |
|
---|
109 | \qml
|
---|
110 | Item {
|
---|
111 | ...
|
---|
112 | states: State {
|
---|
113 | ...
|
---|
114 | }
|
---|
115 | }
|
---|
116 | \endqml
|
---|
117 |
|
---|
118 | A \l State is not limited to performing modifications on property values. It
|
---|
119 | can 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 |
|
---|
128 | The \l {declarative/animation/states}{States and Transitions example}
|
---|
129 | demonstrates how to declare a basic set of states and apply animated
|
---|
130 | transitions between them.
|
---|
131 |
|
---|
132 |
|
---|
133 | \section1 The default state
|
---|
134 |
|
---|
135 | Of course, the \l Rectangle in the example above could have simply been moved
|
---|
136 | by setting its position to (50, 50) in the mouse area's \c onClicked handler.
|
---|
137 | However, aside from enabling batched property changes, one of the features of
|
---|
138 | QML states is the ability of an item to revert to its \e {default state}.
|
---|
139 | The default state contains all of an item's initial property values before
|
---|
140 | they were modified in a state change.
|
---|
141 |
|
---|
142 | For example, suppose the \l Rectangle should move to (50,50) when the mouse is
|
---|
143 | pressed, and then move back to its original position when the mouse is
|
---|
144 | released. This can be achieved by using the \l {State::}{when} property,
|
---|
145 | like this:
|
---|
146 |
|
---|
147 | \qml
|
---|
148 | Rectangle {
|
---|
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 |
|
---|
163 | The \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,
|
---|
165 | the 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 |
|
---|
168 | Alternatively, 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
|
---|
170 | using the \l {State::}{when} property, the above code could be changed to:
|
---|
171 |
|
---|
172 | \qml
|
---|
173 | Rectangle {
|
---|
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 |
|
---|
189 | Obviously it makes sense to use the \l {State::}{when} property when possible
|
---|
190 | as it provides a simpler (and a better, more declarative) solution than
|
---|
191 | assigning the state from signal handlers.
|
---|
192 |
|
---|
193 |
|
---|
194 | \section1 Animating state changes
|
---|
195 |
|
---|
196 |
|
---|
197 | State changes can be easily animated through \l {Transitions}{transitions}. A
|
---|
198 | \l Transition defines the animations that should be applied when an item
|
---|
199 | changes from one state to another.
|
---|
200 |
|
---|
201 | If the above example was modified to include the following \l Transition, the
|
---|
202 | movement of the \l Rectangle would be animated:
|
---|
203 |
|
---|
204 | \qml
|
---|
205 | Rectangle {
|
---|
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 |
|
---|
222 | This \l Transition defines that if any \c x or \c y properties have changed
|
---|
223 | during a state change within this item, their values should be animated over 500
|
---|
224 | milliseconds.
|
---|
225 |
|
---|
226 | See the \l Transitions documentation for more information.
|
---|
227 |
|
---|
228 | */
|
---|