QML introduces a separate ownership (QObject
) tree from the visual tree (QtQuick scene graph).
parent
returns the visual parent.children
returns the visual children.data
returns the QObject
children.But how do I access the QObject
parent?
FYI, I intend on dynamically creating and destroying nested layouts containing a custom type, so I need to manage the QObject
tree - just using the visual tree will result in the 'wrong' objects disappearing after reparenting and then deleting.
If you don't actually know the parent of the item, I don't think you can find out without going into C++. If you just want to change the item's QObject
parent, you should be able to do so in QML by adding it to the data
property of its new parent. (This will also change its visual parent, if applicable.)
Quoting the the Qt Quick Concepts documentation:
Any object assigned to an item's data property becomes a child of the item within its QObject hierarchy, for memory management purposes. Additionally, if an object added to the data property is of the Item type, it is also assigned to the Item::children property and becomes a child of the item within the visual scene hierarchy. (Most Qt Quick hierarchy crawling algorithms, especially the rendering algorithms, only consider the visual parent hierarchy.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With