Results 1 to 2 of 2

Thread: Understanding QSqlTableModel/QDataWidgetMapper when editing one record...

  1. #1
    Join Date
    Jun 2010
    Location
    Cincinnati, Ohio, USA
    Posts
    92
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Question Understanding QSqlTableModel/QDataWidgetMapper when editing one record...

    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

  2. #2
    Join Date
    Jun 2010
    Location
    Cincinnati, Ohio, USA
    Posts
    92
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Lightbulb Re: Understanding QSqlTableModel/QDataWidgetMapper when editing one record...

    Ok, looks like I figured it out how to get it to submit, it was a bug on my part:

    I first added a qDebug() statement after the submit and it gave me nothing:

    Qt Code:
    1. _model->submit();
    2. qDebug() << "lasterror: " << _model->lastError().text();
    To copy to clipboard, switch view to plain text mode 

    After stepping into the sumbit code, I saw there was an if condition in the QSqlTableModel::submit() function checking the strategy of the model, which was failing so the submit did nothing. When I changed the code to call submitAll() it worked. Then I changed the editStrategy to OnRowChange and it works fine.

    Ok, as far as the dirty flag goes, I found a way to not have to worry about it, fore my concern was setting UPDATEDBY field. What I did was simply connected to the model's beforeUpdate signal and set the UPDATEDBY field there, once the model has determined there is something to change.

    Sam
    Last edited by scarleton; 4th October 2010 at 01:01.

Similar Threads

  1. Understanding RGB888
    By scarleton in forum Qt Programming
    Replies: 6
    Last Post: 29th August 2010, 20:03
  2. Replies: 3
    Last Post: 4th August 2010, 18:51
  3. Replies: 3
    Last Post: 26th March 2010, 04:32
  4. deleting record (row) from QSqlTableModel
    By schnitzel in forum Qt Programming
    Replies: 3
    Last Post: 13th February 2010, 21:48
  5. Replies: 5
    Last Post: 18th July 2006, 22:31

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.