Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
states.qdoc
Go to the documentation of this file.
1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5\page qtquick-statesanimations-states.html
6\title Qt Quick States
7\brief Creating and setting states
8
9\section1 Related Types
10\annotatedlist qtquick-states
11
12Many user interface designs are \e{state driven}; interfaces have configurations
13that differ depending on the current state. For example, a traffic signal will
14configure its flags or lights depending on its state. While in the signal's
15\c stop state, a red light will turn on while the yellow and the green lights
16will turn off. In the \c caution state, the yellow light is on while the other
17lights are turned off.
18
19In QML, \e states are a set of property configurations defined in a \l State
20type. Different configurations could, for example:
21
22\list
23\li Show some UI components and hide others
24\li Present different available actions to the user
25\li Start, stop, or pause animations
26\li Execute some script required in the new state
27\li Change a property value for a particular item
28\li Show a different view or screen
29\endlist
30
31All \l {Item}-based objects have a \c state property, and can specify additional
32states by adding new \c State objects to the item's \l {Item::}{states}
33property. Each state within a component has a unique \c name, an empty string
34being the default. To change the current state
35of an item, set the \l {Item::}{state} property to the name of the state.
36
37Non-Item objects may use states through the \l StateGroup type.
38
39\section1 Creating States
40
41To create a state, add a \l State object to the item's \l {Item::}{states} property,
42which holds a list of states for that item.
43
44A warning \c signal component may have two states, the \c NORMAL and the
45\c CRITICAL state. Suppose that in the \c NORMAL state, the \c color of the
46signal should be \c green and the warning \c flag is down. Meanwhile, in the
47\c CRITICAL state, the \c color should be \c red and the flag is \c up. We may
48model the states using the \c State type and the color and flag
49configurations with the \c PropertyChanges type.
50\snippet qml/states.qml signal states
51The \l PropertyChanges type will change the values of object properties.
52Objects are referenced through their
53\l{qtqml-syntax-objectattributes.html#the-id-attribute}{id}. Objects outside
54the component are also referenced using the \c id property, exemplified by the
55property change to the external \c flag object.
56
57Further, the state may change by assigning the \c state property with the
58appropriate signal state. A state switch could be in a \l MouseArea type,
59assigning a different state whenever the signal receives a mouse click.
60\snippet qml/states.qml switch states
61
62The State type is not limited to performing modifications on property values.
63It can also:
64\list
65\li Run some script using \l StateChangeScript
66\li Override an existing signal handler for an object using \l PropertyChanges
67\li Re-parent an \l Item using \l ParentChange
68\li Modify anchor values using \l AnchorChanges
69\endlist
70
71\section1 The Default State
72
73Every \l Item based component has a \c state property and a \e{default state}.
74The default state is the empty string (\c{""}) and contains all of an item's
75initial property values. The default state is useful for managing property
76values before state changes. Setting the \c state property to an empty string
77will load the default state.
78
79\section1 The \c when Property
80
81For convenience, the \l State type has a \c when property that can bind to
82expressions to change the state whenever the bound expression evaluates to
83\c true. The \c when property will revert the state back to the
84\l {The Default State}{default state} when the expression evaluates to false.
85
86\snippet qml/states.qml when property
87The \c bell component will change to the \c RINGING state whenever the
88\c signal.state is \c CRITICAL.
89
90\section1 Animating State Changes
91
92State changes induce abrupt value changes. The \l Transition type allow
93smoother changes during state changes. In transitions, animations and
94interpolation behaviors are definable. The
95\l{qtquick-statesanimations-animations.html}
96{Animation and Transitions} article has more information about creating state
97animations.
98
99The \l {Qt Quick Examples - Animation}{Animation} example
100demonstrates how to declare a basic set of states and apply animated
101transitions between them.
102
103\l{qtquick-statesanimations-behaviors.html}{Using Qt Quick Behaviors with States}
104explains a common problem when using Behaviors to animate state changes.
105
106\section1 State Fast Forwarding
107
108In order for Transition to correctly animate state changes, it is sometimes necessary
109for the engine to fast forward and rewind a state (that is, internally set and unset the state)
110before it is finally applied. The process is as follows:
111
112\list 1
113\li The state is fast forwarded to determine the complete set of end values.
114\li The state is rewound.
115\li The state is fully applied, with transitions.
116\endlist
117
118In some cases this may cause unintended behavior. For example, a state that changes
119a view's \e model or a Loader's \e sourceComponent will set these properties
120multiple times (to apply, rewind, and then reapply), which can be relatively expensive.
121
122State fast forwarding should be considered an implementation detail,
123and may change in later versions.
124
125*/