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->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->setModel(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);
// 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);
To copy to clipboard, switch view to plain text mode
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())
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);
To copy to clipboard, switch view to plain text mode
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;
}
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;
}
To copy to clipboard, switch view to plain text mode
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
Bookmarks