Interact with editor widgets' data in QTableView
-
12-12-2019 - |
Frage
I'm using an editable QTableView + QStandardItemModel.
While editing a cell in a table view, I'd like to do something according to the new input data in this specific cell when committing the new data into the table view.
To do this, I need the new input data and the current model index (or column & row number). I tried some slots such as
virtual void closeEditor (QWidget * editor, QAbstractItemDelegate::EndEditHint hint)
and
virtual void commitData ( QWidget * editor ).
commitData seems to be what I need, however, the parameter is only the editor and I cannot figure out how to obtain the text in this editor widget. I looked QTextEdit but it's not a inherited class of QWidget.
I wonder if there's any way to obtain the data (text) and axis (column, row) of an editor widget?
Lösung
I suggest to implement your own item delegate, inheriting QStandardItemDelegate
(or QAbstractItemDelegate
). There you can override
void setModelData ( QWidget * editor, QAbstractItemModel * model, const QModelIndex & index ) const
Simply do you your custom processing and then call QStandardItemDelegate::setModelData(...)
to make sure that your model is updated with the newly edited data.
Andere Tipps
The itemChanged(QStandardItem*)
signal is emitted by a QStandardItemModel
whenever an item's data changes.
From the given QStandardItem
, you can retrieve the row and column directly. To get the displayed text, pass Qt::DisplayRole
to the item's data()
method.