Check if the data has been changed in QSqlTableModel
-
Hello everyone!
I know that this question has been touched a lot, but I still wasn't able to find the proper solution.
My case is as follows: I have theQSqlTableModel
and I am using edit strategyQSqlTableModel::OnManualSubmit
. The data is being changed with theQDataWidgetMapper
, the submit policy is set toQDataWidgetMapper::ManualSubmit
I would like to be able to tell if the data of currently selected row has been changed in order to signal to the user.I tried with connecting to signal
QAbstractItemModel::dataChanged(...)
(that would be the optimal solution) but signal is never emitted. Likewise checkingQSqlTableModel::isDirty()
with the timer does not work as wanted: when checking for the value I am getting always false until I manually submit and then always true afterwards.What would be the proper way of checking if the data was changed here? Maybe I should disable
QSqlTableModel::OnManualSubmit
orQDataWidgetMapper::ManualSubmit
? I am not sure if the both are needed here. -
Did you created customized SqlTableModel and overrided this function:
virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) ???/
-
Thank you for your replies.
@Slawomir_Piernikowski Here I am using original QSqlTableModel.
@VRonin That has given me the clue, to set the
QDataWidgetMapper
toAutoSubmit
andQSqlTableModel
toOnManualSubmit
so that all the changes are applied to the model. Of course right now thedataChanged
will be emitted, but I have a curious case:after I submit the changes to the model:
QSqlTableModel model = database_->neededModel(); model->database().transaction(); if (model->submitAll()) // actually submits to the model model->database().commit(); else { model->database().rollback(); QMessageBox::warning(this, tr("Cached Table"), tr("The database reported an error: %1") .arg(model->lastError().text())); }
afterwards the signal will not be emitted anymore. No changes can be changed be saved anymore. What may be the reason? I submit changed to database exactly like in example "Cached Table".
EDIT: Ok, I get it now: the model is repopulated, so the
QDataWidgetMapper
loses the selected index and no changes will be recognized until I select index again. This makes it more complicated than it was when I was just cachingQDataWidgetMapper
(setting it's Submit Policy to ManualSubmit). Is there any way to make it any easier as by checking if there are any changes in cachedQDataWidgetMapper
instead of the model? I can't see any function likeisDirty()
forQDataWidgetMapper
.