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. How to reload Tableview in Widget after Dialog is being closed in QT
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 640 Views 1 Watching
  • 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.
  • R Offline
    R Offline
    redrussianarmy
    wrote on last edited by redrussianarmy
    #1

    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());
        }
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • R Offline
        R Offline
        redrussianarmy
        wrote on last edited by
        #3

        Thank you.

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

        jsulmJ 1 Reply Last reply
        0
        • R redrussianarmy

          Thank you.

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

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • R Offline
            R Offline
            redrussianarmy
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by SGaist
              #6

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

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              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