<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- qquickstate.cpp --> <title>State QML Type | Qt Quick 5.9</title> <link rel="stylesheet" type="text/css" href="style/offline-simple.css" /> <script type="text/javascript"> document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css"); // loading style sheet breaks anchors that were jumped to before // so force jumping to anchor again setTimeout(function() { var anchor = location.hash; // need to jump to different anchor first (e.g. none) location.hash = "#"; setTimeout(function() { location.hash = anchor; }, 0); }, 0); </script> </head> <body> <div class="header" id="qtdocheader"> <div class="main"> <div class="main-rounded"> <div class="navigationbar"> <table><tr> <td >Qt 5.9</td><td ><a href="qtquick-index.html">Qt Quick</a></td><td ><a href="qtquick-qmlmodule.html">QML Types</a></td><td >State QML Type</td></tr></table><table class="buildversion"><tr> <td id="buildversion" width="100%" align="right">Qt 5.9.4 Reference Documentation</td> </tr></table> </div> </div> <div class="content"> <div class="line"> <div class="content mainContent"> <div class="sidebar"> <div class="toc"> <h3><a name="toc">Contents</a></h3> <ul> <li class="level1"><a href="#properties">Properties</a></li> <li class="level1"><a href="#details">Detailed Description</a></li> </ul> </div> <div class="sidebar-content" id="sidebar-content"></div></div> <h1 class="title">State QML Type</h1> <span class="subtitle"></span> <!-- $$$State-brief --> <p>Defines configurations of objects and properties <a href="#details">More...</a></p> <!-- @@@State --> <div class="table"><table class="alignedsummary"> <tr><td class="memItemLeft rightAlign topAlign"> Import Statement:</td><td class="memItemRight bottomAlign"> import QtQuick 2.7</td></tr></table></div><ul> <li><a href="qml-qtquick-state-members.html">List of all members, including inherited members</a></li> </ul> <a name="properties"></a> <h2 id="properties">Properties</h2> <ul> <li class="fn"><b><b><a href="qml-qtquick-state.html#changes-prop">changes</a></b></b> : list<Change></li> <li class="fn"><b><b><a href="qml-qtquick-state.html#extend-prop">extend</a></b></b> : string</li> <li class="fn"><b><b><a href="qml-qtquick-state.html#name-prop">name</a></b></b> : string</li> <li class="fn"><b><b><a href="qml-qtquick-state.html#when-prop">when</a></b></b> : bool</li> </ul> <!-- $$$State-description --> <a name="details"></a> <h2 id="details">Detailed Description</h2> <p>A <i>state</i> is a set of batched changes from the default configuration.</p> <p>All items have a default state that defines the default configuration of objects and property values. New states can be defined by adding State items to the <a href="qml-qtquick-item.html#states-prop">states</a> property to allow items to switch between different configurations. These configurations can, for example, be used to apply different sets of property values or execute different scripts.</p> <p>The following example displays a single <a href="qml-qtquick-rectangle.html">Rectangle</a>. In the default state, the rectangle is colored black. In the "clicked" state, a <a href="qml-qtquick-propertychanges.html">PropertyChanges</a> object changes the rectangle's color to red. Clicking within the <a href="qml-qtquick-mousearea.html">MouseArea</a> toggles the rectangle's state between the default state and the "clicked" state, thus toggling the color of the rectangle between black and red.</p> <pre class="qml"> import QtQuick 2.0 <span class="type"><a href="qml-qtquick-rectangle.html">Rectangle</a></span> { <span class="name">id</span>: <span class="name">myRect</span> <span class="name">width</span>: <span class="number">100</span>; <span class="name">height</span>: <span class="number">100</span> <span class="name">color</span>: <span class="string">"black"</span> <span class="type"><a href="qml-qtquick-mousearea.html">MouseArea</a></span> { <span class="name">id</span>: <span class="name">mouseArea</span> <span class="name">anchors</span>.fill: <span class="name">parent</span> <span class="name">onClicked</span>: <span class="name">myRect</span>.<span class="name">state</span> <span class="operator">==</span> <span class="string">'clicked'</span> ? <span class="name">myRect</span>.<span class="name">state</span> <span class="operator">=</span> <span class="string">""</span> : <span class="name">myRect</span>.<span class="name">state</span> <span class="operator">=</span> <span class="string">'clicked'</span>; } <span class="name">states</span>: [ <span class="type"><a href="qml-qtquick-state.html">State</a></span> { <span class="name">name</span>: <span class="string">"clicked"</span> <span class="type"><a href="qml-qtquick-propertychanges.html">PropertyChanges</a></span> { <span class="name">target</span>: <span class="name">myRect</span>; <span class="name">color</span>: <span class="string">"red"</span> } } ] } </pre> <p>Notice the default state is referred to using an empty string ("").</p> <p>States are commonly used together with <a href="qtquick-statesanimations-animations.html">Transitions</a> to provide animations when state changes occur.</p> <p><b>Note: </b>Setting the state of an object from within another state of the same object is not allowed.</p><p><b>See also </b><a href="qtquick-animation-example.html#states">States example</a>, <a href="qtquick-statesanimations-states.html">Qt Quick States</a>, <a href="qtquick-statesanimations-animations.html">Transitions</a>, and Qt QML.</p> <!-- @@@State --> <h2>Property Documentation</h2> <!-- $$$changes --> <div class="qmlitem"><div class="qmlproto"> <div class="table"><table class="qmlname"> <tr valign="top" class="odd" id="changes-prop"> <td class="tblQmlPropNode"><p> <a name="changes-prop"></a><span class="qmldefault">[default] </span><span class="name">changes</span> : <span class="type">list</span><<span class="type">Change</span>></p></td></tr> </table></div> </div><div class="qmldoc"><p>This property holds the changes to apply for this state</p> <p>By default these changes are applied against the default state. If the state extends another state, then the changes are applied against the state being extended.</p> </div></div><!-- @@@changes --> <br/> <!-- $$$extend --> <div class="qmlitem"><div class="qmlproto"> <div class="table"><table class="qmlname"> <tr valign="top" class="odd" id="extend-prop"> <td class="tblQmlPropNode"><p> <a name="extend-prop"></a><span class="name">extend</span> : <span class="type">string</span></p></td></tr> </table></div> </div><div class="qmldoc"><p>This property holds the state that this state extends.</p> <p>When a state extends another state, it inherits all the changes of that state.</p> <p>The state being extended is treated as the base state in regards to the changes specified by the extending state.</p> </div></div><!-- @@@extend --> <br/> <!-- $$$name --> <div class="qmlitem"><div class="qmlproto"> <div class="table"><table class="qmlname"> <tr valign="top" class="odd" id="name-prop"> <td class="tblQmlPropNode"><p> <a name="name-prop"></a><span class="name">name</span> : <span class="type">string</span></p></td></tr> </table></div> </div><div class="qmldoc"><p>This property holds the name of the state.</p> <p>Each state should have a unique name within its item.</p> </div></div><!-- @@@name --> <br/> <!-- $$$when --> <div class="qmlitem"><div class="qmlproto"> <div class="table"><table class="qmlname"> <tr valign="top" class="odd" id="when-prop"> <td class="tblQmlPropNode"><p> <a name="when-prop"></a><span class="name">when</span> : <span class="type">bool</span></p></td></tr> </table></div> </div><div class="qmldoc"><p>This property holds when the state should be applied.</p> <p>This should be set to an expression that evaluates to <code>true</code> when you want the state to be applied. For example, the following <a href="qml-qtquick-rectangle.html">Rectangle</a> changes in and out of the "hidden" state when the <a href="qml-qtquick-mousearea.html">MouseArea</a> is pressed:</p> <pre class="qml"> <span class="type"><a href="qml-qtquick-rectangle.html">Rectangle</a></span> { <span class="name">id</span>: <span class="name">myRect</span> <span class="name">width</span>: <span class="number">100</span>; <span class="name">height</span>: <span class="number">100</span> <span class="name">color</span>: <span class="string">"red"</span> <span class="type"><a href="qml-qtquick-mousearea.html">MouseArea</a></span> { <span class="name">id</span>: <span class="name">mouseArea</span>; <span class="name">anchors</span>.fill: <span class="name">parent</span> } <span class="name">states</span>: <span class="name">State</span> { <span class="name">name</span>: <span class="string">"hidden"</span>; <span class="name">when</span>: <span class="name">mouseArea</span>.<span class="name">pressed</span> <span class="type"><a href="qml-qtquick-propertychanges.html">PropertyChanges</a></span> { <span class="name">target</span>: <span class="name">myRect</span>; <span class="name">opacity</span>: <span class="number">0</span> } } } </pre> <p>If multiple states in a group have <code>when</code> clauses that evaluate to <code>true</code> at the same time, the first matching state will be applied. For example, in the following snippet <code>state1</code> will always be selected rather than <code>state2</code> when sharedCondition becomes <code>true</code>.</p> <pre class="qml"> <span class="type"><a href="qml-qtquick-item.html">Item</a></span> { <span class="name">states</span>: [ <span class="type"><a href="qml-qtquick-state.html">State</a></span> { <span class="name">name</span>: <span class="string">"state1"</span>; <span class="name">when</span>: <span class="name">sharedCondition</span> }, <span class="type"><a href="qml-qtquick-state.html">State</a></span> { <span class="name">name</span>: <span class="string">"state2"</span>; <span class="name">when</span>: <span class="name">sharedCondition</span> } ] <span class="comment">// ...</span> } </pre> </div></div><!-- @@@when --> <br/> </div> </div> </div> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2017 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners.<br> 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.<br> Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners. </p> </div> </body> </html>