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

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



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



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

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

    I really appreciate your help


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.