From 9e07d3ae8f5b90b84735b3b8b81fa6c749a646b2 Mon Sep 17 00:00:00 2001 From: Sean Harmer Date: Fri, 20 May 2016 10:31:30 +0100 Subject: 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 --- src/quick3d/quick3d/qt3dquick_global.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'src/quick3d/quick3d/qt3dquick_global.cpp') 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(parent); + if (parentNode) { + auto node = qmlobject_cast(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) -- cgit v1.2.3