-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqevent.cpp
4885 lines (3840 loc) · 157 KB
/
qevent.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include "qevent.h"
#include "qcursor.h"
#include "private/qguiapplication_p.h"
#include "private/qinputdevice_p.h"
#include "private/qpointingdevice_p.h"
#include "qpa/qplatformintegration.h"
#include "private/qevent_p.h"
#include "private/qeventpoint_p.h"
#include "qfile.h"
#include "qhashfunctions.h"
#include "qmetaobject.h"
#include "qmimedata.h"
#include "qevent_p.h"
#include "qmath.h"
#include "qloggingcategory.h"
#include "qpointer.h"
#if QT_CONFIG(draganddrop)
#include <qpa/qplatformdrag.h>
#include <private/qdnd_p.h>
#endif
#if QT_CONFIG(shortcut)
#include <private/qshortcut_p.h>
#endif
#include <private/qdebug_p.h>
#define Q_IMPL_POINTER_EVENT(Class) \
Class::Class(const Class &) = default; \
Class::~Class() = default; \
Class* Class::clone() const \
{ \
auto c = new Class(*this); \
for (auto &point : c->m_points) \
QMutableEventPoint::detach(point); \
QEvent *e = c; \
/* check that covariant return is safe to add */ \
Q_ASSERT(reinterpret_cast<quintptr>(c) == reinterpret_cast<quintptr>(e)); \
return c; \
}
QT_BEGIN_NAMESPACE
static_assert(sizeof(QMutableTouchEvent) == sizeof(QTouchEvent));
static_assert(sizeof(QMutableSinglePointEvent) == sizeof(QSinglePointEvent));
static_assert(sizeof(QMouseEvent) == sizeof(QSinglePointEvent));
static_assert(sizeof(QVector2D) == sizeof(quint64));
/*!
\class QEnterEvent
\ingroup events
\inmodule QtGui
\brief The QEnterEvent class contains parameters that describe an enter event.
Enter events occur when the mouse cursor enters a window or a widget.
\since 5.0
*/
/*!
Constructs an enter event object originating from \a device.
The points \a localPos, \a scenePos and \a globalPos specify the
mouse cursor's position relative to the receiving widget or item,
window, and screen or desktop, respectively.
*/
QEnterEvent::QEnterEvent(const QPointF &localPos, const QPointF &scenePos, const QPointF &globalPos, const QPointingDevice *device)
: QSinglePointEvent(QEvent::Enter, device, localPos, scenePos, globalPos, Qt::NoButton, Qt::NoButton, Qt::NoModifier)
{
}
Q_IMPL_POINTER_EVENT(QEnterEvent)
/*!
\fn QPoint QEnterEvent::globalPos() const
\deprecated [6.0] Use globalPosition() instead.
Returns the global position of the mouse cursor \e{at the time of the event}.
*/
/*!
\fn int QEnterEvent::globalX() const
\deprecated [6.0] Use globalPosition().x() instead.
Returns the global position on the X-axis of the mouse cursor \e{at the time of the event}.
*/
/*!
\fn int QEnterEvent::globalY() const
\deprecated [6.0] Use globalPosition().y() instead.
Returns the global position on the Y-axis of the mouse cursor \e{at the time of the event}.
*/
/*!
\fn QPointF QEnterEvent::localPos() const
\deprecated [6.0] Use position() instead.
Returns the mouse cursor's position relative to the receiving widget.
*/
/*!
\fn QPoint QEnterEvent::pos() const
\deprecated [6.0] Use position().toPoint() instead.
Returns the position of the mouse cursor relative to the receiving widget.
*/
/*!
\fn QPointF QEnterEvent::screenPos() const
\deprecated [6.0] Use globalPosition() instead.
Returns the position of the mouse cursor relative to the receiving screen.
*/
/*!
\fn QPointF QEnterEvent::windowPos() const
\deprecated [6.0] Use scenePosition() instead.
Returns the position of the mouse cursor relative to the receiving window.
*/
/*!
\fn int QEnterEvent::x() const
\deprecated [6.0] Use position().x() instead.
Returns the x position of the mouse cursor relative to the receiving widget.
*/
/*!
\fn int QEnterEvent::y() const
\deprecated [6.0] Use position().y() instead.
Returns the y position of the mouse cursor relative to the receiving widget.
*/
/*!
\class QInputEvent
\ingroup events
\inmodule QtGui
\brief The QInputEvent class is the base class for events that
describe user input.
*/
/*!
\internal
*/
QInputEvent::QInputEvent(Type type, const QInputDevice *dev, Qt::KeyboardModifiers modifiers)
: QEvent(type, QEvent::InputEventTag{}), m_dev(dev), m_modState(modifiers), m_reserved(0)
{}
/*!
\internal
*/
QInputEvent::QInputEvent(QEvent::Type type, QEvent::PointerEventTag, const QInputDevice *dev, Qt::KeyboardModifiers modifiers)
: QEvent(type, QEvent::PointerEventTag{}), m_dev(dev), m_modState(modifiers), m_reserved(0)
{}
/*!
\internal
*/
QInputEvent::QInputEvent(QEvent::Type type, QEvent::SinglePointEventTag, const QInputDevice *dev, Qt::KeyboardModifiers modifiers)
: QEvent(type, QEvent::SinglePointEventTag{}), m_dev(dev), m_modState(modifiers), m_reserved(0)
{}
Q_IMPL_EVENT_COMMON(QInputEvent)
/*!
\fn QInputDevice *QInputEvent::device() const
\since 6.0
Returns the source device that generated the original event.
In case of a synthesized event, for example a mouse event that was
generated from a touch event, \c device() continues to return the touchscreen
device, so that you can tell that it did not come from an actual mouse.
Thus \c {mouseEvent.source()->type() != QInputDevice::DeviceType::Mouse}
is one possible replacement for the Qt 5 expression
\c {mouseEvent.source() == Qt::MouseEventSynthesizedByQt}.
\sa QPointerEvent::pointingDevice()
*/
/*!
\fn QInputDevice::DeviceType QInputEvent::deviceType() const
Returns the type of device that generated the event.
*/
/*!
\fn Qt::KeyboardModifiers QInputEvent::modifiers() const
Returns the keyboard modifier flags that existed immediately
before the event occurred.
\sa QGuiApplication::keyboardModifiers()
*/
/*! \fn void QInputEvent::setModifiers(Qt::KeyboardModifiers modifiers)
\internal
Sets the keyboard modifiers flags for this event.
*/
/*!
\fn quint64 QInputEvent::timestamp() const
Returns the window system's timestamp for this event.
It will normally be in milliseconds since some arbitrary point
in time, such as the time when the system was started.
*/
/*! \fn void QInputEvent::setTimestamp(quint64 atimestamp)
\internal
Sets the timestamp for this event.
*/
/*!
\class QPointerEvent
\since 6.0
\inmodule QtGui
\brief A base class for pointer events.
*/
/*!
\fn qsizetype QPointerEvent::pointCount() const
Returns the number of points in this pointer event.
*/
/*!
Returns a QEventPoint reference for the point at index \a i.
*/
QEventPoint &QPointerEvent::point(qsizetype i)
{
return m_points[i];
}
/*!
\fn const QList<QEventPoint> &QPointerEvent::points() const
Returns a list of points in this pointer event.
*/
/*!
\fn QPointingDevice::PointerType QPointerEvent::pointerType() const
Returns the type of point that generated the event.
*/
/*!
\internal
*/
QPointerEvent::QPointerEvent(QEvent::Type type, const QPointingDevice *dev,
Qt::KeyboardModifiers modifiers, const QList<QEventPoint> &points)
: QInputEvent(type, QEvent::PointerEventTag{}, dev, modifiers), m_points(points)
{
}
/*!
\internal
*/
QPointerEvent::QPointerEvent(QEvent::Type type, QEvent::SinglePointEventTag, const QInputDevice *dev, Qt::KeyboardModifiers modifiers)
: QInputEvent(type, QEvent::SinglePointEventTag{}, dev, modifiers)
{
}
Q_IMPL_POINTER_EVENT(QPointerEvent);
/*!
Returns the point whose \l {QEventPoint::id()}{id} matches the given \a id,
or \c nullptr if no such point is found.
*/
QEventPoint *QPointerEvent::pointById(int id)
{
for (auto &p : m_points) {
if (p.id() == id)
return &p;
}
return nullptr;
}
/*!
Returns \c true if every point in points() has either an exclusiveGrabber()
or one or more passiveGrabbers().
*/
bool QPointerEvent::allPointsGrabbed() const
{
for (const auto &p : points()) {
if (!exclusiveGrabber(p) && passiveGrabbers(p).isEmpty())
return false;
}
return true;
}
/*!
Returns \c true if isPointAccepted() is \c true for every point in
points(); otherwise \c false.
*/
bool QPointerEvent::allPointsAccepted() const
{
for (const auto &p : points()) {
if (!p.isAccepted())
return false;
}
return true;
}
/*!
\reimp
*/
void QPointerEvent::setAccepted(bool accepted)
{
QEvent::setAccepted(accepted);
for (auto &p : m_points)
p.setAccepted(accepted);
}
/*!
Returns the source device from which this event originates.
This is the same as QInputEvent::device() but typecast for convenience.
*/
const QPointingDevice *QPointerEvent::pointingDevice() const
{
return static_cast<const QPointingDevice *>(m_dev);
}
/*! \internal
Sets the timestamp for this event and its points().
*/
void QPointerEvent::setTimestamp(quint64 timestamp)
{
QInputEvent::setTimestamp(timestamp);
for (auto &p : m_points)
QMutableEventPoint::setTimestamp(p, timestamp);
}
/*!
Returns the object which has been set to receive all future update events
and the release event containing the given \a point.
It's mainly for use in Qt Quick at this time.
*/
QObject *QPointerEvent::exclusiveGrabber(const QEventPoint &point) const
{
Q_ASSERT(pointingDevice());
auto persistentPoint = QPointingDevicePrivate::get(pointingDevice())->queryPointById(point.id());
if (Q_UNLIKELY(!persistentPoint)) {
qWarning() << "point is not in activePoints" << point;
return nullptr;
}
return persistentPoint->exclusiveGrabber;
}
/*!
Informs the delivery logic that the given \a exclusiveGrabber is to
receive all future update events and the release event containing
the given \a point, and that delivery to other items can be skipped.
It's mainly for use in Qt Quick at this time.
*/
void QPointerEvent::setExclusiveGrabber(const QEventPoint &point, QObject *exclusiveGrabber)
{
Q_ASSERT(pointingDevice());
auto devPriv = QPointingDevicePrivate::get(const_cast<QPointingDevice *>(pointingDevice()));
devPriv->setExclusiveGrabber(this, point, exclusiveGrabber);
}
/*!
Returns the list of objects that have been requested to receive all
future update events and the release event containing the given \a point.
It's only for use by \l {Qt Quick Input Handlers}.
\sa QPointerEvent::addPassiveGrabber()
*/
QList<QPointer<QObject> > QPointerEvent::passiveGrabbers(const QEventPoint &point) const
{
Q_ASSERT(pointingDevice());
auto persistentPoint = QPointingDevicePrivate::get(pointingDevice())->queryPointById(point.id());
if (Q_UNLIKELY(!persistentPoint)) {
qWarning() << "point is not in activePoints" << point;
return {};
}
return persistentPoint->passiveGrabbers;
}
/*!
Informs the delivery logic that the given \a grabber is to receive all
future update events and the release event containing the given \a point,
regardless where else those events may be delivered.
It's only for use by \l {Qt Quick Input Handlers}.
Returns \c false if \a grabber was already added, \c true otherwise.
*/
bool QPointerEvent::addPassiveGrabber(const QEventPoint &point, QObject *grabber)
{
Q_ASSERT(pointingDevice());
auto devPriv = QPointingDevicePrivate::get(const_cast<QPointingDevice *>(pointingDevice()));
return devPriv->addPassiveGrabber(this, point, grabber);
}
/*!
Removes the passive \a grabber from the given \a point if it was previously added.
Returns \c true if it had been a passive grabber before, \c false if not.
It's only for use by \l {Qt Quick Input Handlers}.
\sa QPointerEvent::addPassiveGrabber()
*/
bool QPointerEvent::removePassiveGrabber(const QEventPoint &point, QObject *grabber)
{
Q_ASSERT(pointingDevice());
auto devPriv = QPointingDevicePrivate::get(const_cast<QPointingDevice *>(pointingDevice()));
return devPriv->removePassiveGrabber(this, point, grabber);
}
/*!
Removes all passive grabbers from the given \a point.
It's only for use by \l {Qt Quick Input Handlers}.
\sa QPointerEvent::addPassiveGrabber()
*/
void QPointerEvent::clearPassiveGrabbers(const QEventPoint &point)
{
Q_ASSERT(pointingDevice());
auto devPriv = QPointingDevicePrivate::get(const_cast<QPointingDevice *>(pointingDevice()));
devPriv->clearPassiveGrabbers(this, point);
}
/*!
\class QSinglePointEvent
\since 6.0
\inmodule QtGui
\brief A base class for pointer events containing a single point, such as
mouse events.
*/
/*! \fn Qt::MouseButton QSinglePointEvent::button() const
Returns the button that caused the event.
The returned value is always Qt::NoButton for mouse move events, as
well as \l TabletMove, \l TabletEnterProximity, and
\l TabletLeaveProximity events.
\sa buttons()
*/
/*! \fn Qt::MouseButtons QSinglePointEvent::buttons() const
Returns the button state when the event was generated.
The button state is a combination of Qt::LeftButton, Qt::RightButton,
and Qt::MiddleButton using the OR operator.
For mouse move or \l TabletMove events, this is all buttons that are
pressed down.
For mouse press, double click, or \l TabletPress events, this includes
the button that caused the event.
For mouse release or \l TabletRelease events, this excludes the button
that caused the event.
\sa button()
*/
/*! \fn QPointF QSinglePointEvent::position() const
Returns the position of the point in this event, relative to the widget or
item that received the event.
If you move your widgets around in response to mouse events, use
globalPosition() instead.
\sa globalPosition()
*/
/*! \fn QPointF QSinglePointEvent::scenePosition() const
Returns the position of the point in this event, relative to the window or
scene.
\sa QEventPoint::scenePosition
*/
/*! \fn QPointF QSinglePointEvent::globalPosition() const
Returns the position of the point in this event on the screen or virtual
desktop.
\note The global position of a mouse pointer is recorded \e{at the time
of the event}. This is important on asynchronous window systems
such as X11; whenever you move your widgets around in response to
mouse events, globalPosition() can differ a lot from the current
cursor position returned by QCursor::pos().
\sa position()
*/
/*!
\internal
*/
QSinglePointEvent::QSinglePointEvent(QEvent::Type type, const QPointingDevice *dev,
const QPointF &localPos, const QPointF &scenePos, const QPointF &globalPos,
Qt::MouseButton button, Qt::MouseButtons buttons,
Qt::KeyboardModifiers modifiers, Qt::MouseEventSource source)
: QPointerEvent(type, QEvent::SinglePointEventTag{}, dev, modifiers),
m_button(button),
m_mouseState(buttons),
m_source(source),
m_reserved(0), m_reserved2(0),
m_doubleClick(false), m_phase(0), m_invertedScrolling(0)
{
bool isPress = (button != Qt::NoButton && (button | buttons) == buttons);
bool isWheel = (type == QEvent::Type::Wheel);
auto devPriv = QPointingDevicePrivate::get(const_cast<QPointingDevice *>(pointingDevice()));
auto epd = devPriv->pointById(0);
QEventPoint &p = epd->eventPoint;
Q_ASSERT(p.device() == dev);
// p is a reference to a non-detached instance that lives in QPointingDevicePrivate::activePoints.
// Update persistent info in that instance.
if (isPress || isWheel)
QMutableEventPoint::setGlobalLastPosition(p, globalPos);
else
QMutableEventPoint::setGlobalLastPosition(p, p.globalPosition());
QMutableEventPoint::setGlobalPosition(p, globalPos);
if (isWheel && p.state() != QEventPoint::State::Updated)
QMutableEventPoint::setGlobalPressPosition(p, globalPos);
if (type == MouseButtonDblClick)
QMutableEventPoint::setState(p, QEventPoint::State::Stationary);
else if (button == Qt::NoButton || isWheel)
QMutableEventPoint::setState(p, QEventPoint::State::Updated);
else if (isPress)
QMutableEventPoint::setState(p, QEventPoint::State::Pressed);
else
QMutableEventPoint::setState(p, QEventPoint::State::Released);
QMutableEventPoint::setScenePosition(p, scenePos);
// Now detach, and update the detached instance with ephemeral state.
QMutableEventPoint::detach(p);
QMutableEventPoint::setPosition(p, localPos);
m_points.append(p);
}
/*! \internal
Constructs a single-point event with the given \a point, which must be an instance
(or copy of one) that already exists in QPointingDevicePrivate::activePoints.
Unlike the other constructor, it does not modify the given \a point in any way.
This is useful when synthesizing a QMouseEvent from one point taken from a QTouchEvent, for example.
\sa QMutableSinglePointEvent()
*/
QSinglePointEvent::QSinglePointEvent(QEvent::Type type, const QPointingDevice *dev, const QEventPoint &point,
Qt::MouseButton button, Qt::MouseButtons buttons,
Qt::KeyboardModifiers modifiers, Qt::MouseEventSource source)
: QPointerEvent(type, QEvent::SinglePointEventTag{}, dev, modifiers),
m_button(button),
m_mouseState(buttons),
m_source(source),
m_reserved(0), m_reserved2(0),
m_doubleClick(false), m_phase(0), m_invertedScrolling(0)
{
m_points << point;
}
Q_IMPL_POINTER_EVENT(QSinglePointEvent)
/*!
Returns \c true if this event represents a \l {button()}{button} being pressed.
*/
bool QSinglePointEvent::isBeginEvent() const
{
// A double-click event does not begin a sequence: it comes after a press event,
// and while it tells which button caused the double-click, it doesn't represent
// a change of button state. So it's an update event.
return m_button != Qt::NoButton && m_mouseState.testFlag(m_button)
&& type() != QEvent::MouseButtonDblClick;
}
/*!
Returns \c true if this event does not include a change in \l {buttons()}{button state}.
*/
bool QSinglePointEvent::isUpdateEvent() const
{
// A double-click event is an update event even though it tells which button
// caused the double-click, because a MouseButtonPress event was sent right before it.
return m_button == Qt::NoButton || type() == QEvent::MouseButtonDblClick;
}
/*!
Returns \c true if this event represents a \l {button()}{button} being released.
*/
bool QSinglePointEvent::isEndEvent() const
{
return m_button != Qt::NoButton && !m_mouseState.testFlag(m_button);
}
/*!
\property QSinglePointEvent::exclusivePointGrabber
\brief the object that will receive future updates
The exclusive grabber is an object that has chosen to receive all future
update events and the release event containing the same point that this
event carries.
Setting the exclusivePointGrabber property is a convenience equivalent to:
\code
setExclusiveGrabber(points().first(), exclusiveGrabber);
\endcode
*/
/*!
\class QMouseEvent
\ingroup events
\inmodule QtGui
\brief The QMouseEvent class contains parameters that describe a mouse event.
Mouse events occur when a mouse button is pressed or released
inside a widget, or when the mouse cursor is moved.
Mouse move events will occur only when a mouse button is pressed
down, unless mouse tracking has been enabled with
QWidget::setMouseTracking().
Qt automatically grabs the mouse when a mouse button is pressed
inside a widget; the widget will continue to receive mouse events
until the last mouse button is released.
A mouse event contains a special accept flag that indicates
whether the receiver wants the event. You should call ignore() if
the mouse event is not handled by your widget. A mouse event is
propagated up the parent widget chain until a widget accepts it
with accept(), or an event filter consumes it.
\note If a mouse event is propagated to a \l{QWidget}{widget} for
which Qt::WA_NoMousePropagation has been set, that mouse event
will not be propagated further up the parent widget chain.
The state of the keyboard modifier keys can be found by calling the
\l{QInputEvent::modifiers()}{modifiers()} function, inherited from
QInputEvent.
The position() function gives the cursor position
relative to the widget or item that receives the mouse event.
If you move the widget as a result of the mouse event, use the
global position returned by globalPosition() to avoid a shaking motion.
The QWidget::setEnabled() function can be used to enable or
disable mouse and keyboard events for a widget.
Reimplement the QWidget event handlers, QWidget::mousePressEvent(),
QWidget::mouseReleaseEvent(), QWidget::mouseDoubleClickEvent(),
and QWidget::mouseMoveEvent() to receive mouse events in your own
widgets.
\sa QWidget::setMouseTracking(), QWidget::grabMouse(),
QCursor::pos()
*/
#if QT_DEPRECATED_SINCE(6, 4)
/*!
\deprecated [6.4] Use another constructor instead (global position is required).
Constructs a mouse event object originating from \a device.
The \a type parameter must be one of QEvent::MouseButtonPress,
QEvent::MouseButtonRelease, QEvent::MouseButtonDblClick,
or QEvent::MouseMove.
The \a localPos is the mouse cursor's position relative to the
receiving widget or item. The window position is set to the same value
as \a localPos.
The \a button that caused the event is given as a value from
the Qt::MouseButton enum. If the event \a type is
\l MouseMove, the appropriate button for this event is Qt::NoButton.
The mouse and keyboard states at the time of the event are specified by
\a buttons and \a modifiers.
The globalPosition() is initialized to QCursor::pos(), which may not
be appropriate. Use the other constructor to specify the global
position explicitly.
*/
QMouseEvent::QMouseEvent(Type type, const QPointF &localPos, Qt::MouseButton button,
Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers, const QPointingDevice *device)
: QSinglePointEvent(type, device, localPos, localPos,
#ifdef QT_NO_CURSOR
localPos,
#else
QCursor::pos(),
#endif
button, buttons, modifiers)
{
}
#endif
/*!
Constructs a mouse event object originating from \a device.
The \a type parameter must be QEvent::MouseButtonPress,
QEvent::MouseButtonRelease, QEvent::MouseButtonDblClick,
or QEvent::MouseMove.
The \a localPos is the mouse cursor's position relative to the
receiving widget or item. The cursor's position in screen coordinates is
specified by \a globalPos. The window position is set to the same value
as \a localPos. The \a button that caused the event is
given as a value from the \l Qt::MouseButton enum. If the event \a
type is \l MouseMove, the appropriate button for this event is
Qt::NoButton. \a buttons is the state of all buttons at the
time of the event, \a modifiers the state of all keyboard
modifiers.
*/
QMouseEvent::QMouseEvent(Type type, const QPointF &localPos, const QPointF &globalPos,
Qt::MouseButton button, Qt::MouseButtons buttons,
Qt::KeyboardModifiers modifiers, const QPointingDevice *device)
: QMouseEvent(type, localPos, localPos, globalPos, button, buttons, modifiers, device)
{
}
/*!
Constructs a mouse event object.
The \a type parameter must be QEvent::MouseButtonPress,
QEvent::MouseButtonRelease, QEvent::MouseButtonDblClick,
or QEvent::MouseMove.
The points \a localPos, \a scenePos and \a globalPos specify the
mouse cursor's position relative to the receiving widget or item,
window, and screen or desktop, respectively.
The \a button that caused the event is given as a value from the
\l Qt::MouseButton enum. If the event \a type is \l MouseMove,
the appropriate button for this event is Qt::NoButton. \a buttons
is the state of all buttons at the time of the event, \a modifiers
is the state of all keyboard modifiers.
*/
QMouseEvent::QMouseEvent(QEvent::Type type, const QPointF &localPos,
const QPointF &scenePos, const QPointF &globalPos,
Qt::MouseButton button, Qt::MouseButtons buttons,
Qt::KeyboardModifiers modifiers, const QPointingDevice *device)
: QSinglePointEvent(type, device, localPos, scenePos, globalPos, button, buttons, modifiers)
{
}
QMouseEvent::QMouseEvent(QEvent::Type type, const QPointF &localPos, const QPointF &windowPos,
const QPointF &globalPos, Qt::MouseButton button, Qt::MouseButtons buttons,
Qt::KeyboardModifiers modifiers, Qt::MouseEventSource source,
const QPointingDevice *device)
: QSinglePointEvent(type, device, localPos, windowPos, globalPos, button, buttons, modifiers, source)
{
}
Q_IMPL_POINTER_EVENT(QMouseEvent)
/*!
\fn Qt::MouseEventSource QMouseEvent::source() const
\since 5.3
\deprecated [6.0] Use pointingDevice() instead.
Returns information about the mouse event source.
The mouse event source can be used to distinguish between genuine
and artificial mouse events. The latter are events that are
synthesized from touch events by the operating system or Qt itself.
This enum tells you from where it was synthesized; but often
it's more useful to know from which device it was synthesized,
so try to use pointingDevice() instead.
\note Many platforms provide no such information. On such platforms
\l Qt::MouseEventNotSynthesized is returned always.
\sa Qt::MouseEventSource
\sa QGraphicsSceneMouseEvent::source()
\note In Qt 5-based code, source() was often used to attempt to distinguish
mouse events from an actual mouse vs. those that were synthesized because
some legacy QQuickItem or QWidget subclass did not react to a QTouchEvent.
However, you could not tell whether it was synthesized from a QTouchEvent
or a QTabletEvent, and other information was lost. pointingDevice()
tells you the specific device that it came from, so you might check
\c {pointingDevice()->type()} or \c {pointingDevice()->capabilities()} to
decide how to react to this event. But it's even better to react to the
original event rather than handling only mouse events.
*/
// Note: the docs mention 6.0 as a deprecation version. That is correct and
// intended, because we want our users to stop using it! Internally we will
// deprecate it when we port our code away from using it.
Qt::MouseEventSource QMouseEvent::source() const
{
return Qt::MouseEventSource(m_source);
}
/*!
\since 5.3
Returns the mouse event flags.
The mouse event flags provide additional information about a mouse event.
\sa Qt::MouseEventFlag
\sa QGraphicsSceneMouseEvent::flags()
*/
Qt::MouseEventFlags QMouseEvent::flags() const
{
return (m_doubleClick ? Qt::MouseEventCreatedDoubleClick : Qt::NoMouseEventFlag);
}
/*!
\fn QPointF QMouseEvent::localPos() const
\deprecated [6.0] Use position() instead.
\since 5.0
Returns the position of the mouse cursor as a QPointF, relative to the
widget or item that received the event.
If you move the widget as a result of the mouse event, use the
screen position returned by screenPos() to avoid a shaking
motion.
\sa x(), y(), windowPos(), screenPos()
*/
/*!
\fn void QMouseEvent::setLocalPos(const QPointF &localPosition)
\since 5.8
\internal
Sets the local position in the mouse event to \a localPosition. This allows to re-use one event
when sending it to a series of receivers that expect the local pos in their
respective local coordinates.
*/
/*!
\fn QPointF QMouseEvent::windowPos() const
\deprecated [6.0] Use scenePosition() instead.
\since 5.0
Returns the position of the mouse cursor as a QPointF, relative to the
window that received the event.
If you move the widget as a result of the mouse event, use the
global position returned by globalPos() to avoid a shaking
motion.
\sa x(), y(), pos(), localPos(), screenPos()
*/
/*!
\fn QPointF QMouseEvent::screenPos() const
\deprecated [6.0] Use globalPosition() instead.
\since 5.0
Returns the position of the mouse cursor as a QPointF, relative to the
screen that received the event.
\sa x(), y(), pos(), localPos(), windowPos()
*/
/*!
\fn QPoint QMouseEvent::pos() const
\deprecated [6.0] Use position() instead.
Returns the position of the mouse cursor, relative to the widget
that received the event.
If you move the widget as a result of the mouse event, use the
global position returned by globalPos() to avoid a shaking
motion.
\sa x(), y(), globalPos()
*/
/*!
\fn QPoint QMouseEvent::globalPos() const
\deprecated [6.0] Use globalPosition().toPoint() instead.
Returns the global position of the mouse cursor \e{at the time
of the event}. This is important on asynchronous window systems
like X11. Whenever you move your widgets around in response to
mouse events, globalPos() may differ a lot from the current
pointer position QCursor::pos(), and from
QWidget::mapToGlobal(pos()).
\sa globalX(), globalY()
*/
/*!
\fn int QMouseEvent::x() const
\deprecated [6.0] Use position().x() instead.
Returns the x position of the mouse cursor, relative to the
widget that received the event.
\sa y(), pos()
*/
/*!
\fn int QMouseEvent::y() const
\deprecated [6.0] Use position().y() instead.
Returns the y position of the mouse cursor, relative to the
widget that received the event.
\sa x(), pos()
*/
/*!
\fn int QMouseEvent::globalX() const
\deprecated [6.0] Use globalPosition().x() instead.
Returns the global x position of the mouse cursor at the time of
the event.
\sa globalY(), globalPos()
*/
/*!
\fn int QMouseEvent::globalY() const
\deprecated [6.0] Use globalPosition().y() instead.
Returns the global y position of the mouse cursor at the time of
the event.
\sa globalX(), globalPos()
*/
/*!
\class QHoverEvent
\ingroup events
\inmodule QtGui
\brief The QHoverEvent class contains parameters that describe a mouse event.
Mouse events occur when a mouse cursor is moved into, out of, or within a
widget, and if the widget has the Qt::WA_Hover attribute.
The function pos() gives the current cursor position, while oldPos() gives
the old mouse position.
There are a few similarities between the events QEvent::HoverEnter
and QEvent::HoverLeave, and the events QEvent::Enter and QEvent::Leave.
However, they are slightly different because we do an update() in the event
handler of HoverEnter and HoverLeave.
QEvent::HoverMove is also slightly different from QEvent::MouseMove. Let us
consider a top-level window A containing a child B which in turn contains a
child C (all with mouse tracking enabled):
\image hoverevents.png
Now, if you move the cursor from the top to the bottom in the middle of A,
you will get the following QEvent::MouseMove events:
\list 1
\li A::MouseMove
\li B::MouseMove
\li C::MouseMove
\endlist
You will get the same events for QEvent::HoverMove, except that the event
always propagates to the top-level regardless whether the event is accepted
or not. It will only stop propagating with the Qt::WA_NoMousePropagation
attribute.
In this case the events will occur in the following way:
\list 1
\li A::HoverMove
\li A::HoverMove, B::HoverMove
\li A::HoverMove, B::HoverMove, C::HoverMove
\endlist
*/
/*!
\fn QPoint QHoverEvent::pos() const
\deprecated [6.0] Use position().toPoint() instead.
Returns the position of the mouse cursor, relative to the widget
that received the event.
On QEvent::HoverLeave events, this position will always be