<?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" /> <!-- example-slideswitch.qdoc --> <title>Qt 4.8: QML Example - Toggle Switch</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 Example - Toggle Switch</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"> <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> <h1 class="title">QML Example - Toggle Switch</h1> <span class="subtitle"></span> <!-- $$$qdeclarativeexampletoggleswitch.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 <tt>$QTDIR/examples/declarative/ui-components/slideswitch</tt> directory.</p> <p>The elements that compose the switch are:</p> <ul> <li>a <tt>on</tt> 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 (a <i>on</i> state and a <i>off</i> state),</li> <li>two functions or slots to react to the user interation (<tt>toggle()</tt> and <tt>dorelease()</tt>),</li> <li>and a transition that describe how to go from one state to the other.</li> </ul> <a name="switch-qml"></a> <h2>Switch.qml</h2> <pre class="qml"></pre> <a name="walkthrough"></a> <h2>Walkthrough</h2> <a name="interface"></a> <h3>Interface</h3> <pre class="qml"></pre> <p>This property is the interface of the switch. By default, the switch is off and this property is <tt>false</tt>. It can be used to activate/disactivate the switch or to query its current state.</p> <p>In this example:</p> <pre class="qml"> <span class="type"><a href="qml-item.html">Item</a></span> { <span class="type">Switch</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-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"></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-mousearea.html">MouseArea</a> as a child item of the image. A <tt>MouseArea</tt> has a <tt>onClicked</tt> property that is triggered when the item is clicked. For the moment we will just call a <tt>toggle()</tt> function. We will see what this function does in a moment.</p> <pre class="qml"></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 <tt>drag</tt> property of the <tt>MouseArea</tt> is for. We also want to toggle the switch if the knob is released between state. We handle this case in the <tt>dorelease()</tt> function that is called in the <tt>onReleased</tt> property.</p> <a name="states"></a> <h3>States</h3> <pre class="qml"></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 (<tt>x</tt> position is 78) and the <tt>on</tt> property is <tt>true</tt>.</li> <li>In the <i>off</i> state the knob is on the left (<tt>x</tt> position is 1) and the <tt>on</tt> property is <tt>false</tt>.</li> </ul> <p>For more information on states see <a href="qdeclarativestates.html#qmlstates">QML States</a>.</p> <a name="functions"></a> <h3>Functions</h3> <p>We add two JavaScript functions to our switch:</p> <pre class="qml"></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"></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 <tt>toggle()</tt> function otherwise we do nothing.</p> <p>For more information on scripts see <a href="qdeclarativejavascript.html">Integrating JavaScript</a>.</p> <a name="transition"></a> <h3>Transition</h3> <pre class="qml"></pre> <p>At this point, when the switch toggles between the two states the knob will instantly change its <tt>x</tt> position between 1 and 78. In order for the the knob to move smoothly we add a transition that will animate the <tt>x</tt> property with an easing curve for a duration of 200ms.</p> <p>For more information on transitions see <a href="qdeclarativeanimation.html">QML Animation and Transitions</a>.</p> <a name="usage"></a> <h2>Usage</h2> <p>The switch can be used in a QML file, like this:</p> <pre class="qml"></pre> </div> <!-- @@@qdeclarativeexampletoggleswitch.html --> </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>