0

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.

LarsA
  • 595
  • 2
  • 6
  • 14
  • `QAbstractItemModel` is an adapter design pattern. So if you need help you should explain what kind of data you have and what to show in a `QTreeView`. Then we can give you an advice how to implement each parts of adapter `QAbstractItemModel`. In a `QTreeeView` number of columns remains constant. – Marek R Mar 21 '22 at 09:51

2 Answers2

1

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

Hedede
  • 1,133
  • 13
  • 27
0

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.

Joseph Larson
  • 8,530
  • 1
  • 19
  • 36
  • In the QAbstractItemModel::data(..) method we are provided the QModelIndex to the actual data. In this scenario I feel we have all the information needed to return the correct data. However, in the QAbstractItemModel::columnCount(..) method we are provided with the QModelIndex to the parent. In this scenario I struggle to understand when I should return 3 (node A) or 8 (node B) or 5 (node C). – LarsA Mar 18 '21 at 08:04
  • I had to do a lot of experimenting. Perhaps for now return a constant and do some debug output. See what the index really points to. – Joseph Larson Mar 18 '21 at 13:49