Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Data store children and Model (QAbstractItemModel subclass) synchronization
Forum Updated to NodeBB v4.3 + New Features

Data store children and Model (QAbstractItemModel subclass) synchronization

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 2.7k 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.
  • D Offline
    D Offline
    Danielc
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved