Use QStandardItemModel with QTreeView
-
https://doc.qt.io/qt-5/qstandarditemmodel.html#details - there is a short example on how to build a tree, so yes it's possible to use it in a QTreeView.
See also https://doc.qt.io/qt-5/qstandarditem.html#appendRow and https://doc.qt.io/qt-5/qstandarditem.html#appendRow-1 -
@Christian-Ehrlicher so bad incomplete example. :-(
-
@Alexey-Serebryakov said in Use QStandardItemModel with QTreeView:
@Christian-Ehrlicher so bad incomplete example. :-(
There's a complete example linked in that documentation.
-
@Alexey-Serebryakov In which case? There a tree is build up exactly what you requested. There is also a link to a more complete example below: https://doc.qt.io/qt-5/qtwidgets-itemviews-simpletreemodel-example.html
-
@Christian-Ehrlicher so bad and old example... Also I cant find a good simple example for my task.
-
@Alexey-Serebryakov
Googling forQStandardItemModel QTreeView
comes up with a number of other hits, have you looked through them to see if one fits you better -
@Alexey-Serebryakov said in Use QStandardItemModel with QTreeView:
so bad and old example.
Sorry but this example is exactly what you want, it doesn't matter when the code was written. You're just to lazy to try to understand it.
-
@Christian-Ehrlicher Hmm... I'm sorry but I already ask this forum. May be you can explain me.
I have already some tree structured data storage something like QMap<QString, QList<QString>> where key of map is parent items and values of map is children. How can I implement my model to view in QTreeView? May be I need to use QAbstractItemModel?That is very simple data storage but I cant find an any good example ot tutorials, in Qt Examples too.
Thanks a lot.
-
@Alexey-Serebryakov
If you useQStandardItemModel
/QStandardItem
s you will be copying your data items into the model. If you already have your own data structures,QAbstractItemModel
can be used to supply an interface to your existing data. -
I agree with Alexey that the example is incomplete. I am fumbling with setupModelData() and how to connect the model to a QTreeView. The example would be better if code for those two points were included.
-
Christian Ehrlicher Lifetime Qt Championreplied to Scherer Tech on last edited by Christian Ehrlicher
@Scherer-Tech said in Use QStandardItemModel with QTreeView:
how to connect the model to a QTreeView
See https://doc.qt.io/qt-6/qabstractitemview.html#setModel and https://doc.qt.io/qt-6/model-view-programming.html