QObject itself supports children, then why is that the feature is not supported in QML?

Seems like you have to use at least Item if you want to create object trees, and that's huge overhead and waste for non-visual elements which have nothing to do with UI whatsoever.

Is there a way to enable QObject's already existing functionality to build object trees for use in QML?

]]>https://forum.qt.io/topic/52208/why-qtobject-and-derived-can-t-have-childrenRSS for NodeThu, 24 May 2018 19:25:05 GMTFri, 13 Mar 2015 14:10:44 GMT60QObject itself supports children, then why is that the feature is not supported in QML?

Seems like you have to use at least Item if you want to create object trees, and that's huge overhead and waste for non-visual elements which have nothing to do with UI whatsoever.

Is there a way to enable QObject's already existing functionality to build object trees for use in QML?

]]>https://forum.qt.io/post/265216https://forum.qt.io/post/265216Fri, 13 Mar 2015 14:10:44 GMTYou can have children the problem is that QtQbject by default doesn't have a default property. I will say that a QObject does have children so its a bit odd that it doesn't out of the box. Even though you can add a default property, there is still a bug that you cannot make a default property of a list so if you want to pull this off do

]]>https://forum.qt.io/post/265218https://forum.qt.io/post/265218Fri, 13 Mar 2015 14:18:21 GMT@Buttink QObject already has a QList<QObject*> children - I would much prefer to use that instead of having another.
]]>https://forum.qt.io/post/265221https://forum.qt.io/post/265221Fri, 13 Mar 2015 14:25:24 GMT@iter Then I think your only option would be to subclass QObject in C++ and add the "Q_CLASSINFO("DefaultProperty", "children")" required for Qml. You could try to get this added as a enhancement for Qt. I would kind of like this.
]]>https://forum.qt.io/post/265224https://forum.qt.io/post/265224Fri, 13 Mar 2015 14:32:28 GMTThere are two different parent/child concepts which are exposed in QtQuick applications. You can see the documentation about this at http://doc.qt.io/qt-5/qtquick-visualcanvas-topic.html#visual-parent but I will expand below:

The first is QObject parent/child, which is about object ownership. The semantics of this are identical to the semantics of normal Qt QObject parent/child semantics. Unfortunately, this is not exposed directly in QML as a Q_PROPERTY. When you call setParent(QObject*) in C++, you are defining the QObject parent/child relationship.

The second is QQuickItem parent/child, which involves two things: 1) QObject child, 2) scenegraph node child. The Q_PROPERTY associated with this is called "parent" and the associated C++ getter/setter is parentItem() and setParentItem().

That is, in QML code, when you do: someItem.parent = someOtherItem;
The C++-equivalent code is: someItem.setParentItem(someOtherItem);

This is one of the things about QtQuick which I've always disliked, honestly.
If you wish to set just QObject parent, you need to write a small Q_INVOKABLE function which exposes that functionality (perhaps in a Singleton type) like: