marcvanriet
26th October 2011, 20:10
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).
ui->setupUi(this);
pModel = new QSqlTableModel(this); // use tablemodel for accessing person table
pModel->setTable("Persons");
pModel->setEditStrategy(QSqlTableModel::OnFieldChange);
pModel->select();
ui->tblvPersons->setModel(pModel); // this is a QTableView
mapper = new QDataWidgetMapper(this); // mapper for mapping fields to controls
mapper->setItemDelegate(new QSqlRelationalDelegate(this));
mapper->setModel(pModel);
mapper->setSubmitPolicy( QDataWidgetMapper::AutoSubmit );
//--- map the fields of the table to edit lines on the form
for( int i=0; i<pModel->columnCount(); i++ )
{
QString sField = pModel->headerData(i,Qt::Horizontal).toString();
if( sField=="FirstName" )
mapper->addMapping(ui->leName, i );
else if( sField=="Nationality" )
mapper->addMapping(ui->leNationality, i);
else if( sField=="Age" )
mapper->addMapping(ui->leAge, i);
}
// slots for when a different row is selected in the persons table
connect(ui->tblvPersons->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)) , mapper, SLOT(setCurrentModelIndex(QModelIndex)));
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
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).
ui->setupUi(this);
pModel = new QSqlTableModel(this); // use tablemodel for accessing person table
pModel->setTable("Persons");
pModel->setEditStrategy(QSqlTableModel::OnFieldChange);
pModel->select();
ui->tblvPersons->setModel(pModel); // this is a QTableView
mapper = new QDataWidgetMapper(this); // mapper for mapping fields to controls
mapper->setItemDelegate(new QSqlRelationalDelegate(this));
mapper->setModel(pModel);
mapper->setSubmitPolicy( QDataWidgetMapper::AutoSubmit );
//--- map the fields of the table to edit lines on the form
for( int i=0; i<pModel->columnCount(); i++ )
{
QString sField = pModel->headerData(i,Qt::Horizontal).toString();
if( sField=="FirstName" )
mapper->addMapping(ui->leName, i );
else if( sField=="Nationality" )
mapper->addMapping(ui->leNationality, i);
else if( sField=="Age" )
mapper->addMapping(ui->leAge, i);
}
// slots for when a different row is selected in the persons table
connect(ui->tblvPersons->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)) , mapper, SLOT(setCurrentModelIndex(QModelIndex)));
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