Results 1 to 5 of 5

Thread: QDataWidgetMapper doesn't always update field in table

  1. #1
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QDataWidgetMapper doesn't always update field in table

    Hello,

    I have an ODBC (MS Access) database with a table containing a list of persons. This list is shown on a form in a table view. Also, different fields like name, nationality, ... of the selected person are shown in a lineEdit. A QDataWidgetMapper is used to map the lineEdits to the fields in the table.

    Below is part of my code (from the constructor of my form).

    Qt Code:
    1. ui->setupUi(this);
    2.  
    3. pModel = new QSqlTableModel(this); // use tablemodel for accessing person table
    4. pModel->setTable("Persons");
    5. pModel->setEditStrategy(QSqlTableModel::OnFieldChange);
    6. pModel->select();
    7.  
    8. ui->tblvPersons->setModel(pModel); // this is a QTableView
    9.  
    10. mapper = new QDataWidgetMapper(this); // mapper for mapping fields to controls
    11. mapper->setItemDelegate(new QSqlRelationalDelegate(this));
    12. mapper->setModel(pModel);
    13. mapper->setSubmitPolicy( QDataWidgetMapper::AutoSubmit );
    14.  
    15. //--- map the fields of the table to edit lines on the form
    16. for( int i=0; i<pModel->columnCount(); i++ )
    17. {
    18. QString sField = pModel->headerData(i,Qt::Horizontal).toString();
    19. if( sField=="FirstName" )
    20. mapper->addMapping(ui->leName, i );
    21. else if( sField=="Nationality" )
    22. mapper->addMapping(ui->leNationality, i);
    23. else if( sField=="Age" )
    24. mapper->addMapping(ui->leAge, i);
    25. }
    26.  
    27. // slots for when a different row is selected in the persons table
    28. connect(ui->tblvPersons->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)) , mapper, SLOT(setCurrentModelIndex(QModelIndex)));
    To copy to clipboard, switch view to plain text mode 

    Now the problem : when I modify one or more lineEdits, change to a different row in the table view, and then go back to the original record, the changes are not always written to the database. Sometimes it works, sometimes I have to retry 3 times just to change a single field.

    Am I doing something wrong ? I would think that with setting the subMitPolicy to AutoSubmit that I don't have to do a submit() or anything like it before changing to a different row.

    Any help would be appreciated.

    Best regards,
    Marc

  2. #2
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QDataWidgetMapper doesn't always update field in table

    Hi,

    A little progress here... It appeared that only the first field that I changed got updated.

    I fixed it by setting the setEditStrategy for the table model to onmanualsubmit. Then I connected a slot to all my edit fields so that, when they loose focus, the datawidgetmapper submit() method is called. Appearently the EditStrategy of the datawidgetmapper doesn't make any difference.

    Not really elegant, but it works...

    I wish there was a better solution, because I frequently use this kind of user interface where there is a table on the left for choosing a record, and on the right the different fields of a single record for editing.

    Best regards,
    Marc

  3. #3
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QDataWidgetMapper doesn't always update field in table

    Found this bug report that seems to describe your problem. As you'll see, it's been around a while and there are a couple of comments by the previous (recently) maintainer.

  4. The following user says thank you to norobro for this useful post:

    marcvanriet (31st October 2011)

  5. #4
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Unhappy Re: QDataWidgetMapper doesn't always update field in table (TBUG-1086)

    Is it normal for bugs like this to stay open for so long ? It doesn't seem like a trivial one too : you just can't use a QDatawidgetmapper with a qsqltablemodel.

    Question : what is everybody else doing when they make a form with fields to be filled in in an sql table ?

    I'm sure there must be a better way then manually coding all the 'update' queries for all the fields (which I used to do, but I'm looking for an easier way).

    Best regards,
    Marc

    P.S. By the way, the previous 'solution' of mine doesn't work. It's kind of weird. All the data appears to be saved : when you go to another record and back, all the new data is shown on the form. Everything seems fine too when you edit more records. BUT... NOTHING IS EVER WRITTEN TO THE DATABASE ! When I close the program and open it again, none of the changes are present.

  6. #5
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QDataWidgetMapper doesn't always update field in table (TBUG-1086)

    Hi,

    Can someone suggest me a CONVENIENT way to implement this :
    - I have a SQL table
    - I have a form to show and edit fields of a single row of the table
    - I select a row somewhere and then open the form to edit the fields, field values must be written back if changed, unless the user presses 'cancel'

    This is an example of my field form :
    field_form.gif

    Right now I write SQL queries manually to get the field values, see what fields are changed, and update the changed fields. There MUST be an easier way.

    QDatawidgetmapper unfortunately doesn't work properly with a QSqlTable because of QTBUG-1086.

    Best regards,
    Marc

Similar Threads

  1. How to update BLOB field in a SQLite database ?
    By aircraftstories in forum Qt Programming
    Replies: 6
    Last Post: 8th April 2011, 20:45
  2. still some problems on update databases using QDataWidgetMapper
    By xiongxiongchuan in forum Qt Programming
    Replies: 7
    Last Post: 8th July 2010, 18:39
  3. Replies: 2
    Last Post: 8th April 2010, 16:16
  4. SQL slow query - table update
    By lasher in forum Newbie
    Replies: 4
    Last Post: 21st October 2009, 23:12
  5. Replies: 8
    Last Post: 25th February 2008, 17:00

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.