I have the PK of the row in a table which needs to be edited. I want to display a form that will allow the user to edit the one record, when the user presses the <Save> button, the information is updated in the database.

There shouldn't be any change on the DB if no data was actually changed. This can be achieved by either only enabling the <Save> button when a change to the data has happened or by simply doing nothing when the <Save> button is pressed, which ever requires the least amount of code.

There is on catch, though. There is an audit field that contains the last modified user id. Aka the id of the user that is making the change, this needs to be set correctly for the update.

Is it possible to use the QSqlTableModel and QDataWidgetMapper to do this and minimize the amount of code I have to write?

I have wired up the construction of the form using both the model and the mapper and data appears correctly! The problem is on the save, nothing is ever saved to the database.

The last three lines of the constructor are:

Qt Code:
  1. _model->setFilter(QString("PARISHNER_ID = %1").arg(parishionerId));
  2. _model->select();
  3. _mapper->toFirst();
To copy to clipboard, switch view to plain text mode 

Currently I am trying the approach of the Save button is always enabled and I look to see if the model has changed to do the submit:

Qt Code:
  1. bool isDirty = false;
  2. for(int idx = 0; isDirty == false && idx < _model->columnCount(); idx++)
  3. isDirty += _model->isDirty(_model->index(0, idx));
  4.  
  5. if(isDirty)
  6. {
  7. QModelIndex modelIndex = _model->index(0, _model->fieldIndex("UPDATEDBY"));
  8. _model->setData(modelIndex, _parishionerId);
  9. _model->submit();
  10. }
To copy to clipboard, switch view to plain text mode 
The problem is that isDirty ALWAYS gets set immediately and the _model->submit() doesn't actually do anything.

Any thoughts?

Sam