在QT中关闭Dialog后如何在Widget中重新加载Tableview

时间:2019-06-14 06:53:38

标签: c++ qt qtableview qsqlquery qsqltablemodel

我在Widget中有一个tableview,并且有一个对话框可以在Tableview中编辑记录。

当我更新sqlite表时,对话框关闭。但是,tableview不会自动重新加载。因此,请找到以下代码;

我需要帮助关闭对话框后重新加载表格视图。预先谢谢你。

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());
    }
}

0 个答案:

没有答案