<?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" /> <!-- qdeclarativestates.qdoc --> <title>Qt 4.8: QML States</title> <link rel="stylesheet" type="text/css" href="style/style.css" /> <script src="scripts/jquery.js" type="text/javascript"></script> <script src="scripts/functions.js" type="text/javascript"></script> <link rel="stylesheet" type="text/css" href="style/superfish.css" /> <link rel="stylesheet" type="text/css" href="style/narrow.css" /> <!--[if IE]> <meta name="MSSmartTagsPreventParsing" content="true"> <meta http-equiv="imagetoolbar" content="no"> <![endif]--> <!--[if lt IE 7]> <link rel="stylesheet" type="text/css" href="style/style_ie6.css"> <![endif]--> <!--[if IE 7]> <link rel="stylesheet" type="text/css" href="style/style_ie7.css"> <![endif]--> <!--[if IE 8]> <link rel="stylesheet" type="text/css" href="style/style_ie8.css"> <![endif]--> <script src="scripts/superfish.js" type="text/javascript"></script> <script src="scripts/narrow.js" type="text/javascript"></script> </head> <body class="" onload="CheckEmptyAndLoadList();"> <div class="header" id="qtdocheader"> <div class="content"> <div id="nav-logo"> <a href="index.html">Home</a></div> <a href="index.html" class="qtref"><span>Qt Reference Documentation</span></a> <div id="narrowsearch"></div> <div id="nav-topright"> <ul> <li class="nav-topright-home"><a href="http://qt.digia.com/">Qt HOME</a></li> <li class="nav-topright-dev"><a href="http://qt-project.org/">DEV</a></li> <li class="nav-topright-doc nav-topright-doc-active"><a href="http://qt-project.org/doc/"> DOC</a></li> <li class="nav-topright-blog"><a href="http://blog.qt.digia.com/">BLOG</a></li> </ul> </div> <div id="shortCut"> <ul> <li class="shortCut-topleft-inactive"><span><a href="index.html">Qt 4.8</a></span></li> <li class="shortCut-topleft-active"><a href="http://qt-project.org/doc/">ALL VERSIONS </a></li> </ul> </div> <ul class="sf-menu" id="narrowmenu"> <li><a href="#">API Lookup</a> <ul> <li><a href="classes.html">Class index</a></li> <li><a href="functions.html">Function index</a></li> <li><a href="modules.html">Modules</a></li> <li><a href="namespaces.html">Namespaces</a></li> <li><a href="qtglobal.html">Global Declarations</a></li> <li><a href="qdeclarativeelements.html">QML elements</a></li> </ul> </li> <li><a href="#">Qt Topics</a> <ul> <li><a href="qt-basic-concepts.html">Programming with Qt</a></li> <li><a href="qtquick.html">Device UIs & Qt Quick</a></li> <li><a href="qt-gui-concepts.html">UI Design with Qt</a></li> <li><a href="supported-platforms.html">Supported Platforms</a></li> <li><a href="technology-apis.html">Qt and Key Technologies</a></li> <li><a href="best-practices.html">How-To's and Best Practices</a></li> </ul> </li> <li><a href="#">Examples</a> <ul> <li><a href="all-examples.html">Examples</a></li> <li><a href="tutorials.html">Tutorials</a></li> <li><a href="demos.html">Demos</a></li> <li><a href="qdeclarativeexamples.html">QML Examples</a></li> </ul> </li> </ul> </div> </div> <div class="wrapper"> <div class="hd"> <span></span> </div> <div class="bd group"> <div class="sidebar"> <div class="searchlabel"> Search index:</div> <div class="search" id="sidebarsearch"> <form id="qtdocsearch" action="" onsubmit="return false;"> <fieldset> <input type="text" name="searchstring" id="pageType" value="" /> <div id="resultdialog"> <a href="#" id="resultclose">Close</a> <p id="resultlinks" class="all"><a href="#" id="showallresults">All</a> | <a href="#" id="showapiresults">API</a> | <a href="#" id="showarticleresults">Articles</a> | <a href="#" id="showexampleresults">Examples</a></p> <p id="searchcount" class="all"><span id="resultcount"></span><span id="apicount"></span><span id="articlecount"></span><span id="examplecount"></span> results:</p> <ul id="resultlist" class="all"> </ul> </div> </fieldset> </form> </div> <div class="box first bottombar" id="lookup"> <h2 title="API Lookup"><span></span> API Lookup</h2> <div id="list001" class="list"> <ul id="ul001" > <li class="defaultLink"><a href="classes.html">Class index</a></li> <li class="defaultLink"><a href="functions.html">Function index</a></li> <li class="defaultLink"><a href="modules.html">Modules</a></li> <li class="defaultLink"><a href="namespaces.html">Namespaces</a></li> <li class="defaultLink"><a href="qtglobal.html">Global Declarations</a></li> <li class="defaultLink"><a href="qdeclarativeelements.html">QML elements</a></li> </ul> </div> </div> <div class="box bottombar" id="topics"> <h2 title="Qt Topics"><span></span> Qt Topics</h2> <div id="list002" class="list"> <ul id="ul002" > <li class="defaultLink"><a href="qt-basic-concepts.html">Programming with Qt</a></li> <li class="defaultLink"><a href="qtquick.html">Device UIs & Qt Quick</a></li> <li class="defaultLink"><a href="qt-gui-concepts.html">UI Design with Qt</a></li> <li class="defaultLink"><a href="supported-platforms.html">Supported Platforms</a></li> <li class="defaultLink"><a href="technology-apis.html">Qt and Key Technologies</a></li> <li class="defaultLink"><a href="best-practices.html">How-To's and Best Practices</a></li> </ul> </div> </div> <div class="box" id="examples"> <h2 title="Examples"><span></span> Examples</h2> <div id="list003" class="list"> <ul id="ul003"> <li class="defaultLink"><a href="all-examples.html">Examples</a></li> <li class="defaultLink"><a href="tutorials.html">Tutorials</a></li> <li class="defaultLink"><a href="demos.html">Demos</a></li> <li class="defaultLink"><a href="qdeclarativeexamples.html">QML Examples</a></li> </ul> </div> </div> </div> <div class="wrap"> <div class="toolbar"> <div class="breadcrumb toolblock"> <ul> <li class="first"><a href="index.html">Home</a></li> <!-- Breadcrumbs go here --> <li>QML States</li> </ul> </div> <div class="toolbuttons toolblock"> <ul> <li id="smallA" class="t_button">A</li> <li id="medA" class="t_button active">A</li> <li id="bigA" class="t_button">A</li> <li id="print" class="t_button"><a href="javascript:this.print();"> <span>Print</span></a></li> </ul> </div> </div> <div class="content mainContent"> <link rel="prev" href="qmlreusablecomponents.html" /> <link rel="next" href="qdeclarativeanimation.html" /> <p class="naviNextPrevious headerNavi"> <a class="prevPage" href="qmlreusablecomponents.html">Importing Reusable Components</a> <a class="nextPage" href="qdeclarativeanimation.html">Animation and Transitions</a> </p><p/> <div class="toc"> <h3><a name="toc">Contents</a></h3> <ul> <li class="level1"><a href="#states-elements">States Elements</a></li> <li class="level1"><a href="#creating-states">Creating States</a></li> <li class="level1"><a href="#the-default-state">The Default State</a></li> <li class="level1"><a href="#the-property">The <tt>when</tt> Property</a></li> <li class="level1"><a href="#animating-state-changes">Animating State Changes</a></li> <li class="level1"><a href="#state-fast-forwarding">State Fast Forwarding</a></li> </ul> </div> <h1 class="title">QML States</h1> <span class="subtitle"></span> <!-- $$$qdeclarativestates.html-description --> <div class="descr"> <a name="details"></a> <a name="qmlstates"></a><a name="states-elements"></a> <h2>States Elements</h2> <ul> <li><a href="qml-state.html">State</a></li> <li><a href="qml-propertychanges.html">PropertyChanges</a></li> <li><a href="qml-stategroup.html">StateGroup</a></li> <li><a href="qml-statechangescript.html">StateChangeScript</a></li> <li><a href="qml-parentchange.html">ParentChange</a></li> <li><a href="qml-anchorchanges.html">AnchorChanges</a></li> </ul> <p>Many user interface designs are <i>state</i> driven; interfaces have configurations that differ depending on the current state. For example, a traffic signal will configure its flags or lights depending on its state. While in the signal's <tt>stop</tt> state, a red light will turn on while the yellow and the green lights will turn off. In the <tt>caution</tt> state, the yellow light is on while the other lights are turned off.</p> <p>In QML, <i>states</i> are a set of property configurations defined in a <a href="qml-state.html">State</a> element. Different configurations could, for example:</p> <ul> <li>Show some UI elements and hide others</li> <li>Present different available actions to the user</li> <li>Start, stop, or pause animations</li> <li>Execute some script required in the new state</li> <li>Change a property value for a particular item</li> <li>Show a different view or screen</li> </ul> <p>All <a href="qml-item.html">Item</a>-based objects have a <tt>state</tt> property, and can specify additional states by adding new <tt>State</tt> objects to the item's <a href="qml-item.html#states-prop">states</a> property. Each state within a component has a unique <tt>name</tt>, an empty string being the default. To change the current state of an item, set the <a href="qml-item.html#state-prop">state</a> property to the name of the state.</p> <p>Non-Item objects may use states through the <a href="qml-stategroup.html">StateGroup</a> element.</p> <a name="creating-states"></a> <h2>Creating States</h2> <p>To create a state, add a <a href="qml-state.html">State</a> object to the item's <a href="qml-item.html#states-prop">states</a> property, which holds a list of states for that item.</p> <p>A warning <tt>signal</tt> component may have two states, the <tt>NORMAL</tt> and the <tt>CRITICAL</tt> state. Suppose that in the <tt>NORMAL</tt> state, the <tt>color</tt> of the signal should be <tt>green</tt> and the warning <tt>flag</tt> is down. Meanwhile, in the <tt>CRITICAL</tt> state, the <tt>color</tt> should be <tt>red</tt> and the flag is <tt>up</tt>. We may model the states using the <tt>State</tt> element and the color and flag configurations with the <tt>PropertyChanges</tt> element.</p> <pre class="qml"> <span class="type"><a href="qml-rectangle.html">Rectangle</a></span> { <span class="name">id</span>: <span class="name">signal</span> <span class="name">width</span>: <span class="number">200</span>; <span class="name">height</span>: <span class="number">200</span> <span class="name">state</span>: <span class="string">"NORMAL"</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">"NORMAL"</span> <span class="type"><a href="qml-propertychanges.html">PropertyChanges</a></span> { <span class="name">target</span>: <span class="name">signal</span>; <span class="name">color</span>: <span class="string">"green"</span>} <span class="type"><a href="qml-propertychanges.html">PropertyChanges</a></span> { <span class="name">target</span>: <span class="name">flag</span>; <span class="name">state</span>: <span class="string">"FLAG_DOWN"</span>} }, <span class="type"><a href="qml-state.html">State</a></span> { <span class="name">name</span>: <span class="string">"CRITICAL"</span> <span class="type"><a href="qml-propertychanges.html">PropertyChanges</a></span> { <span class="name">target</span>: <span class="name">signal</span>; <span class="name">color</span>: <span class="string">"red"</span>} <span class="type"><a href="qml-propertychanges.html">PropertyChanges</a></span> { <span class="name">target</span>: <span class="name">flag</span>; <span class="name">state</span>: <span class="string">"FLAG_UP"</span>} } ] }</pre> <p>The <a href="qml-propertychanges.html">PropertyChanges</a> element will change the values of object properties. Objects are referenced through their <a href="propertybinding.html#qml-id-property">id</a>. Objects outside the component are also referenced using the <tt>id</tt> property, exemplified by the property change to the external <tt>flag</tt> object.</p> <p>Further, the state may change by assigning the <tt>state</tt> property with the appropriate signal state. A state switch could be in a <a href="qml-mousearea.html">MouseArea</a> element, assigning a different state whenever the signal receives a mouse click.</p> <pre class="qml"> <span class="type"><a href="qml-rectangle.html">Rectangle</a></span> { <span class="name">id</span>: <span class="name">signalswitch</span> <span class="name">width</span>: <span class="number">75</span>; <span class="name">height</span>: <span class="number">75</span> <span class="name">color</span>: <span class="string">"blue"</span> <span class="type"><a href="qml-mousearea.html">MouseArea</a></span> { <span class="name">anchors</span>.fill: <span class="name">parent</span> <span class="name">onClicked</span>: { <span class="keyword">if</span> (<span class="name">signal</span>.<span class="name">state</span> <span class="operator">==</span> <span class="string">"NORMAL"</span>) <span class="name">signal</span>.<span class="name">state</span> <span class="operator">=</span> <span class="string">"CRITICAL"</span> else <span class="name">signal</span>.<span class="name">state</span> <span class="operator">=</span> <span class="string">"NORMAL"</span> } } }</pre> <p>The State element is not limited to performing modifications on property values. It can also:</p> <ul> <li>Run some script using <a href="qml-statechangescript.html">StateChangeScript</a></li> <li>Override an existing signal handler for an object using <a href="qml-propertychanges.html">PropertyChanges</a></li> <li>Re-parent an <a href="qml-item.html">Item</a> using <a href="qml-parentchange.html">ParentChange</a></li> <li>Modify anchor values using <a href="qml-anchorchanges.html">AnchorChanges</a></li> </ul> <a name="the-default-state"></a> <h2>The Default State</h2> <p>Every <a href="qml-item.html">Item</a> based component has a <tt>state</tt> property and a <i>default state</i>. The default state is the empty string (<tt>""</tt>) and contains all of an item's initial property values. The default state is useful for managing property values before state changes. Setting the <tt>state</tt> property to an empty string will load the default state.</p> <a name="the-property"></a> <h2>The <tt>when</tt> Property</h2> <p>For convenience, the <a href="qml-state.html">State</a> element has a <tt>when</tt> property that can bind to expressions to change the state whenever the bound expression evaluates to <tt>true</tt>. The <tt>when</tt> property will revert the state back to the <a href="#the-default-state">default state</a> when the expression evaluates to false.</p> <pre class="qml"> <span class="type"><a href="qml-rectangle.html">Rectangle</a></span> { <span class="name">id</span>: <span class="name">bell</span> <span class="name">width</span>: <span class="number">75</span>; <span class="name">height</span>: <span class="number">75</span> <span class="name">color</span>: <span class="string">"yellow"</span> <span class="name">states</span>: <span class="name">State</span> { <span class="name">name</span>: <span class="string">"RINGING"</span> <span class="name">when</span>: (<span class="name">signal</span>.<span class="name">state</span> <span class="operator">==</span> <span class="string">"CRITICAL"</span>) <span class="type"><a href="qml-propertychanges.html">PropertyChanges</a></span> {<span class="name">target</span>: <span class="name">speaker</span>; <span class="name">play</span>: <span class="string">"RING!"</span>} } }</pre> <p>The <tt>bell</tt> component will change to the <tt>RINGING</tt> state whenever the <tt>signal.state</tt> is <tt>CRITICAL</tt>.</p> <a name="animating-state-changes"></a> <h2>Animating State Changes</h2> <p>State changes induce abrupt value changes. The <a href="qml-transition.html">Transition</a> element allow smoother changes during state changes. In transitions, animations and interpolation behaviors are definable. The <a href="qdeclarativeanimation.html">Animation and Transitions</a> article has more information about creating state animations.</p> <p>The <a href="declarative-animation-states.html">States and Transitions example</a> demonstrates how to declare a basic set of states and apply animated transitions between them.</p> <p><a href="qml-behaviors-and-states.html">Using QML Behaviors with States</a> explains a common problem when using Behaviors to animate state changes.</p> <a name="state-fast-forwarding"></a> <h2>State Fast Forwarding</h2> <p>In order for Transition to correctly animate state changes, it is sometimes necessary for the engine to fast forward and rewind a state (that is, internally set and unset the state) before it is finally applied. The process is as follows:</p> <ol class="1"> <li>The state is fast forwarded to determine the complete set of end values.</li> <li>The state is rewound.</li> <li>The state is fully applied, with transitions.</li> </ol> <p>In some cases this may cause unintended behavior. For example, a state that changes a view's</p> <p>model or a Loader's</p> <p>sourceComponent will set these properties multiple times (to apply, rewind, and then reapply), which can be relatively expensive.</p> <p>State fast forwarding should be considered an implementation detail, and may change in later versions.</p> </div> <!-- @@@qdeclarativestates.html --> <p class="naviNextPrevious footerNavi"> <a class="prevPage" href="qmlreusablecomponents.html">Importing Reusable Components</a> <a class="nextPage" href="qdeclarativeanimation.html">Animation and Transitions</a> </p> </div> </div> </div> <div class="ft"> <span></span> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2013 Digia Plc 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> Digia, Qt and their respective logos are trademarks of Digia Plc 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> <script src="scripts/functions.js" type="text/javascript"></script> </body> </html>