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. I can't update my items in QAbstractListModel
Qt 6.11 is out! See what's new in the release blog

I can't update my items in QAbstractListModel

Scheduled Pinned Locked Moved Solved QML and Qt Quick
10 Posts 3 Posters 7.6k Views 1 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.
  • AlienA Offline
    AlienA Offline
    Alien
    wrote on last edited by
    #1

    Hi,
    I implemented a class which inherited from QAbstractListModel I don't have any problem with inserting an items or removing them (The ListView will update correctly)the problem is that I can't update the items inside the list my ListView doesn't update whatsoever.

    Note:I don't want to remove the item and then insert it again in order to simulate the update behavior.

    I would appreciate any ideas

    SDK:QT5.9.0
    COMPILER:MINGW 32bit on Windows 7

    E 1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Moderators Qt Champions 2024 Qt Champions 2022 Qt Champions 2017
      wrote on last edited by
      #2

      Did you get a chance to look at the ObjectList example given in the Qt Installation Example directory ?. It may help you.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      https://www.pthinks.com

      AlienA 1 Reply Last reply
      1
      • AlienA Alien

        Hi,
        I implemented a class which inherited from QAbstractListModel I don't have any problem with inserting an items or removing them (The ListView will update correctly)the problem is that I can't update the items inside the list my ListView doesn't update whatsoever.

        Note:I don't want to remove the item and then insert it again in order to simulate the update behavior.

        I would appreciate any ideas

        SDK:QT5.9.0
        COMPILER:MINGW 32bit on Windows 7

        E Offline
        E Offline
        Eeli K
        wrote on last edited by
        #3

        @Alien What have you tried so far? dataChanged signal?

        AlienA 1 Reply Last reply
        1
        • dheerendraD dheerendra

          Did you get a chance to look at the ObjectList example given in the Qt Installation Example directory ?. It may help you.

          AlienA Offline
          AlienA Offline
          Alien
          wrote on last edited by
          #4

          Dear @dheerendra ,
          No where is it I just have a review of AbstractItemModel example

          1 Reply Last reply
          0
          • E Eeli K

            @Alien What have you tried so far? dataChanged signal?

            AlienA Offline
            AlienA Offline
            Alien
            wrote on last edited by Alien
            #5

            Dear @Eeli-K ,
            I just want to update my model so I expect that ListView will be aware of the change, I used

            emit dataChanged()
            

            after I've changed model's data with no hope.
            When I remove the item and insert it again the item will be updated of course I use some pair functions like ( beginInsertRows,endInsertRows) and (beginRemoveRows,endRemoveRows) , but as you know this is simulate the update behavior(Not a real update).
            Extra information:I use QList in my model for keeping data and for updating the items I use replace function of QList .

            E 1 Reply Last reply
            0
            • AlienA Alien

              Dear @Eeli-K ,
              I just want to update my model so I expect that ListView will be aware of the change, I used

              emit dataChanged()
              

              after I've changed model's data with no hope.
              When I remove the item and insert it again the item will be updated of course I use some pair functions like ( beginInsertRows,endInsertRows) and (beginRemoveRows,endRemoveRows) , but as you know this is simulate the update behavior(Not a real update).
              Extra information:I use QList in my model for keeping data and for updating the items I use replace function of QList .

              E Offline
              E Offline
              Eeli K
              wrote on last edited by
              #6

              @Alien Can you show the code which actually changes the data and emits dataChanged?

              AlienA 1 Reply Last reply
              0
              • E Eeli K

                @Alien Can you show the code which actually changes the data and emits dataChanged?

                AlienA Offline
                AlienA Offline
                Alien
                wrote on last edited by Alien
                #7

                @Eeli-K ok that's my code:

                Header File:

                class LayerManagerModel : public QAbstractListModel
                {
                    Q_OBJECT
                public:
                    LayerManagerModel(QObject *parent = 0);
                    ~LayerManagerModel();
                    // QAbstractItemModel interface
                public:
                    int rowCount(const QModelIndex & parent = QModelIndex()) const override;
                    bool setData(const QModelIndex &index, const QVariant &value, int role) override;
                    QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
                    Qt::ItemFlags flags(const QModelIndex &index) const override;
                    QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override;
                    //    bool insertRows(int row, int count, const QModelIndex &parent=QModelIndex()) override;
                    //    bool removeRows(int row, int count, const QModelIndex &parent) override;
                    bool removeData(int row);
                    QHash<int, QByteArray> roleNames() const override;
                    bool addData(LayerInformation* layerInformation);
                    void updateVisibility(int index,int visibilitStatus);
                
                private:
                    QList<LayerInformation> m_layerInformationList;
                    enum LayerManagerModelRoles {
                        NameRole = Qt::UserRole + 1,
                        VisibleRole,
                        DateOfCreationRole,
                        DescriptionRole,
                        UserNameRole,
                        ZOrderRole,
                        FilePathRole
                    };
                };
                

                Update function implementation:

                void LayerManagerModel::updateVisibility(int index,int visibilitStatus)
                {
                /*    
                	LayerInformation layerInfo = m_layerInformationList.at(index);
                    layerInfo.setIsVisible((visibilitStatus==1)?"YES":"NO");
                    beginRemoveRows(QModelIndex(),index,index);
                    m_layerInformationList.removeAt(index);
                    endRemoveRows();
                    beginInsertRows(QModelIndex(),index,index);
                    m_layerInformationList.insert(index,layerInfo);
                    endInsertRows();
                	*/
                	LayerInformation layerInfo = m_layerInformationList.at(index);
                	layerInfo.setIsVisible((visibilitStatus==1)?"YES":"NO");
                    //    m_layerInformationList.replace(index,layerInfo);
                    //    setData(qm ,visibilitStatus,VisibleRole);
                	
                    emit dataChanged(QModelIndex(),QModelIndex());
                
                }
                

                Thanks for your attention

                E 2 Replies Last reply
                0
                • AlienA Alien

                  @Eeli-K ok that's my code:

                  Header File:

                  class LayerManagerModel : public QAbstractListModel
                  {
                      Q_OBJECT
                  public:
                      LayerManagerModel(QObject *parent = 0);
                      ~LayerManagerModel();
                      // QAbstractItemModel interface
                  public:
                      int rowCount(const QModelIndex & parent = QModelIndex()) const override;
                      bool setData(const QModelIndex &index, const QVariant &value, int role) override;
                      QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
                      Qt::ItemFlags flags(const QModelIndex &index) const override;
                      QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override;
                      //    bool insertRows(int row, int count, const QModelIndex &parent=QModelIndex()) override;
                      //    bool removeRows(int row, int count, const QModelIndex &parent) override;
                      bool removeData(int row);
                      QHash<int, QByteArray> roleNames() const override;
                      bool addData(LayerInformation* layerInformation);
                      void updateVisibility(int index,int visibilitStatus);
                  
                  private:
                      QList<LayerInformation> m_layerInformationList;
                      enum LayerManagerModelRoles {
                          NameRole = Qt::UserRole + 1,
                          VisibleRole,
                          DateOfCreationRole,
                          DescriptionRole,
                          UserNameRole,
                          ZOrderRole,
                          FilePathRole
                      };
                  };
                  

                  Update function implementation:

                  void LayerManagerModel::updateVisibility(int index,int visibilitStatus)
                  {
                  /*    
                  	LayerInformation layerInfo = m_layerInformationList.at(index);
                      layerInfo.setIsVisible((visibilitStatus==1)?"YES":"NO");
                      beginRemoveRows(QModelIndex(),index,index);
                      m_layerInformationList.removeAt(index);
                      endRemoveRows();
                      beginInsertRows(QModelIndex(),index,index);
                      m_layerInformationList.insert(index,layerInfo);
                      endInsertRows();
                  	*/
                  	LayerInformation layerInfo = m_layerInformationList.at(index);
                  	layerInfo.setIsVisible((visibilitStatus==1)?"YES":"NO");
                      //    m_layerInformationList.replace(index,layerInfo);
                      //    setData(qm ,visibilitStatus,VisibleRole);
                  	
                      emit dataChanged(QModelIndex(),QModelIndex());
                  
                  }
                  

                  Thanks for your attention

                  E Offline
                  E Offline
                  Eeli K
                  wrote on last edited by
                  #8

                  @Alien at() is for const reference, you have to use [] if you want to modify the returned item via the reference.

                  1 Reply Last reply
                  0
                  • AlienA Alien

                    @Eeli-K ok that's my code:

                    Header File:

                    class LayerManagerModel : public QAbstractListModel
                    {
                        Q_OBJECT
                    public:
                        LayerManagerModel(QObject *parent = 0);
                        ~LayerManagerModel();
                        // QAbstractItemModel interface
                    public:
                        int rowCount(const QModelIndex & parent = QModelIndex()) const override;
                        bool setData(const QModelIndex &index, const QVariant &value, int role) override;
                        QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
                        Qt::ItemFlags flags(const QModelIndex &index) const override;
                        QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override;
                        //    bool insertRows(int row, int count, const QModelIndex &parent=QModelIndex()) override;
                        //    bool removeRows(int row, int count, const QModelIndex &parent) override;
                        bool removeData(int row);
                        QHash<int, QByteArray> roleNames() const override;
                        bool addData(LayerInformation* layerInformation);
                        void updateVisibility(int index,int visibilitStatus);
                    
                    private:
                        QList<LayerInformation> m_layerInformationList;
                        enum LayerManagerModelRoles {
                            NameRole = Qt::UserRole + 1,
                            VisibleRole,
                            DateOfCreationRole,
                            DescriptionRole,
                            UserNameRole,
                            ZOrderRole,
                            FilePathRole
                        };
                    };
                    

                    Update function implementation:

                    void LayerManagerModel::updateVisibility(int index,int visibilitStatus)
                    {
                    /*    
                    	LayerInformation layerInfo = m_layerInformationList.at(index);
                        layerInfo.setIsVisible((visibilitStatus==1)?"YES":"NO");
                        beginRemoveRows(QModelIndex(),index,index);
                        m_layerInformationList.removeAt(index);
                        endRemoveRows();
                        beginInsertRows(QModelIndex(),index,index);
                        m_layerInformationList.insert(index,layerInfo);
                        endInsertRows();
                    	*/
                    	LayerInformation layerInfo = m_layerInformationList.at(index);
                    	layerInfo.setIsVisible((visibilitStatus==1)?"YES":"NO");
                        //    m_layerInformationList.replace(index,layerInfo);
                        //    setData(qm ,visibilitStatus,VisibleRole);
                    	
                        emit dataChanged(QModelIndex(),QModelIndex());
                    
                    }
                    

                    Thanks for your attention

                    E Offline
                    E Offline
                    Eeli K
                    wrote on last edited by
                    #9

                    @Alien And if your're worried about efficiency you should emit dataChanged(this->index(index),this->index(index)); so that the view doesn't go through all rows.

                    AlienA 1 Reply Last reply
                    2
                    • E Eeli K

                      @Alien And if your're worried about efficiency you should emit dataChanged(this->index(index),this->index(index)); so that the view doesn't go through all rows.

                      AlienA Offline
                      AlienA Offline
                      Alien
                      wrote on last edited by
                      #10

                      @Eeli-K ,
                      Thank you so much that was just because of emit

                      dataChanged(this->index(index),this->index(index));
                      

                      I really appreciate your help

                      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