Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to reload Tableview in Widget after Dialog is being closed in QT



  • I have a tableview in Widget and a dialog to edit records in Tableview.

    When I update the sqlite table, the dialog closes. However, tableview does not reload automatically. So please find following codes;

    I need help to reload/refresh tableview after closing dialog. Thank you in advance.

    projects.h

    public slots:
        void reload_table();
    
    private slots:
        void on_comboBox_software_currentIndexChanged(const QString &arg1);
    

    projects.cpp

    void Projects::on_comboBox_software_currentIndexChanged(const QString &arg1)
    {
        reload_table();
    }
    
    void Projects::reload_table()
    {
        Login conn;
        if(!conn.connOpen()){
            qDebug()<<"Failed to open the database";
            return;
        }
    
        conn.connOpen();
    
        QString software_name = ui->comboBox_software->currentText();
    
        QSqlTableModel *model_details_changed = new QSqlTableModel;
        model_details_changed->setTable("project_details");
        //model_details_changed->setEditStrategy(QSqlTableModel::OnManualSubmit);
        model_details_changed->setFilter("Software_Name='"+software_name+"'");
        model_details_changed->setSort(0, Qt::DescendingOrder);
        model_details_changed->select();
        //model_details_changed->setQuery("SELECT ID, Software_Name, Released_Date, Software_Language, Person_Changing, Change_Date, Purpose, Version, Report_Path "
        //                                         "FROM project_details WHERE Software_Name='"+software_name+"' ORDER BY ID DESC");
    
        ui->tableView_projectDetails->setEditTriggers(QAbstractItemView::NoEditTriggers);
        ui->tableView_projectDetails->setModel(model_details_changed);
        ui->tableView_projectDetails->setColumnHidden(0, true);
        ui->tableView_projectDetails->setColumnHidden(1, true);
        ui->tableView_projectDetails->verticalHeader()->setVisible(false);
        ui->tableView_projectDetails->setStyleSheet("QHeaderView::section { background-color:#BC4726; color:#fff; font-size: 10pt }");
        ui->tableView_projectDetails->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    
        int row_num = model_details_changed->rowCount();
        for(int i=0; i<row_num; i++){
            QPushButton* viewButton_Change_Note = new QPushButton("Görüntüle");
            ui->tableView_projectDetails->setIndexWidget(model_details_changed->index(i,7), viewButton_Change_Note);
            connect(viewButton_Change_Note, &QPushButton::clicked, this, &Projects::popUp_Change_Note);
    
            QPushButton* viewButton_Report = new QPushButton("Raporu Aç");
            ui->tableView_projectDetails->setIndexWidget(model_details_changed->index(i,9), viewButton_Report);
            connect(viewButton_Report, &QPushButton::clicked, this, &Projects::popUp_Report);
        }
    
        ui->pushButton_addChange->setEnabled(false);
        ui->pushButton_edit->setEnabled(false);
        ui->pushButton_delete->setEnabled(false);
    
        conn.connClose();
    }
    

    edit_record.h

    signals:
        void record_edited();
    

    edit_record.cpp

    void Edit_Record::on_pushButton_save_clicked()
        {  
    .
    .
    .
            Login conn;
            if(!conn.connOpen()){
            qDebug()<<"Failed to open the database";
            return;
        }
    
        conn.connOpen();
        QSqlQuery qry;
        qry.prepare("UPDATE project_details SET Project_Name='"+project_name+"', Software_Name='"+software_name+"', Software_Language='"+soft_lang+"', "
                            "Person_Changing='"+per_changing+"', Change_Date='"+change_date_str+"', Purpose='"+change_note+"', Report_Path='"+report_path+"', Version='"+version+"'"
                            "WHERE ID='"+id+"'");
    
        if(qry.exec())
        {
            QMessageBox::information(this,tr("GÜNCELLEME"),tr("Versiyon Başarıyla Güncellendi."));
    
            if(QFile::copy(ui->lineEdit_uploadReport->text(), destinationPath)){
                qDebug() << "Rapor başarıyla yüklendi.";
            }
            else{
                qDebug() << "failed";
                QMessageBox::warning(this, "HATA", "Dosya yüklenemedi.");
            }
    
            //Projects *projects = new Projects();
            Projects *projects;
            projects = new Projects();
            //QObject::connect(this, SIGNAL(record_edited()), projects, SLOT(load_table()));
            connect(this, &Edit_Record::record_edited, projects, &Projects::reload_table);
            emit record_edited();
    
            close();
            conn.connClose();
        }
        else
        {
            QMessageBox::critical(this,tr("HATA"),qry.lastError().text());
        }
    }
    

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Are you creating unique connection each ? If not, you will be globally closing your connection.

    Why are you closing these connections each time ?



  • Thank you.

    I am kind a newbie. What is your suggestion? There is no specific reason to close connections each time.


  • Lifetime Qt Champion

    @redrussianarmy said in How to reload Tableview in Widget after Dialog is being closed in QT:

    There is no specific reason to close connections each time

    Then don't do so.
    Open the connection once and close it when the application finishes (you can use constructor/destructor for this).



  • Now, I deleted connections except the first one. However, tableview still does not refresh itself. What is wrong in my code?


  • Lifetime Qt Champion

    What is the new version of your code ?
    Are you calling select on the model once you modified its source ?


Log in to reply