diff options
author | Santhosh Kumar <santhosh.kumar.selvaraj@qt.io> | 2023-02-08 09:54:48 +0100 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2023-02-09 12:02:08 +0000 |
commit | b08dcd760c84fcac6e90fae2aaa243434495e6c6 (patch) | |
tree | 547fba4a721f5a5ac423c67d3d9adc854853595a | |
parent | deb0628a18e87be6b4e57ae7dace9408bc0f4f91 (diff) |
Fix mouse inside MouseArea (containsMouse) for press event
In MouseArea, containsMouse (mouse inside MouseArea) flag is enabled for
press event irrespective of whether its accepted or not. This creates
containsMouse to be enabled always for this corresponding item leaving
no option to reset.
To fix this issue, containsMouse flag is enabled only if the press
event is accepted for mouse handler.
Fixes: QTBUG-110594
Change-Id: Ibb0e89529ccebc3063330c22b8b3501b2917d78f
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
(cherry picked from commit 2af8743aab51287ec914d25176ae0fc45202db32)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r-- | src/quick/items/qquickmousearea.cpp | 6 | ||||
-rw-r--r-- | tests/auto/quick/qquickmousearea/data/containsMouseAndHoverDisabled.qml | 15 | ||||
-rw-r--r-- | tests/auto/quick/qquickmousearea/tst_qquickmousearea.cpp | 15 |
3 files changed, 35 insertions, 1 deletions
diff --git a/src/quick/items/qquickmousearea.cpp b/src/quick/items/qquickmousearea.cpp index a99976473f..31c74765f7 100644 --- a/src/quick/items/qquickmousearea.cpp +++ b/src/quick/items/qquickmousearea.cpp @@ -1124,8 +1124,10 @@ void QQuickMouseArea::setHoverEnabled(bool h) \qmlproperty bool QtQuick::MouseArea::containsMouse This property holds whether the mouse is currently inside the mouse area. - \warning If hoverEnabled is false, containsMouse will only be valid + \warning If hoverEnabled is \c false, \c containsMouse will be \c true when the mouse is pressed while the mouse cursor is inside the MouseArea. + But if you set \c {mouse.accepted = false} in an \c onPressed handler, + \c containsMouse will remain \c false because the press was rejected. */ bool QQuickMouseArea::hovered() const { @@ -1235,6 +1237,8 @@ bool QQuickMouseArea::setPressed(Qt::MouseButton button, bool p, Qt::MouseEventS if (!me.isAccepted()) { d->pressed = Qt::NoButton; + if (!hoverEnabled()) + setHovered(false); } if (!oldPressed) { diff --git a/tests/auto/quick/qquickmousearea/data/containsMouseAndHoverDisabled.qml b/tests/auto/quick/qquickmousearea/data/containsMouseAndHoverDisabled.qml new file mode 100644 index 0000000000..d98ef85c55 --- /dev/null +++ b/tests/auto/quick/qquickmousearea/data/containsMouseAndHoverDisabled.qml @@ -0,0 +1,15 @@ +import QtQuick + +Rectangle { + width: 200 + height: 200 + visible: true + MouseArea { + id: mouseArea + objectName: "mouseArea" + anchors.fill: parent + hoverEnabled: false + onPressed: function(mouse) { mouse.accepted = false } + } +} + diff --git a/tests/auto/quick/qquickmousearea/tst_qquickmousearea.cpp b/tests/auto/quick/qquickmousearea/tst_qquickmousearea.cpp index 345b42487e..44aa28f3f9 100644 --- a/tests/auto/quick/qquickmousearea/tst_qquickmousearea.cpp +++ b/tests/auto/quick/qquickmousearea/tst_qquickmousearea.cpp @@ -137,6 +137,7 @@ private slots: void negativeZStackingOrder(); void containsMouseAndVisibility(); void containsMouseAndVisibilityMasked(); + void containsMouseAndHoverDisabled(); void doubleClickToHide(); void releaseFirstTouchAfterSecond(); #if QT_CONFIG(tabletevent) @@ -2453,6 +2454,20 @@ void tst_QQuickMouseArea::containsMouseAndVisibilityMasked() QTRY_VERIFY(!mouseArea2->hovered()); } +// QTBUG-110594 +void tst_QQuickMouseArea::containsMouseAndHoverDisabled() +{ + QQuickView window; + QVERIFY(QQuickTest::showView(window, testFileUrl("containsMouseAndHoverDisabled.qml"))); + + QQuickMouseArea *mouseArea = window.rootObject()->findChild<QQuickMouseArea *>("mouseArea"); + QVERIFY(mouseArea != nullptr); + QVERIFY(!mouseArea->hoverEnabled()); + + QTest::mousePress(&window, Qt::LeftButton, Qt::NoModifier, QPoint(100, 100)); + QTRY_VERIFY(!mouseArea->hovered()); +} + // QTBUG-35995 and QTBUG-102158 void tst_QQuickMouseArea::doubleClickToHide() { |