PDA

View Full Version : QDataWidgetMapper Problem with updating fields



pcheng
19th March 2013, 15:27
Greetings everyone. Once again I require the wisdom of the QT masters.

I have the following form with a qdatawidgetmapper linked to a table in MySQL.




// Setup the model to use in the mapper
model = new QSqlRelationalTableModel(this);
model->setTable("Orders");

model->setRelation(7, QSqlRelation("customers", "CustomerNumber", "Name"));
model->setRelation(5, QSqlRelation("employee", "EmployeeNumber", "UserName"));
model->select();
Qt::SortOrder order = Qt::AscendingOrder;
model->sort(0, order);

// Setup the mapper for the order widgets
mapper = new QDataWidgetMapper(this);
mapper->setModel(model);
mapper->setItemDelegate(new QSqlRelationalDelegate(model));
mapper->addMapping(ui->idLineEdit, model->fieldIndex("OrderID"));
mapper->addMapping(ui->fullfilledCheckBox, model->fieldIndex("OrderFullfilled"));
mapper->addMapping(ui->fullfilledDateEdit, model->fieldIndex("FullfieldDate"));
mapper->addMapping(ui->orderDateEdit, model->fieldIndex("DateTime"));
mapper->addMapping(ui->dueDateEdit, model->fieldIndex("DueDateTime"));
mapper->addMapping(ui->employeeLineEdit, 5);
mapper->addMapping(ui->dueTimeEdit, model->fieldIndex("DueTime"));
mapper->addMapping(ui->customerLineEdit, 7);
//mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);



Then I have a checkbox which should set the date in the fullfilledDateEdit box to today's date and save it.



ui->fullfilledDateEdit->setFocus();
ui->fullfilledDateEdit->setDate(QDate::currentDate());
ui->closePushButton->setFocus();
ui->fullfilledCheckBox->setDisabled(true);
ui->fullfilledCheckBox->setChecked(true);

//Save the order
if (saveOrder())
QMessageBox::critical(this,"Update Order Error", "There was an error saving the record!", QMessageBox::Ok);


SaveOrder simply performs a model->submitall() as shown below.



int addOrder::saveOrder()
{
int currIndex = mapper->currentIndex();
if ((ui->customerLineEdit->text()=="") || (ui->employeeLineEdit->text()==""))
return 1; // Notify user that save was not successful

// Submit all changes to the database
if(!model->submit() && (!model->lastError().text().contains("No Fields to update"))) { return 1; }
else if (!itemModel->submitAll() && (!itemModel->lastError().text().contains("No Fields to update"))) { return 1; }
else
{
mapper->setCurrentIndex(currIndex);
ui->savePushButton->setEnabled(false);
}

return 0;
}


My problem is that when I press the checkbox the date is changed in the GUI but it does not update the value in the database. When I close the window and open it again it shows the old value.

Any ideas on why this is not working?

I tried using manualsubmit but this created other problems in that it would not update anything in the table.

Thanks in advance,

Pericles

anda_skoa
20th March 2013, 19:35
Do I guess correctly that the setFocus() calls you are doing are intended to trigger the "input finished" behavior of the date time widget?
I.e.make it look like the user had entered the date and has moved on?

That might not actually happen, I don't think the Focus event is directly sent to the widget, it is more likely queued for the event loop to process like other events.

I think the best way would be to use QDataWidgetMapper::currentIndex() plus the column info (either hardcode or through QDataWidgetMapper::mappedSection() and the input widget) and create a QModelIndex for that and call model->setData()
I.e. changing the actual data, not its UI representation.

Cheers,
_

pcheng
21st March 2013, 14:13
Thanks for the reply Anda.

Yes you are right to assume that the setfocus is there to trigger the change in value.

But I cannot really understand what I need to do to set the data in the model. Could you please provide me with some more information on what I should do?

I found model->setData() but I do not know what to put in the three fields that it needs. It wants an index, the value (which will be QDate::currentDate()) and an integer that is equal to Qt::EditRole.

Thanks for the help,

Pericles

anda_skoa
22nd March 2013, 16:07
Probably something like this



const QModelIndex index = model->index( currIndex, model->model->fieldIndex("FullfieldDate") );
model->setData( index, QDate::currentDate() );


Cheers,
_