<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- userinput.qdoc --> <title>Use Case - Responding To User Input in QML | Qt 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 ><a href="index.html">Qt 5.9</a></td><td >Use Case - Responding To User Input in QML</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="#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> <div class="sidebar-content" id="sidebar-content"></div></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 id="supported-types-of-user-input">Supported Types of User Input</h2> <p>The Qt Quick 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.3 <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.3 <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 Qt Quick 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.3</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> </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>