Use Case - Responding To User Input in QML#
Example of how to accept user input and respond to it in a QML application
Supported types of user input#
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.
This article covers how to handle basic user input. For information about audio-visual input, see the Qt Multimedia documentation.
Mouse and touch events#
The input handlers let QML applications handle mouse and touch events. For example, you could create a button by adding a TapHandler to an Image, or to a Rectangle with a Text object inside. The TapHandler responds to taps or clicks on any type of pointing device.
import QtQuick Item { id: root width: 320 height: 480 Rectangle { color: "#272822" width: 320 height: 480 } Rectangle { id: rectangle x: 40 y: 20 width: 120 height: 120 color: "red" TapHandler { onTapped: rectangle.width += 10 } } }
Note
Some Item types have their own built-in input handling. For example, Flickable responds to mouse dragging, touch flicking, and mouse wheel scrolling.