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

Data store children and Model (QAbstractItemModel subclass) synchronization



  • Hi everybody and thanks for reading.

    I'm working with QAbstractItemModel and QTreeView, and I need some advices to achieve a correct design.

    I've been reading the docs and I'm trying to follow this note: http://developer.qt.nokia.com/doc/qt-4.7/model-view-programming.html#note-13 .

    I need to represent different instances of some data in a view (with children). I have a collection with the base for the elements. Each instance (row) can have modified values of the base element

    @class RosterElement {
    friend class RosterModel;
    friend class Roster;
    public:
    RosterElement(RosterElement *_parent = 0);
    virtual ~RosterElement();

    int childCount() const;
    int row() const;
    RosterElement* child(int index) const;
    RosterElement* parent() const;
    
    void addChild(RosterElement *child);
    void removeChild(RosterElement *child);
    
    QString name;
    QString key;
    uint cost;
    uint size;
    

    protected:
    RosterElement(short _type, RosterElement *_parent = 0);

    private:
    const short type;

    RosterElement *parentItem;
    QList<RosterElement*> children;
    

    };

    class RosterModel : public QAbstractItemModel
    {
    Q_OBJECT

    friend class Roster;
    

    public:
    RosterModel(QList<RosterElement*> *_list, ColumnHeader *_header, QObject *parent = 0);

    QVariant data(const QModelIndex &index, int role) const;
    Qt::ItemFlags flags(const QModelIndex &index) const;
    QVariant headerData(int section, Qt::Orientation orientation,
                        int role = Qt::DisplayRole) const;
    int rowCount(const QModelIndex &parent = QModelIndex()) const;
    int columnCount(const QModelIndex &parent = QModelIndex()) const;
    QModelIndex index(int row, int column,
                      const QModelIndex &parent = QModelIndex()) const;
    QModelIndex parent(const QModelIndex &index) const;
    
    void emitDataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight);
    

    signals:

    public slots:
    void onColumnAboutToBeInserted();
    void onColumnInserted();
    void onColumnsAboutToBeCleared();
    void onColumnsCleared();

    private:
    QList<RosterElement*> *list;
    ColumnHeader *header;
    };@

    Following the advice, I created a class for the actual data store:

    @class Roster
    {
    public:
    Roster(ColumnHeader *header);
    ~Roster();

    RosterModel* model();
    
    void addRosterElement(RosterElement *element);
    void removeRosterElement(int row);
    void clearAll();
    
    int count();
    RosterElement* at(int i);
    RosterElement* value(int i);
    

    private:
    QList<RosterElement*> internalRoster;
    RosterModel *rosterModel;
    };@

    And I use addRosterElement() to add elements to the data store. This model needs to work with children and there is my problem. I need to add/remove children dynamically acording to some checkboxes (per instance), so I should notify the model about it. I can add a method like addChildrenToItem(RosterElement *element) in Roster class, but then I would have to look for the QModelIndex for that row, which means I have to iterate over the whole data store.

    So, could you give me an advice about how to achieve this properly?


Log in to reply