Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Check if the data has been changed in QSqlTableModel
Forum Updated to NodeBB v4.3 + New Features

Check if the data has been changed in QSqlTableModel

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 1.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jendker
    wrote on last edited by
    #1

    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.

    VRoninV 1 Reply Last reply
    0
    • J Jendker

      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.

      VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      @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.

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      4
      • S Offline
        S Offline
        Slawomir_Piernikowski
        wrote on last edited by
        #3

        Did you created customized SqlTableModel and overrided this function:

        virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) ???/

        1 Reply Last reply
        3
        • J Offline
          J Offline
          Jendker
          wrote on last edited by Jendker
          #4

          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.

          1 Reply Last reply
          0
          • J Offline
            J Offline
            Jendker
            wrote on last edited by
            #5

            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 :)

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved