Frage

I have a problem regarding parts of the QT framework. I am using QT 5.0.2 and am developing on Windows at the moment.

In my application I have a Tableview set up with a QSqlRelationalTableModel. Next to it I have a text field and 3 combo boxes connected to the relational table model. The widgets are mapped to the model using QDataWidgetMapper as follows:

mapper = new QDataWidgetMapper(this);
mapper->setModel(model);
mapper->setItemDelegate(new QSqlRelationalDelegate(this));
mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
mapper->addMapping(ui->courseComboBox, model->fieldIndex("course_shortcode"));
mapper->addMapping(ui->subjectComboBox, model->fieldIndex("subject_name"));
mapper->addMapping(ui->lecturerComboBox, model->fieldIndex("lecturer_name"));
mapper->addMapping(ui->themesTextEdit, model->fieldIndex("event_themes"));

As you see the SubmitPolicy is set to manual submit. Under the widgets i have a buttonbox containing a save and a reset button.

When the save button gets clicked, I do this:

qDebug() << this->mapper->submit();
qDebug() << model->lastError().text();

This will create the following output:

true 
" " 

Which means the submit was successful and there was no error reported.

Nevertheless only the first field gets updated in the model. All the other widgets reset their value to the value from the original model (because the model emits datachanged, which the mapper connected itself to, I guess).

I tried removing one or 2 of the mappings and always onl the field whichs mapping gets added first will be updated.

If I change the submitPolicy to autoSubmit the mapper does its work as intended. But I really need to have those reset and apply buttons and not have the data get submitted on change.

This seems like an occurrence of QTBug 1086 but that bug got fixed and I cant reproduce the problem from the bug report from the code there either.

I hope you can help me.

War es hilfreich?

Lösung

I edited my answer because I misunderstood the documentation and, after receiving a good explanation, I finally got the correct way to obtain the desired result. You should simply modify the model edit strategy using QSqlTableModel::setEditStrategy() and change it to QSqlTableModel::OnRowChange. This is needed in order to avoid the modifications to be sent to the underlying DB after every single column modification, something that would produce an update of the mapped widgets contents after the very first column change.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top