Results 1 to 3 of 3

Thread: QSqlTableModel issues with saving changes

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2015
    Posts
    2
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default QSqlTableModel issues with saving changes

    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.

    Qt Code:
    1. void AgentReportWidget::onClientSaveButtonClicked()
    2. {
    3. const int row = clientsTableView->currentIndex().row();
    4. QSqlRecord tempRecord = clientsModel->record(row);
    5.  
    6. tempRecord.setValue(ClientTableIndex::Email, clientEmailLineEdit->text());
    7. tempRecord.setValue(ClientTableIndex::First_Name, clientFirstNameLineEdit->text());
    8. tempRecord.setValue(ClientTableIndex::Last_Name, clientLastNameLineEdit->text());
    9. tempRecord.setValue(ClientTableIndex::Company_Name, clientCompanyLineEdit->text());
    10. tempRecord.setValue(ClientTableIndex::AgentId, clientAgentComboBox->currentIndex());
    11.  
    12. qDebug() << "Email: " << tempRecord.value(ClientTableIndex::Email);
    13. qDebug() << "First Name: " << tempRecord.value(ClientTableIndex::First_Name);
    14. qDebug() << "Last Name: " << tempRecord.value(ClientTableIndex::Last_Name);
    15. qDebug() << "Company Name: " << tempRecord.value(ClientTableIndex::Company_Name);
    16. qDebug() << "AgentId: " << tempRecord.value(ClientTableIndex::AgentId);
    17. qDebug() << "--------------------------END RECORD------------------------------------";
    18.  
    19. bool sucess = clientsModel->setRecord(row, tempRecord);
    20. bool submitSuccess = clientsModel->submitAll();
    21.  
    22. for (int i = 0; i != clientsModel->rowCount(); ++i)
    23. {
    24. qDebug() << "Email: " << clientsModel->index(i, ClientTableIndex::Email).data();
    25. qDebug() << "First Name: " << clientsModel->index(i, ClientTableIndex::First_Name).data();
    26. qDebug() << "Last Name: " << clientsModel->index(i, ClientTableIndex::Last_Name).data();
    27. qDebug() << "Company Name: " << clientsModel->index(i, ClientTableIndex::Company_Name).data();
    28. qDebug() << "AgentId: " << clientsModel->index(i, ClientTableIndex::AgentId).data();
    29. qDebug() << "--------------------------END RECORD------------------------------------";
    30. }
    31.  
    32. qDebug() << "Submit Success: " << submitSuccess;
    33. qDebug() << "Set Record Success: " << sucess;
    34. }
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QSqlTableModel issues with saving changes

    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.

  3. The following user says thank you to ChrisW67 for this useful post:

    Zereo (9th March 2015)

  4. #3
    Join Date
    Mar 2015
    Posts
    2
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QSqlTableModel issues with saving changes

    Quote Originally Posted by ChrisW67 View Post
    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.

Similar Threads

  1. QSqlTableModel issues in 4.8.1 vs. 4.7.4?
    By mtnbiker66 in forum Qt Programming
    Replies: 6
    Last Post: 19th July 2012, 00:50
  2. [Solved] QSqlTableModel issues..
    By Nedlinin in forum Qt Programming
    Replies: 5
    Last Post: 4th November 2011, 15:06
  3. How to go about saving?
    By TomJoad in forum Newbie
    Replies: 3
    Last Post: 5th April 2011, 23:19
  4. Replies: 8
    Last Post: 30th March 2011, 20:06
  5. Saving as XML
    By rakkar in forum Newbie
    Replies: 2
    Last Post: 30th October 2009, 17:17

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.