Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
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
12
Many user interface designs are \e{state driven}; interfaces have configurations
13
that differ depending on the current state. For example, a traffic signal will
14
configure 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
16
will turn off. In the \c caution state, the yellow light is on while the other
17
lights are turned off.
18
19
In QML, \e states are a set of property configurations defined in a \l State
20
type. 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
31
All \l {Item}-based objects have a \c state property, and can specify additional
32
states by adding new \c State objects to the item's \l {Item::}{states}
33
property. Each state within a component has a unique \c name, an empty string
34
being the default. To change the current state
35
of an item, set the \l {Item::}{state} property to the name of the state.
36
37
Non-Item objects may use states through the \l StateGroup type.
38
39
\section1 Creating States
40
41
To create a state, add a \l State object to the item's \l {Item::}{states} property,
42
which holds a list of states for that item.
43
44
A 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
46
signal 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
48
model the states using the \c State type and the color and flag
49
configurations with the \c PropertyChanges type.
50
\snippet qml/states.qml signal states
51
The \l PropertyChanges type will change the values of object properties.
52
Objects are referenced through their
53
\l{qtqml-syntax-objectattributes.html#the-id-attribute}{id}. Objects outside
54
the component are also referenced using the \c id property, exemplified by the
55
property change to the external \c flag object.
56
57
Further, the state may change by assigning the \c state property with the
58
appropriate signal state. A state switch could be in a \l MouseArea type,
59
assigning a different state whenever the signal receives a mouse click.
60
\snippet qml/states.qml switch states
61
62
The State type is not limited to performing modifications on property values.
63
It 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
73
Every \l Item based component has a \c state property and a \e{default state}.
74
The default state is the empty string (\c{""}) and contains all of an item's
75
initial property values. The default state is useful for managing property
76
values before state changes. Setting the \c state property to an empty string
77
will load the default state.
78
79
\section1 The \c when Property
80
81
For convenience, the \l State type has a \c when property that can bind to
82
expressions 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
87
The \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
92
State changes induce abrupt value changes. The \l Transition type allow
93
smoother changes during state changes. In transitions, animations and
94
interpolation behaviors are definable. The
95
\l{qtquick-statesanimations-animations.html}
96
{Animation and Transitions} article has more information about creating state
97
animations.
98
99
The \l {Qt Quick Examples - Animation}{Animation} example
100
demonstrates how to declare a basic set of states and apply animated
101
transitions between them.
102
103
\l{qtquick-statesanimations-behaviors.html}{Using Qt Quick Behaviors with States}
104
explains a common problem when using Behaviors to animate state changes.
105
106
\section1 State Fast Forwarding
107
108
In order for Transition to correctly animate state changes, it is sometimes necessary
109
for the engine to fast forward and rewind a state (that is, internally set and unset the state)
110
before 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
118
In some cases this may cause unintended behavior. For example, a state that changes
119
a view's \e model or a Loader's \e sourceComponent will set these properties
120
multiple times (to apply, rewind, and then reapply), which can be relatively expensive.
121
122
State fast forwarding should be considered an implementation detail,
123
and may change in later versions.
124
125
*/
qtdeclarative
src
quick
doc
src
concepts
statesanimations
states.qdoc
Generated on
for Qt by
1.14.0