File: qml-behaviors-and-states.html

package info (click to toggle)
qt4-x11 4%3A4.8.2%2Bdfsg-11
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 701,696 kB
  • sloc: cpp: 2,686,179; ansic: 375,485; python: 25,859; sh: 19,349; xml: 17,091; perl: 14,765; yacc: 5,383; asm: 5,038; makefile: 1,259; lex: 555; ruby: 526; objc: 347; cs: 112; pascal: 112; php: 54; sed: 34
file content (212 lines) | stat: -rw-r--r-- 11,751 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- behaviors-and-states.qdoc -->
  <title>Qt 4.8: Using QML Behaviors with States</title>
  <link rel="stylesheet" type="text/css" href="style/offline.css" />
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="content"> 
    <a href="index.html" class="qtref"><span>Qt Reference Documentation</span></a>
  </div>
  <div class="breadcrumb toolblock">
    <ul>
      <li class="first"><a href="index.html">Home</a></li>
      <!--  Breadcrumbs go here -->
<li>Using QML Behaviors with States</li>
    </ul>
  </div>
</div>
<div class="content mainContent">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#using-behaviors-with-states">Using Behaviors with States</a></li>
</ul>
</div>
<h1 class="title">Using QML Behaviors with States</h1>
<span class="subtitle"></span>
<!-- $$$qml-behaviors-and-states.html-description -->
<div class="descr"> <a name="details"></a>
<a name="using-behaviors-with-states"></a>
<h2>Using Behaviors with States</h2>
<p>In 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.</p>
<p>Here's an example that shows the problem:</p>
<pre class="qml"> import QtQuick 1.0

 <span class="type"><a href="qml-rectangle.html">Rectangle</a></span> {
     <span class="name">width</span>: <span class="number">400</span>
     <span class="name">height</span>: <span class="number">400</span>

     <span class="type"><a href="qml-rectangle.html">Rectangle</a></span> {
         <span class="name">id</span>: <span class="name">coloredRect</span>
         <span class="name">width</span>: <span class="number">100</span>
         <span class="name">height</span>: <span class="number">100</span>
         <span class="name">anchors</span>.centerIn: <span class="name">parent</span>

         <span class="name">color</span>: <span class="string">&quot;red&quot;</span>
         Behavior on <span class="name">color</span> {
             <span class="type"><a href="qml-coloranimation.html">ColorAnimation</a></span> {}
         }

         <span class="type"><a href="qml-mousearea.html">MouseArea</a></span> {
             <span class="name">id</span>: <span class="name">mouser</span>
             <span class="name">anchors</span>.fill: <span class="name">parent</span>
             <span class="name">hoverEnabled</span>: <span class="number">true</span>
         }

         <span class="name">states</span>: <span class="name">State</span> {
             <span class="name">name</span>: <span class="string">&quot;GreenState&quot;</span>
             <span class="name">when</span>: <span class="name">mouser</span>.<span class="name">containsMouse</span>

             <span class="type"><a href="qml-propertychanges.html">PropertyChanges</a></span> {
                 <span class="name">target</span>: <span class="name">coloredRect</span>
                 <span class="name">color</span>: <span class="string">&quot;green&quot;</span>
             }
         }
     }
 }</pre>
<p>Testing 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 problem 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 <a href="qml-mousearea.html">MouseArea</a>, which is easily interrupted.</p>
<p>To state the problem more formally, using States and Behaviors together can cause unexpected behavior when:</p>
<ul>
<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</li>
<li>this Behavior can be interrupted to (re-)enter an explicitly defined state.</li>
</ul>
<p>The problem occurs because of the way the base state is defined for QML: as the &quot;snapshot&quot; 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 to red, and interrupt the animation to return to &quot;GreenState&quot;, the base state will include the color in its intermediate, mid-animation form.</p>
<p>While 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.</p>
<p>1. Use a transition to animate the change, rather than a Behavior.</p>
<pre class="qml"> import QtQuick 1.0

 <span class="type"><a href="qml-rectangle.html">Rectangle</a></span> {
     <span class="name">width</span>: <span class="number">400</span>
     <span class="name">height</span>: <span class="number">400</span>

     <span class="type"><a href="qml-rectangle.html">Rectangle</a></span> {
         <span class="name">id</span>: <span class="name">coloredRect</span>
         <span class="name">width</span>: <span class="number">100</span>
         <span class="name">height</span>: <span class="number">100</span>
         <span class="name">anchors</span>.centerIn: <span class="name">parent</span>

         <span class="name">color</span>: <span class="string">&quot;red&quot;</span>

         <span class="type"><a href="qml-mousearea.html">MouseArea</a></span> {
             <span class="name">id</span>: <span class="name">mouser</span>
             <span class="name">anchors</span>.fill: <span class="name">parent</span>
             <span class="name">hoverEnabled</span>: <span class="number">true</span>
         }

         <span class="name">states</span>: <span class="name">State</span> {
             <span class="name">name</span>: <span class="string">&quot;GreenState&quot;</span>
             <span class="name">when</span>: <span class="name">mouser</span>.<span class="name">containsMouse</span>

             <span class="type"><a href="qml-propertychanges.html">PropertyChanges</a></span> {
                 <span class="name">target</span>: <span class="name">coloredRect</span>
                 <span class="name">color</span>: <span class="string">&quot;green&quot;</span>
             }
         }

         <span class="name">transitions</span>: <span class="name">Transition</span> {
             <span class="type"><a href="qml-coloranimation.html">ColorAnimation</a></span> {}
         }
     }
 }</pre>
<p>2. Use a conditional binding to change the property value, rather than a state</p>
<pre class="qml"> import QtQuick 1.0

 <span class="type"><a href="qml-rectangle.html">Rectangle</a></span> {
     <span class="name">width</span>: <span class="number">400</span>
     <span class="name">height</span>: <span class="number">400</span>

     <span class="type"><a href="qml-rectangle.html">Rectangle</a></span> {
         <span class="name">id</span>: <span class="name">coloredRect</span>
         <span class="name">width</span>: <span class="number">100</span>
         <span class="name">height</span>: <span class="number">100</span>
         <span class="name">anchors</span>.centerIn: <span class="name">parent</span>

         <span class="name">color</span>: <span class="name">mouser</span>.<span class="name">containsMouse</span> ? <span class="string">&quot;green&quot;</span> : <span class="string">&quot;red&quot;</span>
         Behavior on <span class="name">color</span> {
             <span class="type"><a href="qml-coloranimation.html">ColorAnimation</a></span> {}
         }

         <span class="type"><a href="qml-mousearea.html">MouseArea</a></span> {
             <span class="name">id</span>: <span class="name">mouser</span>
             <span class="name">anchors</span>.fill: <span class="name">parent</span>
             <span class="name">hoverEnabled</span>: <span class="number">true</span>
         }
     }
 }</pre>
<p>3. Use only explicitly defined states, rather than an implicit base state</p>
<pre class="qml"> import QtQuick 1.0

 <span class="type"><a href="qml-rectangle.html">Rectangle</a></span> {
     <span class="name">width</span>: <span class="number">400</span>
     <span class="name">height</span>: <span class="number">400</span>

     <span class="type"><a href="qml-rectangle.html">Rectangle</a></span> {
         <span class="name">id</span>: <span class="name">coloredRect</span>
         <span class="name">width</span>: <span class="number">100</span>
         <span class="name">height</span>: <span class="number">100</span>
         <span class="name">anchors</span>.centerIn: <span class="name">parent</span>

         Behavior on <span class="name">color</span> {
             <span class="type"><a href="qml-coloranimation.html">ColorAnimation</a></span> {}
         }

         <span class="type"><a href="qml-mousearea.html">MouseArea</a></span> {
             <span class="name">id</span>: <span class="name">mouser</span>
             <span class="name">anchors</span>.fill: <span class="name">parent</span>
             <span class="name">hoverEnabled</span>: <span class="number">true</span>
         }

         <span class="name">states</span>: [
         <span class="type"><a href="qml-state.html">State</a></span> {
             <span class="name">name</span>: <span class="string">&quot;GreenState&quot;</span>
             <span class="name">when</span>: <span class="name">mouser</span>.<span class="name">containsMouse</span>

             <span class="type"><a href="qml-propertychanges.html">PropertyChanges</a></span> {
                 <span class="name">target</span>: <span class="name">coloredRect</span>
                 <span class="name">color</span>: <span class="string">&quot;green&quot;</span>
             }
         },
         <span class="type"><a href="qml-state.html">State</a></span> {
             <span class="name">name</span>: <span class="string">&quot;RedState&quot;</span>
             <span class="name">when</span>: !<span class="name">mouser</span>.<span class="name">containsMouse</span>

             <span class="type"><a href="qml-propertychanges.html">PropertyChanges</a></span> {
                 <span class="name">target</span>: <span class="name">coloredRect</span>
                 <span class="name">color</span>: <span class="string">&quot;red&quot;</span>
             }
         }]
     }
 }</pre>
</div>
<!-- @@@qml-behaviors-and-states.html -->
  <div class="ft">
    <span></span>
  </div>
</div> 
<div class="footer">
    <p>
      <acronym title="Copyright">&copy;</acronym> 2012 Nokia Corporation and/or its
      subsidiaries. Documentation contributions included herein are the copyrights of
      their respective owners.</p>
    <br />
    <p>
      The documentation provided herein is licensed under the terms of the
      <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation
      License version 1.3</a> as published by the Free Software Foundation.</p>
    <p>
      Documentation sources may be obtained from <a href="http://www.qt-project.org">
      www.qt-project.org</a>.</p>
    <br />
    <p>
      Nokia, Qt and their respective logos are trademarks of Nokia Corporation 
      in Finland and/or other countries worldwide. All other trademarks are property
      of their respective owners. <a title="Privacy Policy"
      href="http://en.gitorious.org/privacy_policy/">Privacy Policy</a></p>
</div>
</body>
</html>