Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTableView does not refresh properly
Forum Updated to NodeBB v4.3 + New Features

QTableView does not refresh properly

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 1 Posters 1.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Panoss
    wrote on last edited by Panoss
    #1

    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?

    1 Reply Last reply
    0
    • P Offline
      P Offline
      Panoss
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • P Offline
        P Offline
        Panoss
        wrote on last edited by Panoss
        #3

        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();
            }
        
        1 Reply Last reply
        0
        • P Offline
          P Offline
          Panoss
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved