How can I store an object in a QTableWidget so I can access it later? Maybe if the object is a model?
-
In MFC one could store data in a CListBox together with the text to be displayed so one could access this data (any object really) during program execution... I need to do the same in a QTableWidget... What is the most natural way? The data I want to store is the row contents of the widget... is it easy to create a simple model to encapsulate this vector of rows?
Can someone point me on how to create this model?
Thanks!!
Juan
-
@jdent said in How can I store an object in a QTableWidget so I can access it later? Maybe if the object is a model?:
I need to do the same in a QTableWidget
That might be difficult. I think you have to use
QTableView
and implement the model + a proper delegate yourself.If you understand correctly, you want to store objects like
CListBox
(don't know what this exactly is) including the data they are currently displaying in a table?!
IsCListBox
more likeQComboBox
or more likeQLineEdit
?The whole approach seems weird. Wouldn't it be better to store the display data right away in the table model and then link the widgets to it somehow?!
Why you want to store the widgets as "data" in your model?Can you give a example how your "data" looks like and what you think it should look when storing it?
Check out
QStyleItemDelegate
for displaying custom model items.
-
I don't know much about Qt's model concept. If I understand you correctly, you want to have a data object for each cell in the table. In this case, I always created a subclass of the class (here, for example, QTableWidgetItem) that has a reference to a data object. I then inserted an object from this subclass into the QTableWidget. If I then wanted to work with a QTableWidgetItem from the table, I only had to cast to the subclass to access the data.
-
@Pl45m4 My data is a vector of tuples all of identical structure. How hard is it to make a model out of this?
I have a JoinedGridDisplayer that currently accepts the vector and displays it in the QTableWidget - or could be QTableView.So the question is should I create a model class to encapsulate this vector or should I derive from QTableWidgetItem so it references the rows/cols of the vector?
-
@jdent
QTableWidget is a "convenience" QTableView with its own model.The QTableWidget class provides an item-based table view with a default model.
You seem to have your own model?
If you want a table that uses your own data model you should use QTableView rather than this class.
If you want to use your data structure directly derive from QAbstractItemModel (or QAbstractTableModel.). In principle you only need to do https://doc.qt.io/qt-6/qabstractitemmodel.html#subclassing:
When subclassing QAbstractItemModel, at the very least you must implement index(), parent(), rowCount(), columnCount(), and data(). These functions are used in all read-only models, and form the basis of editable models.
Just 5 simple methods for a read-only model you can then show in a
QTreeView
. A few more if you want it editable. It's concise and easy to implement. -
Without going into whether this is appropriate use or not, an easy solution is to store the data using a role that the delegate won't render.
Eg:
QAbstractItemModel *model = tableWidget.model(); QModelIndex index = tableWidget.currentIndex().; QVariant data = getData(); model->setData(index, data, ItemDataRole::UserRole)