PDA

View Full Version : QSqlTableModel issues with saving changes



Zereo
9th March 2015, 16:14
Hey everyone,

I am having some trouble getting changes to save to my QSqlTableModel (Both the model itself and the underlying database).

The way I go about doing the changes is grabbing the current QSqlRecord, making the changes to the copy of it and then submitting it to the QSqlTableModel with setRecord() and submitAll() calls. If there is a better way to go about this then what I am doing I would love to hear about that also. Though my main concern is figuring out why the model isn't getting changed and why it isn't setting the changes to the database itself.

Here is the code that is doing the actual changes and submitting them to the model.


void AgentReportWidget::onClientSaveButtonClicked()
{
const int row = clientsTableView->currentIndex().row();
QSqlRecord tempRecord = clientsModel->record(row);

tempRecord.setValue(ClientTableIndex::Email, clientEmailLineEdit->text());
tempRecord.setValue(ClientTableIndex::First_Name, clientFirstNameLineEdit->text());
tempRecord.setValue(ClientTableIndex::Last_Name, clientLastNameLineEdit->text());
tempRecord.setValue(ClientTableIndex::Company_Name , clientCompanyLineEdit->text());
tempRecord.setValue(ClientTableIndex::AgentId, clientAgentComboBox->currentIndex());

qDebug() << "Email: " << tempRecord.value(ClientTableIndex::Email);
qDebug() << "First Name: " << tempRecord.value(ClientTableIndex::First_Name);
qDebug() << "Last Name: " << tempRecord.value(ClientTableIndex::Last_Name);
qDebug() << "Company Name: " << tempRecord.value(ClientTableIndex::Company_Name);
qDebug() << "AgentId: " << tempRecord.value(ClientTableIndex::AgentId);
qDebug() << "--------------------------END RECORD------------------------------------";

bool sucess = clientsModel->setRecord(row, tempRecord);
bool submitSuccess = clientsModel->submitAll();

for (int i = 0; i != clientsModel->rowCount(); ++i)
{
qDebug() << "Email: " << clientsModel->index(i, ClientTableIndex::Email).data();
qDebug() << "First Name: " << clientsModel->index(i, ClientTableIndex::First_Name).data();
qDebug() << "Last Name: " << clientsModel->index(i, ClientTableIndex::Last_Name).data();
qDebug() << "Company Name: " << clientsModel->index(i, ClientTableIndex::Company_Name).data();
qDebug() << "AgentId: " << clientsModel->index(i, ClientTableIndex::AgentId).data();
qDebug() << "--------------------------END RECORD------------------------------------";
}

qDebug() << "Submit Success: " << submitSuccess;
qDebug() << "Set Record Success: " << sucess;
}

I also have set the models edit strategy to QSqlTableModel::OnManualSubmit in the constructor of the class. If more code is needed would be glad to paste anything else.

Here is the information from the qDebug() calls also.

----------------------------------------------------------- Debug Information -----------------------------------------------------

// The temp QSqlRecord's fields

Email: QVariant(QString, "tests")
First Name: QVariant(QString, "test")
Last Name: QVariant(QString, "test")
Company Name: QVariant(QString, "test")
AgentId: QVariant(int, 0)
--------------------------END RECORD------------------------------------

// The model's records after the submitAll() call notice how the fields aren't replaced with the above.
// (Just printed all the rows in the model but the first one should be replaced since that is the current index)

Email: QVariant(QString, "unlisted@aaa.net") // Replaced this info since it was customer info.
First Name: QVariant(QString, "Jane")
Last Name: QVariant(QString, "Doe")
Company Name: QVariant(QString, "Company Information")
AgentId: QVariant(QString, "Billy")
--------------------------END RECORD------------------------------------
Email: QVariant(QString, "UnlistedTwo@aaa.net")
First Name: QVariant(QString, "John")
Last Name: QVariant(QString, "Doe")
Company Name: QVariant(QString, "Unlisted Inc.")
AgentId: QVariant(QString, "Billy")
--------------------------END RECORD------------------------------------
Submit Success: true
Set Record Success: true

I am quite stumped on why nothing is updating. The information stays the same after calling onClientsaveButtonClick() both for the database and the QTableView on my application. The only thing I can think of is I am going about making the changes to the model (And database underneath) the wrong way. Any suggestions or guidance you guys can provide would be greatly appreciated since I am still trying to stumble my way through picking up the QT framework ;p. Thanks in advance.

ChrisW67
9th March 2015, 20:48
Does the underlying table have a primary key (unique identifier)?
What is the data type of the agentid column in the database? String or integer? Your new record has an int, your existing data is returned as a string.

Zereo
9th March 2015, 21:25
Does the underlying table have a primary key (unique identifier)?
What is the data type of the agentid column in the database? String or integer? Your new record has an int, your existing data is returned as a string.

Thank you for the reply Chris, the reason the agentid column is int and string is because I am dealing with a relational table (Forgot to mention that sorry) though not sure if I was doing it right in the code above (Databases are still a very new topic to me). Though thanks again for your reply, it really helped point out the last bug I was having with the model not saving the updated data to the database because I was missing a primary key for that table in the database. Been struggling with this bug for a few hours now and good to have it fixed.

For anyone that might come across this thread in the future and has the same problem the way I went about fixing everything was getting rid of manually updating the tables fields and instead using a QDataWidgetMapper which greatly simplified the process, plus of course going back and adding a primary key in my database which I missed ;p.