From 6f75096afc000991111bb0fd7a7e530ce3518627 Mon Sep 17 00:00:00 2001 From: Frederik Gladhorn Date: Fri, 29 Jul 2016 13:34:29 +0200 Subject: Add helper function to reset QMouseEvent localPos In Qt Quick there are many places which copy mouse events repeatedly, with the only goal of adjusting the local position. Instead it's much more sensible to re-use the same event. Change-Id: I2c6f2b73ee3a7a6df489f813cf2f60b48a6e48df Reviewed-by: Shawn Rutledge --- src/gui/kernel/qevent.cpp | 12 ++++++++++++ src/gui/kernel/qevent.h | 2 ++ .../auto/gui/kernel/qmouseevent/tst_qmouseevent.cpp | 21 +++++++++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/src/gui/kernel/qevent.cpp b/src/gui/kernel/qevent.cpp index 47c1f91fb0..d574ba5cc6 100644 --- a/src/gui/kernel/qevent.cpp +++ b/src/gui/kernel/qevent.cpp @@ -404,6 +404,18 @@ Qt::MouseEventFlags QMouseEvent::flags() const \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 diff --git a/src/gui/kernel/qevent.h b/src/gui/kernel/qevent.h index fb8a49b1c6..fb72c561ab 100644 --- a/src/gui/kernel/qevent.h +++ b/src/gui/kernel/qevent.h @@ -132,6 +132,8 @@ public: inline Qt::MouseButton button() const { return b; } inline Qt::MouseButtons buttons() const { return mouseState; } + inline void setLocalPos(const QPointF &localPosition) { l = localPosition; } + #if QT_DEPRECATED_SINCE(5, 0) QT_DEPRECATED inline QPointF posF() const { return l; } #endif diff --git a/tests/auto/gui/kernel/qmouseevent/tst_qmouseevent.cpp b/tests/auto/gui/kernel/qmouseevent/tst_qmouseevent.cpp index 6af6738bdb..a1de205571 100644 --- a/tests/auto/gui/kernel/qmouseevent/tst_qmouseevent.cpp +++ b/tests/auto/gui/kernel/qmouseevent/tst_qmouseevent.cpp @@ -75,6 +75,7 @@ public slots: void cleanupTestCase(); void init(); private slots: + void mouseEventBasic(); void checkMousePressEvent_data(); void checkMousePressEvent(); void checkMouseReleaseEvent_data(); @@ -107,6 +108,26 @@ void tst_QMouseEvent::init() testMouseWidget->mouseReleaseModifiers = 0; } +void tst_QMouseEvent::mouseEventBasic() +{ + QPointF local(100, 100); + QPointF scene(200, 200); + QPointF screen(300, 300); + QMouseEvent me(QEvent::MouseButtonPress, local, scene, screen, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier); + QCOMPARE(me.isAccepted(), true); + QCOMPARE(me.button(), Qt::LeftButton); + QCOMPARE(me.buttons(), Qt::LeftButton); + QCOMPARE(me.localPos(), local); + QCOMPARE(me.windowPos(), scene); + QCOMPARE(me.screenPos(), screen); + + QPointF changedLocal(33, 66); + me.setLocalPos(changedLocal); + QCOMPARE(me.localPos(), changedLocal); + QCOMPARE(me.windowPos(), scene); + QCOMPARE(me.screenPos(), screen); +} + void tst_QMouseEvent::checkMousePressEvent_data() { QTest::addColumn("buttonPressed"); -- cgit v1.2.3