Important: Please read the 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: .

    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;
    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;

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

    const short type;

    RosterElement *parentItem;
    QList<RosterElement*> children;


    class RosterModel : public QAbstractItemModel

    friend class Roster;

    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);


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

    QList<RosterElement*> *list;
    ColumnHeader *header;

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

    @class Roster
    Roster(ColumnHeader *header);

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

    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