Congratulations to our 2022 Qt Champions!

I can't update my items in QAbstractListModel

  • 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

    COMPILER:MINGW 32bit on Windows 7

  • Qt Champions 2017

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

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

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

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

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

  • @Eeli-K ok that's my code:

    Header File:

    class LayerManagerModel : public QAbstractListModel
        LayerManagerModel(QObject *parent = 0);
        // QAbstractItemModel interface
        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);
        QList<LayerInformation> m_layerInformationList;
        enum LayerManagerModelRoles {
            NameRole = Qt::UserRole + 1,

    Update function implementation:

    void LayerManagerModel::updateVisibility(int index,int visibilitStatus)
    	LayerInformation layerInfo =;
    	LayerInformation layerInfo =;
        //    m_layerInformationList.replace(index,layerInfo);
        //    setData(qm ,visibilitStatus,VisibleRole);
        emit dataChanged(QModelIndex(),QModelIndex());

    Thanks for your attention

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

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

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


    I really appreciate your help

Log in to reply