Tree View with varying column counts
-
I would like to show in a QTreeView a model that has two levels of items. The child and parent items would have a different qty of columns and different column names. The parent items will all have 4 columns. Each parent item would have several child items with 5 columns that have no relation to the parent's columns.
Below is a quick sketch of roughly how I would want it to appear. The parent columns show information about shipments between warehouses, the child items represent actual lines of inventory that make up that shipment. In this example "Shipment #3" and Shipment #5" are expanded to show child items.
-Can this be achieved with a QTreeView?
-Could I use a standard model, or do I need to create my own?
-Do you know of any other similar examples on how I might implement this, or have any suggestions? -
@hieuht
Just to get you going, if nobody provides a more specific example.A tree model/view allows for any number of columns varying per parent per int QAbstractItemModel::columnCount(const QModelIndex &parent = QModelIndex()) const. So your case of all children having 5 columns is permissible. I don't about your horizontal header for each parent-children, that is not supported, you would presumably need some custom delegate to handle that.
I think you would need a
QTreeView
and your own model, because for aQTreeWidget
you would not have access to the model to alter the column counts per parent. -
Hi @JonB,
Thank you for your reply. I'm using
QTreeView
and I can set the number of column by columCount method. But it is quite limited.First, the child's count cannot more than the parent's count.
Second, the child's column size depend on the parent's size like the following picture.How can I overcome these limitations? Could you provide an example of using delegate to handle this? Thank you :D
-
@hieuht said in Tree View with varying column counts:
First, the child's count cannot more than the parent's count.
Hmm, I did not see that mentioned. Indeed https://doc.qt.io/qt-6/qabstractitemmodel.html#columnCount says:
In most subclasses, the number of columns is independent of the parent.
Maybe it's a limitation on
QTreeView
rather than on the model?Second, the child's column size depend on the parent's size like the following picture.
It looks like the child is only really using the parent's existing columns, just (according to you) it can only use the same number or fewer. Understandable, but not what your picture shows.
Could you provide an example of using delegate to handle this?
Quite beyond me! This is not just a simple delegate, it's virtually the drawing of a complete new table.
@SGaist sometimes picks out something which KDE has added in https://api.kde.org/frameworks/index.html. You might look through that, maybe there is something there for you?