ListModel deleteing model Object ?
-
I ran into a bizarre problem where i'm creating a QObject that has a Q_PROPERTY of a QList<QObject*>.
i'm passing the QObject* to qml for a list view model, and then the object gets deleted even though nothing is calling "delete" on the object. for example, I create new MyObject without a parent, and then when the following code is implemented, the MyObject instance gets deleted. If i set the model to "null" then the object doesn't get deleted.class MyObject : public QObject { Q_OBJECT Q_PROPERTY(QList<QObject*> list READ list NOTIFY listChanged)
class MyQmlInterface : public QObject{ [...] public slots: QObject* getMyObject(); // returns a parentless MyObject
ListView { model: MyQmlInterface.getMyObject().list
if i give MyObject instance a parent then it doesnt get deleted. Is QML deleting objects without parents ??
-
i'm thinking it comes down to this..
https://doc.qt.io/qt-5/qtqml-cppintegration-data.html
when a QObject is returned from an explicit C++ method call [...] the QML engine assumes ownership of the object.
Additionally, the QML engine respects the normal QObject parent ownership semantics of Qt C++ objects, and will never delete a QObject instance which has a parent.So if the engine assumes ownership, then in C++land you can't assume that object is good anymore. So before returning any QObject to QML, make sure the parent is set.