如何正确link qt dialogs?

How to correctly link qt dialogs?

我需要绑定对话框,以便“first_dialog”window 从带有两个按钮“确定”和“关闭”的主window 打开。单击“关闭”只是关闭 window。单击“确定”应该打开“second_dialog”,同时还有两个按钮“确定”和“关闭”。单击“关闭”将关闭“second_dialog”。如果你点击“确定”,那么“second_dialog”和“first_dialog”将被关闭,但应该调用 mainwindow 插槽。如何正确地做到这一点,勾勒出一个例子。我尝试绑定 destroyed () 信号并编写自己的信号,但程序捕获了分段错误。

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_pushButton_clicked()
{
    first_dialog* a= new first_dialog(this);
    //connect(a,SIGNAL(...),this,SLOT(new_button));
    a->exec();
}
#include "first_dialog.h"
#include "ui_first_dialog.h"

first_dialog::first_dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::first_dialog)
{
    ui->setupUi(this);
}

first_dialog::~first_dialog()
{
    delete ui;
}

void first_dialog::on_pushButton_clicked()
{
    second_dialog* a =new second_dialog(this);
    //connect(a,SIGNAL(...),this,SLOT(...));
    a->exec();
}

void first_dialog::on_pushButton_2_clicked()
{
    this->close();
}
#include "second_dialog.h"
#include "ui_second_dialog.h"

second_dialog::second_dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::second_dialog)
{
    ui->setupUi(this);
}

second_dialog::~second_dialog()
{
    delete ui;
}

void second_dialog::on_pushButton_2_clicked()
{
    this->close();
}

void second_dialog::on_pushButton_clicked()
{
    //....
}



看来您可以使用 acceptreject 方法。在您的对话框中,当按下任一按钮时调用这些函数。然后确保检查 return 值以了解点击了哪一个。

// MainWindow.cpp
void MainWindow::on_pushButton_clicked()
{
    first_dialog* a= new first_dialog(this);

    if (a->exec() == QDialog::Accepted)
    {
        // Both dialogs were accepted
    }
    else
    {
        // First dialog was canceled
    }
}

// first_dialog.cpp
void first_dialog::on_pushButton_clicked()
{
    second_dialog* a =new second_dialog(this);

    if (a->exec() == QDialog::Accepted)
    {
        // Second dialog was accepted, so accept the first dialog too.
        accept();
    }
    else
    {
        // Second dialog was canceled
    }
}

void first_dialog::on_pushButton_2_clicked()
{
    reject();
}

// second_dialog.cpp
void second_dialog::on_pushButton_clicked()
{
    accept();
}

void second_dialog::on_pushButton_2_clicked()
{
    reject();
}