Fix hover after press event
authorFrederik Gladhorn <frederik.gladhorn@digia.com>
Fri, 26 Apr 2013 14:18:54 +0000 (16:18 +0200)
committerThe Qt Project <gerrit-noreply@qt-project.org>
Sat, 27 Apr 2013 18:36:57 +0000 (20:36 +0200)
When a mouse press event was rejected,
we would unconditionally cancel the hover
and claim that the mouse is not contained any more.

Instead check if the mouse left and only then cancel
the hover state.

Task-number: QTBUG-30783

Change-Id: I5fac6f3a1f2807ef03e07982c603492d40d2a249
Reviewed-by: Florian Boucault <florian@boucault.net>
Reviewed-by: Alan Alpert <aalpert@blackberry.com>

src/quick/items/qquickmousearea.cpp
tests/auto/quick/qquickmousearea/data/hoverAfterPress.qml [new file with mode: 0644]
tests/auto/quick/qquickmousearea/tst_qquickmousearea.cpp

index 324331f..a878658 100644 (file)
@@ -954,7 +954,8 @@ void QQuickMouseArea::ungrabMouse()
         emit canceled();
         emit pressedChanged();
         emit pressedButtonsChanged();
-        if (d->hovered) {
+
+        if (d->hovered && !isUnderMouse()) {
             d->hovered = false;
             emit hoveredChanged();
         }
diff --git a/tests/auto/quick/qquickmousearea/data/hoverAfterPress.qml b/tests/auto/quick/qquickmousearea/data/hoverAfterPress.qml
new file mode 100644 (file)
index 0000000..69ec8fb
--- /dev/null
@@ -0,0 +1,29 @@
+import QtQuick 2.0
+
+Item {
+    width: 500
+    height: 500
+
+    Rectangle {
+        width: 300
+        height: 300
+        color: "grey"
+        x: 100
+        y: 100
+
+        MouseArea {
+            id: mouseArea
+            objectName: "mouseArea"
+            anchors.fill: parent
+            hoverEnabled: true
+            onPressed: mouse.accepted = false
+            //onContainsMouseChanged: print("containsMouse changed =", containsMouse)
+
+            Rectangle {
+                visible: parent.containsMouse
+                color: "red"
+                width: 10; height: 10
+            }
+        }
+    }
+}
index a959a60..a582c62 100644 (file)
@@ -78,6 +78,7 @@ private slots:
     void hoverPosition();
     void hoverPropagation();
     void hoverVisible();
+    void hoverAfterPress();
     void disableAfterPress();
     void onWheel();
     void transformedMouseArea_data();
@@ -1007,6 +1008,33 @@ void tst_QQuickMouseArea::hoverVisible()
     delete window;
 }
 
+void tst_QQuickMouseArea::hoverAfterPress()
+{
+    QQuickView *window = createView();
+    window->setSource(testFileUrl("hoverAfterPress.qml"));
+
+    QQuickItem *root = window->rootObject();
+    QVERIFY(root != 0);
+
+    QQuickMouseArea *mouseArea = window->rootObject()->findChild<QQuickMouseArea*>("mouseArea");
+    QVERIFY(mouseArea != 0);
+    QTest::mouseMove(window, QPoint(22,33));
+    QCOMPARE(mouseArea->hovered(), false);
+    QTest::mouseMove(window, QPoint(200,200));
+    QCOMPARE(mouseArea->hovered(), true);
+    QTest::mouseMove(window, QPoint(22,33));
+    QCOMPARE(mouseArea->hovered(), false);
+    QTest::mouseMove(window, QPoint(200,200));
+    QCOMPARE(mouseArea->hovered(), true);
+    QTest::mousePress(window, Qt::LeftButton, Qt::NoModifier, QPoint(200,200));
+    QCOMPARE(mouseArea->hovered(), true);
+    QTest::mouseRelease(window, Qt::LeftButton, Qt::NoModifier, QPoint(200,200));
+    QCOMPARE(mouseArea->hovered(), true);
+    QTest::mouseMove(window, QPoint(22,33));
+    QCOMPARE(mouseArea->hovered(), false);
+    delete window;
+}
+
 void tst_QQuickMouseArea::disableAfterPress()
 {
     QQuickView *window = createView();