Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. ListModel deleteing model Object ?
Forum Updated to NodeBB v4.3 + New Features

ListModel deleteing model Object ?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
2 Posts 1 Posters 258 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    poncho524
    wrote on last edited by poncho524
    #1

    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 ??

    1 Reply Last reply
    0
    • P Offline
      P Offline
      poncho524
      wrote on last edited by poncho524
      #2

      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.

      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved