Don't use QStandardItemModel. If you've got an id, then store it the internalId of the QModelIndex and use QAbstractItemModel.
Qt Model / View framework, tree view
-
27-11-2021 - |
Frage
I’m experimenting with Qt and the Model/View framework.
I have some data that I would like to display and edit in a tree view.
I have read the documentation and this tutorial, but I’m still unsure about what is the best practice in my case.
The data comes from a server and looks something like this:
int id;
int parentId;
QString name;
float price;
The id’s are “running” numbers and all unique so the data might look like this.
Id =1
parentId =0
name= food
id = 5
parentId =1
name = pizza
The id is of no interest to the end user and should not be displayed.
Getting this in to a hierarchy should be pretty straight forward using a recursive function. (But any ideas here are also most welcome)
- Should I put the data I get from the server straight in to a
QStandardItemModel
? - Is there any point in creating my own class and inherit
QStandardItemModel/Item
? - Would each field (id, name, price) be it’s own
QStandardItem
?
If yes on #3, When I edit the item that contains a name, how do I get the id connected to that name so I can post the change back to the server.
As you understand these are basics, a sketch of a recommended structure would be great.
Lösung