PDA

View Full Version : QDataWidgetMapper doesn't always update field in table



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

marcvanriet
30th October 2011, 22:40
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

norobro
31st October 2011, 01:42
Found this (https://bugreports.qt.nokia.com/browse/QTBUG-1086) 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.

marcvanriet
23rd November 2011, 00:19
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.

marcvanriet
14th December 2011, 12:57
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 :
7175

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