Important: Please read the Qt Code of Conduct -

QTableView does not refresh properly

  • I have a model,QSqlTableModel *clients_tbl_model;
    I have a QTableView, named clients_tbl.

        // set clients_tbl
        clients_tbl_model = new QSqlTableModel;
        ui.clients_tbl->setColumnHidden(0, true);

    This is the code that removes a row:

    void RepairDevices::on_clients_delete_btn_clicked()
        QModelIndex index = ui.clients_tbl->currentIndex();    
        // remove row from model
            // save changes in db
            if(clients_tbl_model->submitAll()) {
            } else {
                    qDebug() << "1. on_clients_delete_btn_clicked Database Write Error" <<
                         "The database reported an error: " <<
            qDebug() << "0. on_tclients_delete_btn_clicked model removeRow Error: " <<

    The row gets removed from the db, gets removed from the model, but from the table, instead of being removed, it gets...empty!!!
    In the vertical header, instead of the row number, an exclamation mark (!) is placed!
    And the fields in the row, are empty.

    The exactly same code works fine on other two QTableViews.

    What is happening?

  • I found it, I had made a mistake with model 's setEditStrategy, I as setting it for another model (types_tbl_model, line 3 of 1st block of code) and not for the clients_tbl_model...

  • I reopen this topic of mine because I have, again. problem with QTableView 's refresh ater deleting a record.
    So, when I delete a record, the record (row in the table) does not disappear, but instead, columns become empty, and the vertical header has an exclamation mark.
    This appeared after changing my model 's editStrategy from QSqlRelationalTableModel::OnManualSubmit to OnFieldChange.
    This is my code that removes a row from the table:

        QModelIndex index = ui.types_tbl->currentIndex();
        // remove row from model
            // apply changes to db
            if(types_tbl_model->submitAll()) {
            } else {
                    qDebug() << "1. on_types_delete_btn_clicked Database Write Error" <<
                         "The database reported an error: " <<
            qDebug() << "0. on_types_delete_btn_clicked model removeRow Error: " <<

  • After "database().commit();" I put "types_tbl_model->select();" and now it refreshes properly.
    But, is this the correct solution?

Log in to reply