diff options
author | Shawn Rutledge <shawn.rutledge@qt.io> | 2018-11-22 14:00:39 +0100 |
---|---|---|
committer | Shawn Rutledge <shawn.rutledge@qt.io> | 2018-12-07 20:45:10 +0000 |
commit | ee2ac69595d3f854f7bf7158e600091f2675fa18 (patch) | |
tree | 50ef6ecedadd96ac1c563e65f16f28dd1ded2943 /tests/auto/quick/pointerhandlers/qquickpointerhandler/tst_qquickpointerhandler.cpp | |
parent | 1cd3b2acfeb041ef54fe546b07a5e9efb5a2099b (diff) |
Add dynamically-created Event Handlers to the relevant handlers vector
If any kind of Pointer Handler is created dynamically in JS by
calling Component.createObject(), QObject::setParent() is called
rather than passing the parent to the constructor, so
QQuickItemPrivate::data_append() did not take care of adding the
handler to QQuickItemPrivate's extra->pointerHandlers vector.
We need to use the auto-parent mechanism (just as we did with
handling dynamic creation of nested Windows in
8cb02e23abbefc9d020707fc1a2d8b6eb4e103b6). Added
QQuickItemPrivate::addPointerHandler() to put the prepend()
and implied setAcceptedMouseButtons() in one place.
Fixes: QTBUG-71427
Change-Id: I3be3dd033c1c89e6e5b5c3463e1a720bbe963281
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
Diffstat (limited to 'tests/auto/quick/pointerhandlers/qquickpointerhandler/tst_qquickpointerhandler.cpp')
-rw-r--r-- | tests/auto/quick/pointerhandlers/qquickpointerhandler/tst_qquickpointerhandler.cpp | 65 |
1 files changed, 61 insertions, 4 deletions
diff --git a/tests/auto/quick/pointerhandlers/qquickpointerhandler/tst_qquickpointerhandler.cpp b/tests/auto/quick/pointerhandlers/qquickpointerhandler/tst_qquickpointerhandler.cpp index e8b076052a..906cbf63e4 100644 --- a/tests/auto/quick/pointerhandlers/qquickpointerhandler/tst_qquickpointerhandler.cpp +++ b/tests/auto/quick/pointerhandlers/qquickpointerhandler/tst_qquickpointerhandler.cpp @@ -182,12 +182,18 @@ public: class EventHandler : public QQuickPointerHandler { +public: void handlePointerEventImpl(QQuickPointerEvent *event) override { QQuickPointerHandler::handlePointerEventImpl(event); if (!enabled()) return; - EventItem *item = static_cast<EventItem *>(target()); + ++eventCount; + EventItem *item = qmlobject_cast<EventItem *>(target()); + if (!item) { + event->point(0)->setGrabberPointerHandler(this); + return; + } qCDebug(lcPointerTests) << item->objectName() << event; int c = event->pointCount(); for (int i = 0; i < c; ++i) { @@ -206,10 +212,13 @@ class EventHandler : public QQuickPointerHandler void onGrabChanged(QQuickPointerHandler *, QQuickEventPoint::GrabTransition stateChange, QQuickEventPoint *point) override { - EventItem *item = static_cast<EventItem *>(target()); - item->eventList.append(Event(Event::HandlerDestination, QEvent::None, - static_cast<Qt::TouchPointState>(point->state()), stateChange, eventPos(point), point->scenePosition())); + EventItem *item = qmlobject_cast<EventItem *>(target()); + if (item) + item->eventList.append(Event(Event::HandlerDestination, QEvent::None, + static_cast<Qt::TouchPointState>(point->state()), stateChange, eventPos(point), point->scenePosition())); } + + int eventCount = 0; }; class tst_PointerHandlers : public QQmlDataTest @@ -228,6 +237,8 @@ private slots: void mouseEventDelivery(); void touchReleaseOutside_data(); void touchReleaseOutside(); + void dynamicCreation(); + void dynamicCreationInWindow(); protected: bool eventFilter(QObject *, QEvent *event) @@ -593,6 +604,52 @@ void tst_PointerHandlers::touchReleaseOutside() QCOMPARE_EVENT(endIndexToTest, endDestination, endType, endState, endGrabState); } +void tst_PointerHandlers::dynamicCreation() +{ + QScopedPointer<QQuickView> windowPtr; + createView(windowPtr, "dynamicallyCreated.qml"); + QQuickView * window = windowPtr.data(); + + EventItem *eventItem1 = window->rootObject()->findChild<EventItem*>("eventItem1"); + QVERIFY(eventItem1); + EventHandler *handler = window->rootObject()->findChild<EventHandler*>("eventHandler"); + QVERIFY(handler); + + QCOMPARE(handler->parentItem(), eventItem1); + QCOMPARE(handler->target(), eventItem1); + + QPoint p1(20, 20); + QTest::mousePress(window, Qt::LeftButton, Qt::NoModifier, p1); + QTRY_COMPARE(eventItem1->eventList.size(), 2); + QCOMPARE_EVENT(0, Event::HandlerDestination, QEvent::Pointer, Qt::TouchPointPressed, NoGrab); + QCOMPARE_EVENT(1, Event::MouseDestination, QEvent::MouseButtonPress, Qt::TouchPointPressed, NoGrab); + QTest::mouseRelease(window, Qt::LeftButton, Qt::NoModifier, p1); +} + +void tst_PointerHandlers::dynamicCreationInWindow() +{ + QQmlEngine engine; + QQmlComponent component(&engine); + component.loadUrl(testFileUrl("dynamicallyCreatedInWindow.qml")); + QQuickWindow *window = qobject_cast<QQuickWindow*>(component.create()); + QScopedPointer<QQuickWindow> cleanup(window); + QVERIFY(window); + window->show(); + QVERIFY(QTest::qWaitForWindowExposed(window)); + + EventHandler *handler = window->contentItem()->findChild<EventHandler*>("eventHandler"); + QVERIFY(handler); + + QCOMPARE(handler->parentItem(), window->contentItem()); + QCOMPARE(handler->target(), window->contentItem()); + + QPoint p1(20, 20); + QTest::mousePress(window, Qt::LeftButton, Qt::NoModifier, p1); + QTRY_COMPARE(handler->eventCount, 1); + QTest::mouseRelease(window, Qt::LeftButton, Qt::NoModifier, p1); + QTRY_COMPARE(handler->eventCount, 2); +} + QTEST_MAIN(tst_PointerHandlers) #include "tst_qquickpointerhandler.moc" |