Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Listmodel seft destroy on scrolling then crash app



  • 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


  • Moderators

    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



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


  • Moderators

    @RyuShai it's a possibility



  • @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


  • Moderators

    @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


Log in to reply