PDA

View Full Version : QDataWidgetMapper::submit() updates but not inserts a record



datanas
15th June 2012, 21:52
Hi Guys,

Here's the table:

CREATE TABLE IF NOT EXISTS PersonsTbl(
ID INTEGER NOT NULL AUTO_INCREMENT,
NAME VARCHAR(100) NOT NULL,
MOL VARCHAR(100),
BULSTAT_EGN NUMERIC(10) NOT NULL,
IDNUMDDS VARCHAR(11),
CONTACT VARCHAR(100),
FLAG NUMERIC(1) NOT NULL CHECK(FLAG IN (0, 1)),
TYPEID INTEGER NOT NULL,
CONSTRAINT PersonsTbl_PK PRIMARY KEY(ID));

This is the actual mapping of the fields:

...
orderClientMapper = new QDataWidgetMapper();
orderClientMapper->setSubmitPolicy(QDataWidgetMapper::AutoSubmit);
orderClientMapper->setModel(clientTableModel);
orderClientMapper->setItemDelegate(new QSqlRelationalDelegate());
orderClientMapper->addMapping(clientTypeComboBox, Person_Flag);
orderClientMapper->addMapping(clientNameEdit, Person_Name);
orderClientMapper->addMapping(clientMOLEdit, Person_MOL);
orderClientMapper->addMapping(clientBulstatEgnEdit, Person_ENG_Bulstat);
orderClientMapper->addMapping(clientIdNumDDSEdit, Person_IDNumDDS);
orderClientMapper->addMapping(clientContactEdit, Person_Contact);
...
orderClientMapper->toLast();

When Add button is clicked it should submit a new record but instead it updates the one record I had manually entered
in the DB, i.e. it does not autoincrement the PK.

Here's the actual submit:

int clientRow = orderClientMapper->currentIndex();
QSqlRecord record= clientTableModel->record(clientRow);
record.setValue(Person_Type, 1);
orderClientMapper->submit();
clientTableModel->insertRow(clientRow);

If you have any thoughts why this happens please do share.

ChrisW67
16th June 2012, 01:16
It is doing exactly what you asked it to. You moved the mapper to the last entry in the mode [2nd listing 13], which it then displays. The user edits the displayed record and submits the changes [3rd listing 4] updating the existing record.

If you want to insert a new row at the end of the the model then call model->insertRow(model->rowCount()) and move the mapper to the inserted (empty) row (mapper->toLast()), allow the user to edit it, and submit().

I am not sure what you are trying to achieve at lines 2, 3, and 5 of the third listing. You get a copy of the data from the row the user is seeing, modify a value in the copy and then do nothing with the record. At line 5 you actually insert a blank row in the model.