<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- example-slideswitch.qdoc --> <title>Qt Quick Examples - Toggle Switch | Qt Quick 5.11</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 ><a href="../qtdoc/index.html">Qt 5.11</a></td><td ><a href="qtquick-index.html">Qt Quick</a></td><td >Qt Quick Examples - Toggle Switch</td></tr></table><table class="buildversion"><tr> <td id="buildversion" width="100%" align="right">Qt 5.11.2 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="#switch-qml">Switch.qml</a></li> <li class="level1"><a href="#walkthrough">Walkthrough</a></li> <li class="level2"><a href="#interface">Interface</a></li> <li class="level2"><a href="#images-and-user-interaction">Images and User Interaction</a></li> <li class="level2"><a href="#states">States</a></li> <li class="level2"><a href="#functions">Functions</a></li> <li class="level2"><a href="#transition">Transition</a></li> <li class="level1"><a href="#usage">Usage</a></li> </ul> </div> <div class="sidebar-content" id="sidebar-content"></div></div> <h1 class="title">Qt Quick Examples - Toggle Switch</h1> <span class="subtitle"></span> <!-- $$$qmlexampletoggleswitch.html-description --> <div class="descr"> <a name="details"></a> <p>This example shows how to create a reusable switch component in QML.</p> <p>The code for this example can be found in the <code>examples/quick/customitems/slideswitch</code> directory.</p> <p>The objects that compose the switch are:</p> <ul> <li>a <code>on</code> property (the interface to interact with the switch),</li> <li>two images (the background image and the knob),</li> <li>two mouse regions for user interation (on the background image and on the knob),</li> <li>two states (an <i>on</i> state and an <i>off</i> state),</li> <li>two functions or slots to react to the user interation (<code>toggle()</code> and <code>dorelease()</code>),</li> <li>and a transition that describe how to go from one state to the other.</li> </ul> <a name="switch-qml"></a> <h2 id="switch-qml">Switch.qml</h2> <pre class="qml"> import QtQuick 2.0 <span class="type"><a href="qml-qtquick-item.html">Item</a></span> { <span class="name">id</span>: <span class="name">toggleswitch</span> <span class="name">width</span>: <span class="name">background</span>.<span class="name">width</span>; <span class="name">height</span>: <span class="name">background</span>.<span class="name">height</span> property <span class="type"><a href="../qtqml/qml-bool.html">bool</a></span> <span class="name">on</span>: <span class="number">false</span> <span class="keyword">function</span> <span class="name">toggle</span>() { <span class="keyword">if</span> (<span class="name">toggleswitch</span>.<span class="name">state</span> <span class="operator">==</span> <span class="string">"on"</span>) <span class="name">toggleswitch</span>.<span class="name">state</span> <span class="operator">=</span> <span class="string">"off"</span>; <span class="keyword">else</span> <span class="name">toggleswitch</span>.<span class="name">state</span> <span class="operator">=</span> <span class="string">"on"</span>; } <span class="keyword">function</span> <span class="name">releaseSwitch</span>() { <span class="keyword">if</span> (<span class="name">knob</span>.<span class="name">x</span> <span class="operator">==</span> <span class="number">1</span>) { <span class="keyword">if</span> (<span class="name">toggleswitch</span>.<span class="name">state</span> <span class="operator">==</span> <span class="string">"off"</span>) <span class="keyword">return</span>; } <span class="keyword">if</span> (<span class="name">knob</span>.<span class="name">x</span> <span class="operator">==</span> <span class="number">78</span>) { <span class="keyword">if</span> (<span class="name">toggleswitch</span>.<span class="name">state</span> <span class="operator">==</span> <span class="string">"on"</span>) <span class="keyword">return</span>; } <span class="name">toggle</span>(); } <span class="type"><a href="qml-qtquick-image.html">Image</a></span> { <span class="name">id</span>: <span class="name">background</span> <span class="name">source</span>: <span class="string">"background.png"</span> <span class="type"><a href="qml-qtquick-mousearea.html">MouseArea</a></span> { <span class="name">anchors</span>.fill: <span class="name">parent</span>; <span class="name">onClicked</span>: <span class="name">toggle</span>() } } <span class="type"><a href="qml-qtquick-image.html">Image</a></span> { <span class="name">id</span>: <span class="name">knob</span> <span class="name">x</span>: <span class="number">1</span>; <span class="name">y</span>: <span class="number">2</span> <span class="name">source</span>: <span class="string">"knob.png"</span> <span class="type"><a href="qml-qtquick-mousearea.html">MouseArea</a></span> { <span class="name">anchors</span>.fill: <span class="name">parent</span> <span class="name">drag</span>.target: <span class="name">knob</span>; <span class="name">drag</span>.axis: <span class="name">Drag</span>.<span class="name">XAxis</span>; <span class="name">drag</span>.minimumX: <span class="number">1</span>; <span class="name">drag</span>.maximumX: <span class="number">78</span> <span class="name">onClicked</span>: <span class="name">toggle</span>() <span class="name">onReleased</span>: <span class="name">releaseSwitch</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">"on"</span> <span class="type"><a href="qml-qtquick-propertychanges.html">PropertyChanges</a></span> { <span class="name">target</span>: <span class="name">knob</span>; <span class="name">x</span>: <span class="number">78</span> } <span class="type"><a href="qml-qtquick-propertychanges.html">PropertyChanges</a></span> { <span class="name">target</span>: <span class="name">toggleswitch</span>; <span class="name">on</span>: <span class="number">true</span> } }, <span class="type"><a href="qml-qtquick-state.html">State</a></span> { <span class="name">name</span>: <span class="string">"off"</span> <span class="type"><a href="qml-qtquick-propertychanges.html">PropertyChanges</a></span> { <span class="name">target</span>: <span class="name">knob</span>; <span class="name">x</span>: <span class="number">1</span> } <span class="type"><a href="qml-qtquick-propertychanges.html">PropertyChanges</a></span> { <span class="name">target</span>: <span class="name">toggleswitch</span>; <span class="name">on</span>: <span class="number">false</span> } } ] <span class="name">transitions</span>: <span class="name">Transition</span> { <span class="type"><a href="qml-qtquick-numberanimation.html">NumberAnimation</a></span> { <span class="name">properties</span>: <span class="string">"x"</span>; <span class="name">easing</span>.type: <span class="name">Easing</span>.<span class="name">InOutQuad</span>; <span class="name">duration</span>: <span class="number">200</span> } } } </pre> <a name="walkthrough"></a> <h2 id="walkthrough">Walkthrough</h2> <a name="interface"></a> <h3 >Interface</h3> <pre class="qml"> property <span class="type"><a href="../qtqml/qml-bool.html">bool</a></span> <span class="name">on</span>: <span class="number">false</span> </pre> <p>This property is the interface of the switch. By default, the switch is off and this property is <code>false</code>. It can be used to activate/deactivate the switch or to query its current state.</p> <p>In this example:</p> <pre class="qml"> <span class="type"><a href="qml-qtquick-item.html">Item</a></span> { <span class="type"><a href="../qtquickcontrols/qml-qtquick-controls-switch.html">Switch</a></span> { <span class="name">id</span>: <span class="name">mySwitch</span> <span class="name">on</span>: <span class="number">true</span> } <span class="type"><a href="qml-qtquick-text.html">Text</a></span> { <span class="name">text</span>: <span class="string">"The switch is on"</span> <span class="name">visible</span>: <span class="name">mySwitch</span>.<span class="name">on</span> <span class="operator">==</span> <span class="number">true</span> } } </pre> <p>the text will only be visible when the switch is on.</p> <a name="images-and-user-interaction"></a> <h3 >Images and User Interaction</h3> <pre class="qml"> <span class="type"><a href="qml-qtquick-image.html">Image</a></span> { <span class="name">id</span>: <span class="name">background</span> <span class="name">source</span>: <span class="string">"background.png"</span> <span class="type"><a href="qml-qtquick-mousearea.html">MouseArea</a></span> { <span class="name">anchors</span>.fill: <span class="name">parent</span>; <span class="name">onClicked</span>: <span class="name">toggle</span>() } } </pre> <p>First, we create the background image of the switch. In order for the switch to toggle when the user clicks on the background, we add a <a href="qml-qtquick-mousearea.html">MouseArea</a> as a child item of the image. A <code>MouseArea</code> has a <code>onClicked</code> property that is triggered when the item is clicked. For the moment we will just call a <code>toggle()</code> function. We will see what this function does in a moment.</p> <pre class="qml"> <span class="type"><a href="qml-qtquick-image.html">Image</a></span> { <span class="name">id</span>: <span class="name">knob</span> <span class="name">x</span>: <span class="number">1</span>; <span class="name">y</span>: <span class="number">2</span> <span class="name">source</span>: <span class="string">"knob.png"</span> <span class="type"><a href="qml-qtquick-mousearea.html">MouseArea</a></span> { <span class="name">anchors</span>.fill: <span class="name">parent</span> <span class="name">drag</span>.target: <span class="name">knob</span>; <span class="name">drag</span>.axis: <span class="name">Drag</span>.<span class="name">XAxis</span>; <span class="name">drag</span>.minimumX: <span class="number">1</span>; <span class="name">drag</span>.maximumX: <span class="number">78</span> <span class="name">onClicked</span>: <span class="name">toggle</span>() <span class="name">onReleased</span>: <span class="name">releaseSwitch</span>() } } </pre> <p>Then, we place the image of the knob on top of the background. The interaction here is a little more complex. We want the knob to move with the finger when it is clicked. That is what the <code>drag</code> property of the <code>MouseArea</code> is for. We also want to toggle the switch if the knob is released between state. We handle this case in the <code>dorelease()</code> function that is called in the <code>onReleased</code> property.</p> <a name="states"></a> <h3 >States</h3> <pre class="qml"> <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">"on"</span> <span class="type"><a href="qml-qtquick-propertychanges.html">PropertyChanges</a></span> { <span class="name">target</span>: <span class="name">knob</span>; <span class="name">x</span>: <span class="number">78</span> } <span class="type"><a href="qml-qtquick-propertychanges.html">PropertyChanges</a></span> { <span class="name">target</span>: <span class="name">toggleswitch</span>; <span class="name">on</span>: <span class="number">true</span> } }, <span class="type"><a href="qml-qtquick-state.html">State</a></span> { <span class="name">name</span>: <span class="string">"off"</span> <span class="type"><a href="qml-qtquick-propertychanges.html">PropertyChanges</a></span> { <span class="name">target</span>: <span class="name">knob</span>; <span class="name">x</span>: <span class="number">1</span> } <span class="type"><a href="qml-qtquick-propertychanges.html">PropertyChanges</a></span> { <span class="name">target</span>: <span class="name">toggleswitch</span>; <span class="name">on</span>: <span class="number">false</span> } } ] </pre> <p>We define the two states of the switch:</p> <ul> <li>In the <i>on</i> state the knob is on the right (<code>x</code> position is 78) and the <code>on</code> property is <code>true</code>.</li> <li>In the <i>off</i> state the knob is on the left (<code>x</code> position is 1) and the <code>on</code> property is <code>false</code>.</li> </ul> <p>For more information on states see <a href="qtquick-statesanimations-states.html">Qt Quick States</a>.</p> <a name="functions"></a> <h3 >Functions</h3> <p>We add two JavaScript functions to our switch:</p> <pre class="qml"> <span class="keyword">function</span> <span class="name">toggle</span>() { <span class="keyword">if</span> (<span class="name">toggleswitch</span>.<span class="name">state</span> <span class="operator">==</span> <span class="string">"on"</span>) <span class="name">toggleswitch</span>.<span class="name">state</span> <span class="operator">=</span> <span class="string">"off"</span>; <span class="keyword">else</span> <span class="name">toggleswitch</span>.<span class="name">state</span> <span class="operator">=</span> <span class="string">"on"</span>; } </pre> <p>This first function is called when the background image or the knob are clicked. We simply want the switch to toggle between the two states (<i>on</i> and <i>off</i>).</p> <pre class="qml"> <span class="keyword">function</span> <span class="name">releaseSwitch</span>() { <span class="keyword">if</span> (<span class="name">knob</span>.<span class="name">x</span> <span class="operator">==</span> <span class="number">1</span>) { <span class="keyword">if</span> (<span class="name">toggleswitch</span>.<span class="name">state</span> <span class="operator">==</span> <span class="string">"off"</span>) <span class="keyword">return</span>; } <span class="keyword">if</span> (<span class="name">knob</span>.<span class="name">x</span> <span class="operator">==</span> <span class="number">78</span>) { <span class="keyword">if</span> (<span class="name">toggleswitch</span>.<span class="name">state</span> <span class="operator">==</span> <span class="string">"on"</span>) <span class="keyword">return</span>; } <span class="name">toggle</span>(); } </pre> <p>This second function is called when the knob is released and we want to make sure that the knob does not end up between states (neither <i>on</i> nor <i>off</i>). If it is the case call the <code>toggle()</code> function otherwise we do nothing.</p> <p>For more information on scripts see <a href="../qtqml/qtqml-javascript-expressions.html">JavaScript Expressions in QML Documents</a>.</p> <a name="transition"></a> <h3 >Transition</h3> <pre class="qml"> <span class="name">transitions</span>: <span class="name">Transition</span> { <span class="type"><a href="qml-qtquick-numberanimation.html">NumberAnimation</a></span> { <span class="name">properties</span>: <span class="string">"x"</span>; <span class="name">easing</span>.type: <span class="name">Easing</span>.<span class="name">InOutQuad</span>; <span class="name">duration</span>: <span class="number">200</span> } } </pre> <p>At this point, when the switch toggles between the two states the knob will instantly change its <code>x</code> position between 1 and 78. In order for the knob to move smoothly we add a transition that will animate the <code>x</code> property with an easing curve for a duration of 200ms.</p> <p>For more information on transitions see <a href="qtquick-statesanimations-animations.html">Animation and Transitions in Qt Quick</a>.</p> <a name="usage"></a> <h2 id="usage">Usage</h2> <p>The switch can be used in a QML file, like this:</p> <pre class="qml"> <span class="type"><a href="../qtquickcontrols/qml-qtquick-controls-switch.html">Switch</a></span> { <span class="name">anchors</span>.centerIn: <span class="name">parent</span>; <span class="name">on</span>: <span class="number">false</span> } </pre> </div> <!-- @@@qmlexampletoggleswitch.html --> </div> </div> </div> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2018 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>