Why QtObject and derived can't have children?
-
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?
-
You 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
QtObject { property list<QtObject> children default property alias data: children }
Or you can just make properties for your items I see this with a "App" element
QtObject { id: app property SomeService someService: SomeService { } }
-
@Buttink QObject already has a
QList<QObject*> children
- I would much prefer to use that instead of having another. -
@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.
-
There 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:Q_INVOKABLE void setQObjectParent(QObject *child, QObject *parent) { child->setParent(parent); }
I hope this helps!
-
If you wish, you can.
What you mean is setting some default array values inside a qml document.
-
@Buttink said in Why QtObject and derived can't have children?:
@iter Then I think your only option would be to subclass QObject in C++ and add the "Q_CLASSINFO("DefaultProperty", "children")" required for Qml.
That sounded great, but I tried it, and it didn't work. I'm still getting "Cannot assign to non-existent default property". Any idea?