'QAbstractItemModel::columnCount - variable column count for each row
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
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.
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.
Solution 1:[1]
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.
Solution 2:[2]
No, it is not possible for different children of the same parent to have different column count. At least with the standard QTreeView.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Joseph Larson |
| Solution 2 | Hedede |

