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;
        clients_tbl_model->setTable("clients");
        types_tbl_model->setEditStrategy(QSqlRelationalTableModel::OnManualSubmit);    
        ui.clients_tbl->setModel(clients_tbl_model);
        ui.clients_tbl->setColumnHidden(0, true);
        clients_tbl_model->select();
    

    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
        if(clients_tbl_model->removeRow(index.row())){
            // save changes in db
            if(clients_tbl_model->submitAll()) {
                clients_tbl_model->database().commit();           
            } else {
                clients_tbl_model->database().rollback();
                    qDebug() << "1. on_clients_delete_btn_clicked Database Write Error" <<
                         "The database reported an error: " <<
                           clients_tbl_model->lastError().text();
            }
        }else{
            qDebug() << "0. on_tclients_delete_btn_clicked model removeRow Error: " <<
                   clients_tbl_model->lastError().text();
        }
    }
    

    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
        if(types_tbl_model->removeRow(index.row())){
            // apply changes to db
            if(types_tbl_model->submitAll()) {
                types_tbl_model->database().commit();
            } else {
                types_tbl_model->database().rollback();
                    qDebug() << "1. on_types_delete_btn_clicked Database Write Error" <<
                         "The database reported an error: " <<
                           types_tbl_model->lastError().text();
            }
        }else{
            qDebug() << "0. on_types_delete_btn_clicked model removeRow Error: " <<
                   types_tbl_model->lastError().text();
        }
    


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


Log in to reply
 

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