QSqlTableModel saving changes problems.

  • 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. 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.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.