diff options
author | Paul Lemire <paul.lemire@kdab.com> | 2018-07-11 15:50:10 +0200 |
---|---|---|
committer | Paul Lemire <paul.lemire@kdab.com> | 2018-07-18 07:46:28 +0000 |
commit | 5376853866e49b5a6841c6c41affef5a3d3d4bb5 (patch) | |
tree | d9460391f5811c75947ad9ba0bb9b0c308b29f95 | |
parent | c39e20dcc693b525f2ee2259e864487d664c3856 (diff) |
QNode: _q_postConstructorInit set scene on whole subtree
_q_postConstrutorInit is called in a deferred invocation when constructing a
Node with a parent. If several nodes are created that way, the
_q_postConstructorInit call from the node which parents the other will
actually build the node creation changes for the whole subtree and incidently
prevent the _q_postConstructorInit invocation for the children to do anything
(including setting the scene). Therefore _q_postConstructorInit should set the
scene on all the Node it will be creating creation changes for.
Change-Id: I07d4e80675758b9701cc941881f1faaa1c89af4b
Task-number: QTBUG-69352
Reviewed-by: Svenn-Arne Dragly <svenn-arne.dragly@qt.io>
Reviewed-by: Sean Harmer <sean.harmer@kdab.com>
-rw-r--r-- | src/core/nodes/qnode.cpp | 11 | ||||
-rw-r--r-- | tests/auto/core/nodes/tst_nodes.cpp | 49 |
2 files changed, 57 insertions, 3 deletions
diff --git a/src/core/nodes/qnode.cpp b/src/core/nodes/qnode.cpp index ff7a1b2ce..9c069f936 100644 --- a/src/core/nodes/qnode.cpp +++ b/src/core/nodes/qnode.cpp @@ -188,9 +188,14 @@ void QNodePrivate::_q_postConstructorInit() if (!parentNode) return; - - if (m_scene) - m_scene->addObservable(q); // Sets the m_changeArbiter to that of the scene + // Set the scene on this node and all children it references so that all + // children have a scene set since notifyCreationChanges will set + // m_hasBackendNode to true for all children, which would prevent them from + // ever having their scene set + if (m_scene) { + QNodeVisitor visitor; + visitor.traverse(q, parentNode->d_func(), &QNodePrivate::setSceneHelper); + } // Let the backend know we have been added to the scene notifyCreationChange(); diff --git a/tests/auto/core/nodes/tst_nodes.cpp b/tests/auto/core/nodes/tst_nodes.cpp index d81a229cc..75d7a7799 100644 --- a/tests/auto/core/nodes/tst_nodes.cpp +++ b/tests/auto/core/nodes/tst_nodes.cpp @@ -83,6 +83,7 @@ private slots: void checkConstructionSetParentMix(); // QTBUG-60612 void checkConstructionWithParent(); void checkConstructionAsListElement(); + void checkSceneIsSetOnConstructionWithParent(); // QTBUG-69352 void appendingComponentToEntity(); void appendingParentlessComponentToEntity(); @@ -1162,6 +1163,52 @@ void tst_Nodes::checkConstructionAsListElement() QCOMPARE(newChildEvent->addedNodeId(), node->id()); } +void tst_Nodes::checkSceneIsSetOnConstructionWithParent() +{ + // GIVEN + ObserverSpy spy; + Qt3DCore::QScene scene; + QScopedPointer<MyQNode> root(new MyQNode()); + + // WHEN + root->setArbiterAndScene(&spy, &scene); + root->setSimulateBackendCreated(true); + + // THEN + QVERIFY(Qt3DCore::QNodePrivate::get(root.data())->scene() != nullptr); + + // WHEN + Qt3DCore::QEntity *subTreeRoot = new Qt3DCore::QEntity(root.data()); + + QVector<Qt3DCore::QEntity *> children; + QVector<Qt3DCore::QComponent *> cmps; + children.reserve(5); + cmps.reserve(5); + for (int i = 0; i < 5; ++i) { + const auto cmp = new MyQComponent(subTreeRoot); + cmps << cmp; + children << new Qt3DCore::QEntity(cmp); + + // When cmp is full created, it will also send the creation change for its child entity + } + QCoreApplication::processEvents(); + QCOMPARE(root->children().count(), 1); + QCOMPARE(subTreeRoot->children().count(), 5); + QCOMPARE(spy.events.size(), 12); // 1 subTreeRoot creation change, 5 child creation, 5 cmp creation, 1 child added (subTree to root) + + + spy.events.clear(); + + // Add component in a separate loop to ensure components are added after + // entities have been fully created + for (int i = 0; i < 5; ++i) { + children.at(i)->addComponent(cmps.at(i)); + } + + // THEN + QCOMPARE(spy.events.size(), 10); // 5 QComponentAddedChange(entity, cmp) and 5 QComponentAddedChange(cmp, entity) +} + void tst_Nodes::appendingParentlessComponentToEntity() { // GIVEN @@ -1588,6 +1635,8 @@ void tst_Nodes::checkTrackedPropertyNamesUpdate() } + + QTEST_MAIN(tst_Nodes) #include "tst_nodes.moc" |