2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5\page qtquick-statesanimations-behaviors.html
6\title Using Qt Quick Behaviors with States
7\brief animating property changes with behaviors
8
9\section1 Using Behaviors with States
10
11In some cases you may choose to use a Behavior to animate a property change caused by a state change. While this works well for some situations, in other situations it may lead to unexpected behavior.
12
13Here's an example that shows the problem:
14
15\qml
16import QtQuick 2.0
17
18Rectangle {
19 width: 400
20 height: 400
21
22 Rectangle {
23 id: coloredRect
24 width: 100
25 height: 100
26 anchors.centerIn: parent
27
28 color: "red"
29 Behavior on color {
30 ColorAnimation {}
31 }
32
33 MouseArea {
34 id: mouser
35 anchors.fill: parent
36 hoverEnabled: true
37 }
38
39 states: State {
40 name: "GreenState"
41 when: mouser.containsMouse
42
43 PropertyChanges {
44 target: coloredRect
45 color: "green"
46 }
47 }
48 }
49}
50\endqml
51
52Testing the example by quickly and repeatedly moving the mouse in to and out of the colored rectangle shows that the colored rectangle will settle into a green color over time, never returning to full red. This is not what we wanted! The
53problem occurs because we have used a Behavior to animate the change in color, and our state change is trigged by the mouse entering or exiting the MouseArea, which is easily interrupted.
54
55To state the problem more formally, using States and Behaviors together can cause unexpected behavior when:
56\list
57\li a Behavior is used to animate a property change, specifically when moving from an explicitly defined state back to the implicit base state; and
58\li this Behavior can be interrupted to (re-)enter an explicitly defined state.
59\endlist
60
61The problem occurs because of the way the base state is defined for QML: as the "snapshot" state of the application just prior to entering an explicitly defined state. In this case, if we are in the process of animating from green back
62to red, and interrupt the animation to return to "GreenState", the base state will include the color in its intermediate, mid-animation form.
63
64While future versions of QML should be able to handle this situation more gracefully, there are currently several ways to rework your application to avoid this problem.
65
661. Use a transition to animate the change, rather than a Behavior.
67
68\qml
69import QtQuick 2.0
70
71Rectangle {
72 width: 400
73 height: 400
74
75 Rectangle {
76 id: coloredRect
77 width: 100
78 height: 100
79 anchors.centerIn: parent
80
81 color: "red"
82
83 MouseArea {
84 id: mouser
85 anchors.fill: parent
86 hoverEnabled: true
87 }
88
89 states: State {
90 name: "GreenState"
91 when: mouser.containsMouse
92
93 PropertyChanges {
94 target: coloredRect
95 color: "green"
96 }
97 }
98
99 transitions: Transition {
100 ColorAnimation {}
101 }
102 }
103}
104\endqml
105
1062. Use a conditional binding to change the property value, rather than a state
107
108\qml
109import QtQuick 2.0
110
111Rectangle {
112 width: 400
113 height: 400
114
115 Rectangle {
116 id: coloredRect
117 width: 100
118 height: 100
119 anchors.centerIn: parent
120
121 color: mouser.containsMouse ? "green" : "red"
122 Behavior on color {
123 ColorAnimation {}
124 }
125
126 MouseArea {
127 id: mouser
128 anchors.fill: parent
129 hoverEnabled: true
130 }
131 }
132}
133\endqml
134
1353. Use only explicitly defined states, rather than an implicit base state