Working with a QTableView
and QAbstractTableModel
- when the model emits a dataChanged
event for the cell being edited, the string the user has typed in the cell (but not pressed enter to 'commit' the edit) is erased.
Example: Click a cell, type '123', cell is still in edit mode waiting for more text, dataChanged
is emitted and the '123' is erased, leaving an empty cell in edit mode.
Does anyone know how to stop this behaviour, or how the model can detect when the cell is being edited to prevent dataChanged
events being raised for that cell?
I had the same problem and found an approach without writing my own Delegate:
The problem is exactly how you described it: The data gets updated in the Background and everything you Edit gets cleared out because the dataChanged Event updates all values thus calling the data function, which returns an empty QVariant() Object if nothing is specified for the Qt::EditRole. Even Leonid's answer would always overwrite your edits with the same QString("Text to Edit").
So what I did was this:
Introduce a member variable and dafine it mutable so it can be changed by the const data function:
In your background data updating function, check for m_update date before emmitting the dataChanged Signal:
In your data function, check for the edit role and set m_updateData to false:
After the Edit is finished, the setData function is called, where you update the data in your model. Reset m_updateDate to true after you have done that.
This works perfectly for me :)
I had the same problem. The thing is, that
data()
function is called with differentrole
parameter. For displayingrole==Qt::DisplayRole
and while editing it is called withrole==Qt::EditRole
. For example try changingto
that should do the trick
I think that you should use
dataChanged
event only for indexes that are not being edited or only forQt::ItemDataRole::DisplayRole
. E.g. to update only second column for every row use:Check your model class, you should override the setData method in your model. If every thing is correct it will update model after editing data... please let me know if you have another implementation