1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the documentation of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:FDL$
9 ** GNU Free Documentation License
10 ** Alternatively, this file may be used under the terms of the GNU Free
11 ** Documentation License version 1.3 as published by the Free Software
12 ** Foundation and appearing in the file included in the packaging of
16 ** Alternatively, this file may be used in accordance with the terms
17 ** and conditions contained in a signed written agreement between you
26 ****************************************************************************/
29 \page qdeclarativestates.html
30 \inqmlmodule QtQuick 2
32 \contentspage QML Features
33 \previouspage {Importing Reusable Components}
34 \nextpage {QML Animation and Transitions}{Animation and Transitions}
38 \section1 States Elements
43 \o \l StateChangeScript
48 Many user interface designs are \i state driven; interfaces have configurations
49 that differ depending on the current state. For example, a traffic signal will
50 configure its flags or lights depending on its state. While in the signal's
51 \c stop state, a red light will turn on while the yellow and the green lights
52 will turn off. In the \c caution state, the yellow light is on while the other
53 lights are turned off.
55 In QML, \i states are a set of property configurations defined in a \l State
56 element. Different configurations could, for example:
59 \o Show some UI elements and hide others
60 \o Present different available actions to the user
61 \o Start, stop, or pause animations
62 \o Execute some script required in the new state
63 \o Change a property value for a particular item
64 \o Show a different view or screen
67 All \l {Item}-based objects have a \c state property, and can specify additional
68 states by adding new \c State objects to the item's \l {Item::}{states}
69 property. Each state within a component has a unique \c name, an empty string
70 being the default. To change the current state
71 of an item, set the \l {Item::}{state} property to the name of the state.
73 Non-Item objects may use states through the \l StateGroup element.
75 \section1 Creating States
77 To create a state, add a \l State object to the item's \l {Item::}{states} property,
78 which holds a list of states for that item.
80 A warning \c signal component may have two states, the \c NORMAL and the
81 \c CRITICAL state. Suppose that in the \c NORMAL state, the \c color of the
82 signal should be \c green and the warning \c flag is down. Meanwhile, in the
83 \c CRITICAL state, the \c color should be \c red and the flag is \c up. We may
84 model the states using the \c State element and the color and flag
85 configurations with the \c PropertyChanges element.
86 \snippet doc/src/snippets/declarative/states.qml signal states
87 The \l PropertyChanges element will change the values of object properties.
88 Objects are referenced through their \l {qml-id-property}{id}. Objects outside
89 the component are also referenced using the \c id property, exemplified by the
90 property change to the external \c flag object.
92 Further, the state may change by assigning the \c state property with the
93 appropriate signal state. A state switch could be in a \l MouseArea element,
94 assigning a different state whenever the signal receives a mouse click.
95 \snippet doc/src/snippets/declarative/states.qml switch states
97 The State element is not limited to performing modifications on property values.
100 \o Run some script using \l StateChangeScript
101 \o Override an existing signal handler for an object using \l PropertyChanges
102 \o Re-parent an \l Item using \l ParentChange
103 \o Modify anchor values using \l AnchorChanges
106 \section1 The Default State
108 Every \l Item based component has a \c state property and a \i{default state}.
109 The default state is the empty string (\c{""}) and contains all of an item's
110 initial property values. The default state is useful for managing property
111 values before state changes. Setting the \c state property to an empty string
112 will load the default state.
114 \section1 The \c when Property
116 For convenience, the \l State element has a \c when property that can bind to
117 expressions to change the state whenever the bound expression evaluates to
118 \c true. The \c when property will revert the state back to the
119 \l {The Default State}{default state} when the expression evaluates to false.
121 \snippet doc/src/snippets/declarative/states.qml when property
122 The \c bell component will change to the \c RINGING state whenever the
123 \c signal.state is \c CRITICAL.
125 \section1 Animating State Changes
127 State changes induce abrupt value changes. The \l Transition element allow
128 smoother changes during state changes. In transitions, animations and
129 interpolation behaviors are definable. The
130 \l {QML Animation and Transitions}{Animation and Transitions} article has more
131 information about creating state animations.
133 The \l {declarative/animation/states}{States and Transitions example}
134 demonstrates how to declare a basic set of states and apply animated
135 transitions between them.
137 \l{Using QML Behaviors with States} explains a common problem when using Behaviors
138 to animate state changes.
140 \section1 State Fast Forwarding
142 In order for Transition to correctly animate state changes, it is sometimes necessary
143 for the engine to fast forward and rewind a state (that is, internally set and unset the state)
144 before it is finally applied. The process is as follows:
147 \o The state is fast forwarded to determine the complete set of end values.
148 \o The state is rewound.
149 \o The state is fully applied, with transitions.
152 In some cases this may cause unintended behavior. For example, a state that changes
153 a view's \i model or a Loader's \i sourceComponent will set these properties
154 multiple times (to apply, rewind, and then reapply), which can be relatively expensive.
156 State fast forwarding should be considered an implementation detail,
157 and may change in later versions.