Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
mouse.qdoc
Go to the documentation of this file.
1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5\page qtquick-input-mouseevents.html
6\ingroup QML Features
7\title Mouse Events
8\brief handling mouse events in Qt Quick
9
10A more modern way of handling events from all pointing devices, including
11mouse and touchscreen, is via \l {Qt Quick Input Handlers}{Input Handlers}.
12This page describes the original Qt Quick \l MouseArea type, which was
13initially designed to handle mouse input, and later began handling single-touch
14events (in the form of synthetic mouse events) in simple touch-oriented user
15interfaces.
16
17\section1 Mouse Types
18
19\list
20\li \l{MouseArea} type
21\li \l{MouseEvent} object
22\endlist
23
24\section1 Mouse Event Handling
25
26QML uses \l{qtqml-syntax-signals.html}{signals and handlers} to
27deliver mouse interactions. Specifically, Qt Quick provides the \l MouseArea
28and \l MouseEvent types that allow developers to define JavaScript callbacks
29(also called signal handlers), which accept mouse events within a defined area.
30
31\section1 Defining a Mouse Area
32
33The \l MouseArea type receives events within a defined area. One quick way
34to define this area is to anchor the \c MouseArea to its parent's area using the
35\c anchors.fill property. If the parent is a \l Rectangle (or any \l Item
36component), then the MouseArea will fill the area defined by the parent's
37dimensions. Alternatively, an area smaller or larger than the parent is
38definable.
39\snippet qml/mousearea/mousearea-snippet.qml anchor fill
40
41\section1 Receiving Events
42
43The MouseArea type emits
44\l{qtqml-syntax-signals.html}{signals} in response to different
45mouse events. The \l MouseArea type documentation describes these
46gestures in greater detail:
47
48\list
49\li canceled
50\li clicked
51\li doubleClicked
52\li entered
53\li exited
54\li positionChanged
55\li pressAndHold
56\li pressed
57\li released
58\endlist
59
60These signals can have callbacks that are invoked when the signals are emitted.
61\snippet qml/mousearea/mousearea-snippet.qml signal handlers
62
63\section1 Enabling Gestures
64Some mouse gestures and button clicks need to be enabled before they send or
65receive events. Certain \l MouseArea and \l MouseEvent properties enable these
66gestures.
67
68To listen to (or explicitly ignore) a certain mouse button, set the appropriate
69mouse button to the \l {MouseArea::acceptedButtons}{acceptedButtons} property.
70
71Naturally, the mouse events, such as button presses and mouse positions, are
72sent during a mouse click. For example, the \c containsMouse property will only
73retrieve its correct value during a mouse press. The
74\l {MouseArea::hoverEnabled}{hoverEnabled} will enable mouse events and
75positioning even when there are no mouse button presses. Setting the
76\c hoverEnabled property to \c true, in turn will enable the \c entered,
77\c exited, and \c positionChanged signal and their respective signal handlers.
78
79\snippet qml/mousearea/mousearea-snippet.qml enable handlers
80Additionally, to disable the whole mouse area, set the MouseArea
81\c enabled property to \c false.
82
83\section1 MouseEvent Object
84
85Signals and their callbacks receive a \l MouseEvent object as a parameter.
86The \c mouse object contains information about the mouse event. For example,
87the mouse button that started the event is queried through the
88\l {MouseEvent::button}{mouse.button} property.
89
90The \c MouseEvent object can also ignore a mouse event using its \c accepted
91property.
92
93\section2 Accepting Further Signals
94Many of the signals are sent multiple times to reflect various mouse events
95such as double clicking. To facilitate the classification of mouse clicks, the
96MouseEvent object has an \c accepted property to disable the event propagation.
97
98To learn more about QML's event system, please read the
99\l{qtqml-syntax-signals.html}{signals and handlers, and event system} document.
100
101*/