The role of QStandardItems
-
Hi,
for a long time, I've stopped working on Qt projects and, again, I have to read lot about the basics.
I'm writing a small project that handles some financials and I need a QTableView, for which I subclassed QStandardItemModel.My basic and maybe stupid question is: Do I mandatory need QStandardItems inside the views cells (or inside the model) to handle with the data or is it possible to store data "directly" into the cells? I'm asking that because while reading the documentation I see that it is recommended to generate TreeViews and ListViews empty and appendRows/Columns with directly insering items, while it is recommended for TableViews to scale directly with int row & int column by constructing the model and inserting items with setItem.
Well, I created such a QTableView with an (empty?) QStandardItemModel with 10x10 matrix and as I can see, without setting items, it is possible to double click a cell and store data. So, I'm asking myself if:
a) there are QStandardItems generated by default for each index (cell) or
b) if it is generated by the delegate while typing into the cell or
c) there exists a way of directly storing data into the view (or model?) without having created corresponding itemsCan you help me with that, I don't find the answer in the docs, unfortunately.
Thank you very much for your help!
Kind regards
Binary91 -
Hi,
for a long time, I've stopped working on Qt projects and, again, I have to read lot about the basics.
I'm writing a small project that handles some financials and I need a QTableView, for which I subclassed QStandardItemModel.My basic and maybe stupid question is: Do I mandatory need QStandardItems inside the views cells (or inside the model) to handle with the data or is it possible to store data "directly" into the cells? I'm asking that because while reading the documentation I see that it is recommended to generate TreeViews and ListViews empty and appendRows/Columns with directly insering items, while it is recommended for TableViews to scale directly with int row & int column by constructing the model and inserting items with setItem.
Well, I created such a QTableView with an (empty?) QStandardItemModel with 10x10 matrix and as I can see, without setting items, it is possible to double click a cell and store data. So, I'm asking myself if:
a) there are QStandardItems generated by default for each index (cell) or
b) if it is generated by the delegate while typing into the cell or
c) there exists a way of directly storing data into the view (or model?) without having created corresponding itemsCan you help me with that, I don't find the answer in the docs, unfortunately.
Thank you very much for your help!
Kind regards
Binary91@Binary91 said in The role of QStandardItems:
b) if it is generated by the delegate while typing into the cell or
This one.
QStandardItemModelis just a convenience pre-supplied model which is generic. It lets you easily create data items of any type, and it provides storage for any of the other roles' item data if desired (like color or size of checkability). It provides the data storage. You can also still usedata()/setData()on the model if you don't want to go via "items".But if you have your own data storage/types already written then copying these to & from
QStandardItemModelcan be wasteful. There you might want to write your own to inherit fromQAbstractItemModeland operate directly on your own existing data for efficiency/type safety. -
Hi,
thank you for your reply!
Ok, so as the corresponding item object is created while using a delegate to store data into the views cells, I guess that there can't be data inside the view without a corresponding item object that holds it, right? I was just wondering why, in that case of behaviour, it is possible to retrive an item with a QModelIndex to an empty cell that never stored any data and that I never created an item by myself for..."But if you have your own data storage/types already written then copying these to & from QStandardItemModel can be wasteful. There you might want to write your own to inherit from QAbstractItemModel and operate directly on your own existing data for efficiency/type safety."
Yes, you're right, I think I'll have to do it this way in future projects.. Thank you!
-
Hi,
thank you for your reply!
Ok, so as the corresponding item object is created while using a delegate to store data into the views cells, I guess that there can't be data inside the view without a corresponding item object that holds it, right? I was just wondering why, in that case of behaviour, it is possible to retrive an item with a QModelIndex to an empty cell that never stored any data and that I never created an item by myself for..."But if you have your own data storage/types already written then copying these to & from QStandardItemModel can be wasteful. There you might want to write your own to inherit from QAbstractItemModel and operate directly on your own existing data for efficiency/type safety."
Yes, you're right, I think I'll have to do it this way in future projects.. Thank you!
-
Hi JonB,
sorry for the belated response. Ok, that sonds logical to me. So using the delegate is the only way of "automatically" creating "real items" that holds the data and no other containers exist in a QStandardItemModel that can hold data. That was what I needed to realize.Well, in your first response, you recommended to maybe create my own model by directly inheriting from QAbstractItemModel in case I'd have me own data storage or types. I'd like to work with table data that I will store in a SQL database and load it into the model when I need it. So far, I don't have specific item types, I'd try to work on the QStandardItem. Long time ago, I started a try of subclassing QAbstractItemModel and I realized that it is very hard work, especially handling inserting of new columns and stuff. I thought of QStandardItemModel to be such a basic subclass of it that already fits the generic stuff I need for my basic usage. Well, I'll maybe see it while using it...
Anyway, thank you for your help!
-
Hi JonB,
sorry for the belated response. Ok, that sonds logical to me. So using the delegate is the only way of "automatically" creating "real items" that holds the data and no other containers exist in a QStandardItemModel that can hold data. That was what I needed to realize.Well, in your first response, you recommended to maybe create my own model by directly inheriting from QAbstractItemModel in case I'd have me own data storage or types. I'd like to work with table data that I will store in a SQL database and load it into the model when I need it. So far, I don't have specific item types, I'd try to work on the QStandardItem. Long time ago, I started a try of subclassing QAbstractItemModel and I realized that it is very hard work, especially handling inserting of new columns and stuff. I thought of QStandardItemModel to be such a basic subclass of it that already fits the generic stuff I need for my basic usage. Well, I'll maybe see it while using it...
Anyway, thank you for your help!
@Binary91 said in The role of QStandardItems:
So using the delegate is the only way of "automatically" creating "real items" that holds the data and no other containers exist in a QStandardItemModel that can hold data.
There is nothing magical here. It's just that the
QTableView/Widgetediting delegate has QAbstractItemDelegate::setModelData() method. But you can always create/updateQStandardItems yourself viaQStandardItemModelmethods. The view simply makes whatever appropriate calls to the model, nothing you cannot do explicitly.I'd like to work with table data that I will store in a SQL database and load it into the model when I need it.
It would be simpler if you use Qt's
QSql...classes, including models and queries, rather than extracting stuff out to copy into abstract or standard item models.QTableViewstill works just as much with SQL models as item models.