summaryrefslogtreecommitdiffstats
path: root/src/quick3d/quick3d/qt3dquick_global.cpp
diff options
context:
space:
mode:
authorSean Harmer <sean.harmer@kdab.com>2016-05-20 10:31:30 +0100
committerSean Harmer <sean.harmer@kdab.com>2016-05-23 16:32:01 +0000
commit9e07d3ae8f5b90b84735b3b8b81fa6c749a646b2 (patch)
tree8cdf07d8129ddf49f451faa5897a2379534ae66e /src/quick3d/quick3d/qt3dquick_global.cpp
parent073930f2ef030b3019c323999d910185f4639d9b (diff)
Add hook to set QNode parent when creating a QNode in QML
Just need to be a little careful in QNode::setParent() and the helper in case the qml engine sets a QObject parent but QNode doesn't know about it yet. Task-number: QTBUG-47055 Change-Id: I30269bd230ee6eacd2816282fd48f879cfb7c83a Reviewed-by: Kevin Ottens <kevin.ottens@kdab.com>
Diffstat (limited to 'src/quick3d/quick3d/qt3dquick_global.cpp')
-rw-r--r--src/quick3d/quick3d/qt3dquick_global.cpp27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/quick3d/quick3d/qt3dquick_global.cpp b/src/quick3d/quick3d/qt3dquick_global.cpp
index 78523aa4a..f25aae268 100644
--- a/src/quick3d/quick3d/qt3dquick_global.cpp
+++ b/src/quick3d/quick3d/qt3dquick_global.cpp
@@ -669,12 +669,39 @@ static Quick3DColorProvider *getColorProvider()
return &colorProvider;
}
+static QQmlPrivate::AutoParentResult qquick3ditem_autoParent(QObject *obj, QObject *parent)
+{
+ // When setting a parent (especially during dynamic object creation) in QML,
+ // also try to set up the analogous item/window relationship.
+ auto parentNode = qmlobject_cast<Qt3DCore::QNode *>(parent);
+ if (parentNode) {
+ auto node = qmlobject_cast<Qt3DCore::QNode *>(obj);
+ if (node) {
+ // A QNode has another QNode child
+ node->setParent(parentNode);
+ return QQmlPrivate::Parented;
+ }
+ } else {
+ return QQmlPrivate::IncompatibleParent;
+ }
+ return QQmlPrivate::IncompatibleObject;
+}
+
void Quick3D_initialize()
{
Qt3DCore::Quick::Quick3DValueTypes::registerValueTypes();
QQml_addValueTypeProvider(getValueTypeProvider());
QQml_setColorProvider(getColorProvider());
QAbstractNodeFactory::registerNodeFactory(QuickNodeFactory::instance());
+
+ // Register a hook called when we do component.create() that sets the
+ // parent. We need this as QObject::setParent() is insufficient to propagate
+ // the arbiter and scene to the children (see QNode::setParent(QNode *).
+ // TODO: Replace this with virtual void QObjectPrivate::setParent(QObject *)
+ // that can be called from QObject ctor and QObject::setParent(). That would
+ // allow removal of this hook here and in QtQuick.
+ QQmlPrivate::RegisterAutoParent autoparent = { 0, &qquick3ditem_autoParent };
+ QQmlPrivate::qmlregister(QQmlPrivate::AutoParentRegistration, &autoparent);
}
void Quick3D_registerType(const char *className, const char *quickName, int major, int minor)