Update QTableView after QSqlTableModel insert

  • I'm writing an application over SQLite3 database for home bookkeeping.
    I'm using Qt 5.9.2, MinGW 5.3.0 32bit and QWidgets.

    Here I'm appending record to the QSqlTableModel (forgive me please for that kind of "ass-driven" development):

    void RegistryTab::on_btnAdd_clicked()
        AddEditDialog d(type, this);
        if(d.exec() == QDialog::Accepted) {
            QSqlField field("id",QVariant::String);
            QSqlRecord t;
            QHash<QString,QString>::iterator i;
            for(i=d.data()->begin(); i != d.data()->end(); ++i) {
                QSqlField temp(i.key(), QVariant::String);
    void ModelsContainer::addRegistryRecord(RegistryType type, const QSqlRecord &record)
        tableModels->value((*registryMap)[type])->insertRecord(-1, record);

    But after appending I have such a picture:

    How can I refresh the QTableView to see model changes immediately?
    Model's EditStrategy is "OnFieldChange".

    Thanks in advance!

  • Lifetime Qt Champion


    You should check the return value of insertRecord. Also, are you sure that your QSqlRecordObject contains something ?

  • After that code execution QTableView displays empty cells in a row with an exclamation mark on the left side.
    But, if I restart the whole application, the data successfully appears => the data is inserted into underlying physical SQL table in database file.
    Summing up, I cannot find any way to make QSqlTableModel data changes be immidiately displayed in a QTableView.

  • Lifetime Qt Champion

    AFAIR and from the SQL Presenting chapter, an exclamation mark means that the change was not submitted.

    I'd try to call submitAll after insert to see if it changes anything.

  • @SGaist
    Thanks a lot, the problem is solved, submitAll() was the solution indeed. Marking topic as solved.

Log in to reply

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