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