QAbstractItemModel::columnCount - variable column count for each row

809 views Asked by At

We are implementing an QAbstractItemModel structure as shown in diagram below and would like that;

  • node A to contains 3 data columns
  • node B to contains 8 data columns
  • node C to contains 5 data columns

enter image description here

However, when looking at the signature for QAbstractItemModel::columnCount it is unclear how to implement the above requirement. It is difficult to determine that node A shall have 3 columns, node B shall have 8 columns and node C shall have 5 columns give only the parent index.

int QAbstractItemModel::columnCount(const QModelIndex &parent = QModelIndex()) const

Returns the number of columns for the children of the given parent.

In most subclasses, the number of columns is independent of the parent.

QAbstractItemModel::columnCount

This type of problem must surely be possible to solve using QAbstractItemModel. What am I doing wrong or not thinking about? Appreciate any input on the matter.

2

There are 2 answers

0
Hedede On

No, it is not possible for different children of the same parent to have different column count. At least with the standard QTreeView.

2
Joseph Larson On

I don't know if this will help you, but I implemented a Tree Model to figure out how to write models. It's at my github: https://github.com/jplflyer/qt-TreeViewDemo

To address your specific question: You're passed a QModelIndex object, and you have to figure out what data that is pointing to and use that to determine the number of children.

You already should have code that takes a QModelIndex and returns the column data, so you can tap into the same code to know what to return.