diff options
author | Shawn Rutledge <shawn.rutledge@qt.io> | 2021-12-09 08:06:18 +0100 |
---|---|---|
committer | Shawn Rutledge <shawn.rutledge@qt.io> | 2021-12-10 06:52:35 +0100 |
commit | 8326ff2ac1472415b82734946435d3e7e75d522b (patch) | |
tree | ccafa1912fb16d092ca1645975ca11324cba550d | |
parent | cd5d62e9997c71e2105d571f681777c5a59c3ef1 (diff) |
Instantiator: don't interfere with delegates that assign parents
[ChangeLog][QtQml][Instantiator] Instantiator now avoids re-assigning a
delegate object's parent to itself if it was already set; thus, you can
now declare a parent assignment.
Task-number: QTBUG-64546
Task-number: QTBUG-84730
Change-Id: I7d95fa76e71c363b4cb5b7a512c2e984488c8af4
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
-rw-r--r-- | examples/quick/pointerhandlers/pointerhandlers.qml | 11 | ||||
-rw-r--r-- | src/qmlmodels/qqmlinstantiator.cpp | 3 | ||||
-rw-r--r-- | tests/auto/qml/qqmlinstantiator/data/handlerWithParent.qml | 12 | ||||
-rw-r--r-- | tests/auto/qml/qqmlinstantiator/tst_qqmlinstantiator.cpp | 15 |
4 files changed, 33 insertions, 8 deletions
diff --git a/examples/quick/pointerhandlers/pointerhandlers.qml b/examples/quick/pointerhandlers/pointerhandlers.qml index a5cbbbd56a..db33b961cb 100644 --- a/examples/quick/pointerhandlers/pointerhandlers.qml +++ b/examples/quick/pointerhandlers/pointerhandlers.qml @@ -83,13 +83,10 @@ Rectangle { z: 10000 anchors.fill: parent - // TODO use Instantiator to create these... but we need to be able to set their parents to glassPane somehow (QTBUG-64546) - TouchpointFeedbackSprite { } - TouchpointFeedbackSprite { } - TouchpointFeedbackSprite { } - TouchpointFeedbackSprite { } - TouchpointFeedbackSprite { } - TouchpointFeedbackSprite { } + Instantiator { + model: 10 + delegate: TouchpointFeedbackSprite { parent: glassPane } + } MouseFeedbackSprite { } } diff --git a/src/qmlmodels/qqmlinstantiator.cpp b/src/qmlmodels/qqmlinstantiator.cpp index cb7ca1126c..d7b70d20d6 100644 --- a/src/qmlmodels/qqmlinstantiator.cpp +++ b/src/qmlmodels/qqmlinstantiator.cpp @@ -128,7 +128,8 @@ void QQmlInstantiatorPrivate::_q_createdItem(int idx, QObject* item) return; if (requestedIndex != idx) // Asynchronous creation, reference the object (void)instanceModel->object(idx); - item->setParent(q); + if (!item->parent()) + item->setParent(q); if (objects.size() < idx + 1) { int modelCount = instanceModel->count(); if (objects.capacity() < modelCount) diff --git a/tests/auto/qml/qqmlinstantiator/data/handlerWithParent.qml b/tests/auto/qml/qqmlinstantiator/data/handlerWithParent.qml new file mode 100644 index 0000000000..a032b19efc --- /dev/null +++ b/tests/auto/qml/qqmlinstantiator/data/handlerWithParent.qml @@ -0,0 +1,12 @@ +import QtQuick + +Item { + id: root + Instantiator { + model: 2 + delegate: PointHandler { + objectName: "pointHandler" + parent: root + } + } +} diff --git a/tests/auto/qml/qqmlinstantiator/tst_qqmlinstantiator.cpp b/tests/auto/qml/qqmlinstantiator/tst_qqmlinstantiator.cpp index 4c6c84a9d2..a0ab2cda5b 100644 --- a/tests/auto/qml/qqmlinstantiator/tst_qqmlinstantiator.cpp +++ b/tests/auto/qml/qqmlinstantiator/tst_qqmlinstantiator.cpp @@ -56,6 +56,8 @@ private slots: void asynchronous_data(); void asynchronous(); + + void handlerWithParent(); }; tst_qqmlinstantiator::tst_qqmlinstantiator() @@ -276,6 +278,19 @@ void tst_qqmlinstantiator::asynchronous() } } +void tst_qqmlinstantiator::handlerWithParent() +{ + QQmlEngine engine; + QQmlComponent component(&engine, testFileUrl("handlerWithParent.qml")); + QObject *rootObject = component.create(); + QVERIFY(rootObject != nullptr); + const auto handlers = rootObject->findChildren<QObject *>("pointHandler"); + QCOMPARE(handlers.count(), 2); + for (const auto *h : handlers) { + QCOMPARE(h->parent(), rootObject); + } +} + QTEST_MAIN(tst_qqmlinstantiator) #include "tst_qqmlinstantiator.moc" |