Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Listmodel seft destroy on scrolling then crash app
Qt 6.12 LTS is out! See what's new in the release blog

Listmodel seft destroy on scrolling then crash app

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 2 Posters 1.1k Views
  • 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.
  • R Offline
    R Offline
    RyuShai
    wrote on last edited by RyuShai
    #1

    Hi, im try make move item from listViewA to listViewB. But when i'm copied, sometime listmodel self-destroyed and make my app crash.
    Code is complicated so i cant push code right now, see error on video i recorded here video

    i create custom model like this

    class Q_PersonInfoModel : public QAbstractListModel
    {
        Q_OBJECT
        Q_PROPERTY(ListPersonInfo *lpi READ lpi WRITE setlpi)
    public:
        Q_PersonInfoModel(QObject *parent = nullptr);
        ~Q_PersonInfoModel();
        // QAbstractItemModel interface
    public:
        enum {
            ID = 0,
            NAME,
            S3_FRAME_URL,
            S3_IMAGE_URL,
            CREATE_DATE,
            UPDATE_DATE
        };
        virtual int rowCount(const QModelIndex &parent) const override;
        virtual QVariant data(const QModelIndex &index, int role) const override;
        virtual bool setData(const QModelIndex &index, const QVariant &value, int role) override;
        virtual QHash<int, QByteArray> roleNames() const override;
        ListPersonInfo *lpi()const;
        void setlpi (ListPersonInfo *mlpi);
    private:
        ListPersonInfo *mLpi;
    

    data save on class ListPersonInfo like this

    class ListPersonInfo : public QObject
    {
        Q_OBJECT
    public:
        explicit ListPersonInfo(QObject *parent = nullptr);
    //    static ListPersonInfo *getInstance();
        void addItem(PersonInfo item);
        void removeItem(int pos);
        QList<PersonInfo> getData() ;
        ~ListPersonInfo();
        void setData(const QList<PersonInfo> &data);
    
    signals:
        void preItemAppended();
        void postItemAppended();
    
        void preItemRemoved(int index);
        void postItemRemoved();
    
        void preResetItem();
        void posResetItem();
    
        void modelChanged(QString stct);
    
    public slots:
    private:
        QList<PersonInfo> mData;
        int count=0;
    //    static ListPersonInfo *instance;
    };
    

    assign model to customModel by call Q_INVOKABLE method return ListPersonInfo

    ListPersonView.qml
    ....
     listPersonModel.lpi: controler.getListPersonInfo(index)
    

    here the method getListPersonInfo(positionInList)

    ListPersonInfo *MainControler::getListPersonInfo(int pos){
        DB("pos: "<<pos);
    
        return groupCluster[pos];
    }
    

    declare groupCluster here

    QList<ListPersonInfo *> groupCluster;
    

    any idea why it destroy and how should i do for right ?

    UPDATE:
    root cause may is parent of model your for listview custom model (ListPersonInfo in this case)
    i use QQmlEngine::setObjectOwnership(); then no more crash hehe

    J.HilkJ 1 Reply Last reply
    0
    • R RyuShai

      Hi, im try make move item from listViewA to listViewB. But when i'm copied, sometime listmodel self-destroyed and make my app crash.
      Code is complicated so i cant push code right now, see error on video i recorded here video

      i create custom model like this

      class Q_PersonInfoModel : public QAbstractListModel
      {
          Q_OBJECT
          Q_PROPERTY(ListPersonInfo *lpi READ lpi WRITE setlpi)
      public:
          Q_PersonInfoModel(QObject *parent = nullptr);
          ~Q_PersonInfoModel();
          // QAbstractItemModel interface
      public:
          enum {
              ID = 0,
              NAME,
              S3_FRAME_URL,
              S3_IMAGE_URL,
              CREATE_DATE,
              UPDATE_DATE
          };
          virtual int rowCount(const QModelIndex &parent) const override;
          virtual QVariant data(const QModelIndex &index, int role) const override;
          virtual bool setData(const QModelIndex &index, const QVariant &value, int role) override;
          virtual QHash<int, QByteArray> roleNames() const override;
          ListPersonInfo *lpi()const;
          void setlpi (ListPersonInfo *mlpi);
      private:
          ListPersonInfo *mLpi;
      

      data save on class ListPersonInfo like this

      class ListPersonInfo : public QObject
      {
          Q_OBJECT
      public:
          explicit ListPersonInfo(QObject *parent = nullptr);
      //    static ListPersonInfo *getInstance();
          void addItem(PersonInfo item);
          void removeItem(int pos);
          QList<PersonInfo> getData() ;
          ~ListPersonInfo();
          void setData(const QList<PersonInfo> &data);
      
      signals:
          void preItemAppended();
          void postItemAppended();
      
          void preItemRemoved(int index);
          void postItemRemoved();
      
          void preResetItem();
          void posResetItem();
      
          void modelChanged(QString stct);
      
      public slots:
      private:
          QList<PersonInfo> mData;
          int count=0;
      //    static ListPersonInfo *instance;
      };
      

      assign model to customModel by call Q_INVOKABLE method return ListPersonInfo

      ListPersonView.qml
      ....
       listPersonModel.lpi: controler.getListPersonInfo(index)
      

      here the method getListPersonInfo(positionInList)

      ListPersonInfo *MainControler::getListPersonInfo(int pos){
          DB("pos: "<<pos);
      
          return groupCluster[pos];
      }
      

      declare groupCluster here

      QList<ListPersonInfo *> groupCluster;
      

      any idea why it destroy and how should i do for right ?

      UPDATE:
      root cause may is parent of model your for listview custom model (ListPersonInfo in this case)
      i use QQmlEngine::setObjectOwnership(); then no more crash hehe

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      hi @RyuShai

      I don't see the implementation so I have to ask, your ListPersonInfoobjectes do you give them your c++ class as parent?

      If they are without c++ parent the QML-Enging should take ownership of them and the GC may or may not delete them at random


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      R 1 Reply Last reply
      0
      • J.HilkJ J.Hilk

        hi @RyuShai

        I don't see the implementation so I have to ask, your ListPersonInfoobjectes do you give them your c++ class as parent?

        If they are without c++ parent the QML-Enging should take ownership of them and the GC may or may not delete them at random

        R Offline
        R Offline
        RyuShai
        wrote on last edited by
        #3

        @J.Hilk said in Listmodel seft destroy on scrolling then crash app:

        s do you give them your c++ cla

        i dont set ListPersonInfo parent, i set it default = 0
        ListPersonInfo.h

        explicit ListPersonInfo(QObject *parent = 0);
        

        ListPersonInfo.cpp

        ListPersonInfo::ListPersonInfo(QObject *parent) : QObject(parent)
        {
        
        }
        

        so parent = 0 is problem do you think ?

        J.HilkJ 1 Reply Last reply
        0
        • R RyuShai

          @J.Hilk said in Listmodel seft destroy on scrolling then crash app:

          s do you give them your c++ cla

          i dont set ListPersonInfo parent, i set it default = 0
          ListPersonInfo.h

          explicit ListPersonInfo(QObject *parent = 0);
          

          ListPersonInfo.cpp

          ListPersonInfo::ListPersonInfo(QObject *parent) : QObject(parent)
          {
          
          }
          

          so parent = 0 is problem do you think ?

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @RyuShai it's a possibility


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          R 1 Reply Last reply
          0
          • J.HilkJ J.Hilk

            @RyuShai it's a possibility

            R Offline
            R Offline
            RyuShai
            wrote on last edited by
            #5

            @J.Hilk
            i think you right, just found solution on stackoverflow with QQmlEngine::setObjectOwnership();

            however, still dont understand your idea, which parent should i give ListPersonInfo when create new one

            J.HilkJ 1 Reply Last reply
            0
            • R RyuShai

              @J.Hilk
              i think you right, just found solution on stackoverflow with QQmlEngine::setObjectOwnership();

              however, still dont understand your idea, which parent should i give ListPersonInfo when create new one

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #6

              @RyuShai said in Listmodel seft destroy on scrolling then crash app:

              @J.Hilk
              however, still dont understand your idea, which parent should i give ListPersonInfo when create new one

              probably controler (no idea what the actual c++ class is named)

              99% chance, you can simply give this as parent during creation of the ListPersonInfo object


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              0

              • Login

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