<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.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" /> <!-- userinput.qdoc --> <title>Use Case - Responding To User Input in QML | QtDoc 5.1</title> <link rel="stylesheet" type="text/css" href="style/offline.css" /> </head> <body> <div class="header" id="qtdocheader"></div> <div class="content"> <div class="line"> <div class="content mainContent"> <div class="toc"> <h3><a name="toc">Contents</a></h3> <ul> <li class="level1"><a href="#supported-types-of-user-input">Supported Types of User Input</a></li> <li class="level2"><a href="#mouse-and-touch-events">Mouse and Touch Events</a></li> <li class="level2"><a href="#keyboard-and-button-events">Keyboard and Button Events</a></li> </ul> </div> <h1 class="title">Use Case - Responding To User Input in QML</h1> <span class="subtitle"></span> <!-- $$$qtquick-usecase-userinput.html-description --> <div class="descr"> <a name="details"></a> <a name="supported-types-of-user-input"></a> <h2>Supported Types of User Input</h2> <p>The <a href="whatsnew51.html#qt-quick">Qt Quick</a> module provides support for the most common types of user input, including mouse and touch events, text input and key-press events. Other modules provide support for other types of user input (for example, the Qt Sensors module provides support for shake-gestures in QML applications).</p> <p>This article covers how to handle basic user input; for further information about motion-gesture support, please see the Qt Sensors documentation. For information about audio-visual input, please see the Qt Multimedia documentation.</p> <a name="mouse-and-touch-events"></a> <h3>Mouse and Touch Events</h3> <p>The MouseArea type allows mouse and touch events to be handled in a QML application. A MouseArea can be combined with either an Image or a Rectangle and <a href="whatsnew50.html#text">Text</a> object to implement a simple button.</p> <pre class="qml">import QtQuick 2.0 <span class="type">Item</span> { <span class="name">id</span>: <span class="name">root</span> <span class="name">width</span>: <span class="number">320</span> <span class="name">height</span>: <span class="number">480</span> <span class="type">Rectangle</span> { <span class="name">color</span>: <span class="string">"#272822"</span> <span class="name">width</span>: <span class="number">320</span> <span class="name">height</span>: <span class="number">480</span> } <span class="type">Rectangle</span> { <span class="name">id</span>: <span class="name">rectangle</span> <span class="name">x</span>: <span class="number">40</span> <span class="name">y</span>: <span class="number">20</span> <span class="name">width</span>: <span class="number">120</span> <span class="name">height</span>: <span class="number">120</span> <span class="name">color</span>: <span class="string">"red"</span> <span class="type">MouseArea</span> { <span class="name">anchors</span>.fill: <span class="name">parent</span> <span class="name">onClicked</span>: <span class="name">rectangle</span>.<span class="name">width</span> <span class="operator">+=</span> <span class="number">10</span> } } }</pre> <p>For more advanced use cases requiring multiple touch points, please read the documentation for the MultiPointTouchArea type and the PinchArea type.</p> <p>Note that some types have their own built in input handling. For example, Flickable responds to mouse dragging, mouse wheel scrolling, touch dragging, and touch flicking by default.</p> <a name="keyboard-and-button-events"></a> <h3>Keyboard and Button Events</h3> <p>Button and key presses, from buttons on a device, a keypad, or a keyboard, can all be handled using the Keys attached property. This attached property is available on all Item derived types, and works with the Item::focus property to determine which type receives the key event. For simple key handling, you can set the focus to true on a single Item and do all your key handling there.</p> <pre class="qml">import QtQuick 2.0 <span class="type">Item</span> { <span class="name">id</span>: <span class="name">root</span> <span class="name">width</span>: <span class="number">320</span> <span class="name">height</span>: <span class="number">480</span> <span class="type">Rectangle</span> { <span class="name">color</span>: <span class="string">"#272822"</span> <span class="name">width</span>: <span class="number">320</span> <span class="name">height</span>: <span class="number">480</span> } <span class="type">Rectangle</span> { <span class="name">id</span>: <span class="name">rectangle</span> <span class="name">x</span>: <span class="number">40</span> <span class="name">y</span>: <span class="number">20</span> <span class="name">width</span>: <span class="number">120</span> <span class="name">height</span>: <span class="number">120</span> <span class="name">color</span>: <span class="string">"red"</span> <span class="name">focus</span>: <span class="number">true</span> <span class="name">Keys</span>.onUpPressed: <span class="name">rectangle</span>.<span class="name">y</span> <span class="operator">-=</span> <span class="number">10</span> <span class="name">Keys</span>.onDownPressed: <span class="name">rectangle</span>.<span class="name">y</span> <span class="operator">+=</span> <span class="number">10</span> <span class="name">Keys</span>.onLeftPressed: <span class="name">rectangle</span>.<span class="name">x</span> <span class="operator">+=</span> <span class="number">10</span> <span class="name">Keys</span>.onRightPressed: <span class="name">rectangle</span>.<span class="name">x</span> <span class="operator">-=</span> <span class="number">10</span> } }</pre> <p>For text input the <a href="whatsnew51.html#qt-quick">Qt Quick</a> module provides several built-in types. In particular, the TextInput and TextEdit types allow for single-line entry and multi-line editing respectively.</p> <p>Here is all you need to get a working TextInput:</p> <pre class="cpp">import <span class="type">QtQuick</span> <span class="number">2.0</span> TextInput { focus: <span class="keyword">true</span> text: <span class="string">"Initial Text"</span> }</pre> </div> <!-- @@@qtquick-usecase-userinput.html --> </div> </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> </body> </html>