Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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 the QSqlTableModel and I am using edit strategy QSqlTableModel::OnManualSubmit. The data is being changed with the QDataWidgetMapper, the submit policy is set to QDataWidgetMapper::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 checking QSqlTableModel::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 or QDataWidgetMapper::ManualSubmit? I am not sure if the both are needed here.



  • @Jendker said in Check if the data has been changed in QSqlTableModel:

    but signal is never emitted

    It is. If your view updates then it is.



  • 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 to AutoSubmit and QSqlTableModel to OnManualSubmit so that all the changes are applied to the model. Of course right now the dataChanged 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 caching QDataWidgetMapper (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 cached QDataWidgetMapper instead of the model? I can't see any function like isDirty() for QDataWidgetMapper.



  • Please let me come back to this topic.

    I guess there is no better way than just to reload the row in QDataWidgetMapper every time after database submit. This closes this thread, thank you for your help! I am marking it as solved :)


Log in to reply