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
10
A more modern way of handling events from all pointing devices, including
11
mouse and touchscreen, is via \l {Qt Quick Input Handlers}{Input Handlers}.
12
This page describes the original Qt Quick \l MouseArea type, which was
13
initially designed to handle mouse input, and later began handling single-touch
14
events (in the form of synthetic mouse events) in simple touch-oriented user
15
interfaces.
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
26
QML uses \l{qtqml-syntax-signals.html}{signals and handlers} to
27
deliver mouse interactions. Specifically, Qt Quick provides the \l MouseArea
28
and \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
33
The \l MouseArea type receives events within a defined area. One quick way
34
to 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
36
component), then the MouseArea will fill the area defined by the parent's
37
dimensions. Alternatively, an area smaller or larger than the parent is
38
definable.
39
\snippet qml/mousearea/mousearea-snippet.qml anchor fill
40
41
\section1 Receiving Events
42
43
The MouseArea type emits
44
\l{qtqml-syntax-signals.html}{signals} in response to different
45
mouse events. The \l MouseArea type documentation describes these
46
gestures 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
60
These 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
64
Some mouse gestures and button clicks need to be enabled before they send or
65
receive events. Certain \l MouseArea and \l MouseEvent properties enable these
66
gestures.
67
68
To listen to (or explicitly ignore) a certain mouse button, set the appropriate
69
mouse button to the \l {MouseArea::acceptedButtons}{acceptedButtons} property.
70
71
Naturally, the mouse events, such as button presses and mouse positions, are
72
sent during a mouse click. For example, the \c containsMouse property will only
73
retrieve its correct value during a mouse press. The
74
\l {MouseArea::hoverEnabled}{hoverEnabled} will enable mouse events and
75
positioning 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
80
Additionally, to disable the whole mouse area, set the MouseArea
81
\c enabled property to \c false.
82
83
\section1 MouseEvent Object
84
85
Signals and their callbacks receive a \l MouseEvent object as a parameter.
86
The \c mouse object contains information about the mouse event. For example,
87
the mouse button that started the event is queried through the
88
\l {MouseEvent::button}{mouse.button} property.
89
90
The \c MouseEvent object can also ignore a mouse event using its \c accepted
91
property.
92
93
\section2 Accepting Further Signals
94
Many of the signals are sent multiple times to reflect various mouse events
95
such as double clicking. To facilitate the classification of mouse clicks, the
96
MouseEvent object has an \c accepted property to disable the event propagation.
97
98
To 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
*/
qtdeclarative
src
quick
doc
src
concepts
input
mouse.qdoc
Generated on
for Qt by
1.14.0