拒绝信号似乎只发出一次

时间:2012-03-27 16:28:58

标签: c++ qt

我在Qt中创建了一个非常简单的GUI项目,如下所示:

主:

#include <QApplication>
#include "dialog.h"
int main(int c, char* v[])
{
    QApplication app(c,v);
    Dialog* d = new Dialog;
    d->show();
    app.exec();
}

dialog.h:

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include "ui_dialog.h"
#include "progress_dialog.h"

class Dialog : public QDialog, private Ui::Dialog
{
    Q_OBJECT

public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();

private:
    progress_dialog* progress_dialog_;
public slots:
    void create_and_show_progress_dialog();
    void delete_progress_dialog();
};

#endif // DIALOG_H

dialog.cpp:

#include "dialog.h"
#include "ui_dialog.h"
#include <QMessageBox>
Dialog::Dialog(QWidget *parent) :
    QDialog(parent),progress_dialog_(new progress_dialog)
{
    setupUi(this);
    connect(pushButton,SIGNAL(clicked()),this,SLOT(create_and_show_progress_dialog()));
    connect(progress_dialog_,SIGNAL(rejected()),this,SLOT(delete_progress_dialog()));
}

void Dialog::create_and_show_progress_dialog()
{
    if (!progress_dialog_)
        progress_dialog_ = new progress_dialog;
    progress_dialog_->exec();
}

void Dialog::delete_progress_dialog()
{
    delete progress_dialog_;
    progress_dialog_ = nullptr;
    QMessageBox::information(this,"Progress destroyed","I've just destroyed progress");
}

Dialog::~Dialog()
{
}

progress_dialog:

#ifndef PROGRESS_DIALOG_H
#define PROGRESS_DIALOG_H

#include "ui_progress_dialog.h"

class progress_dialog : public QDialog, private Ui::progress_dialog
{
    Q_OBJECT

public:
    explicit progress_dialog(QWidget *parent = 0);
public slots:
    void exec_me();
};

#endif // PROGRESS_DIALOG_H

progress_dialog.cpp:

#include "progress_dialog.h"

progress_dialog::progress_dialog(QWidget *parent) :
    QDialog(parent)
{
    setupUi(this);
}

所以问题是在运行此应用程序并在主对话框上按下push_button后,正在显示progress_dialog。点击连接到拒绝槽的progress_dialog上的push_buttong后,该对话框正在关闭并显示消息:QMessageBox :: information(这是“Progress destroyed”,“我刚刚销毁了进度”);

但是当我第二次这样做时(按下主对话框上的按钮,然后关闭progress_dialog),没有显示任何消息。试图调试它并设置断点:

void Dialog::delete_progress_dialog()
{
    delete progress_dialog_;//HERE BREAKPOINT WAS PLACED
    progress_dialog_ = nullptr;
    QMessageBox::information(this,"Progress destroyed","I've just destroyed progress");
}

但是这个断点刚刚第一次被击中,之后没有点击这个断点 发生了什么事?

1 个答案:

答案 0 :(得分:1)

每次创建新的progress_dialog时都需要重新连接信号 - 当你销毁旧信号时,连接丢失(否则怎么可能,你只是摧毁了信号源)

创建新对象时,create_and_show_progress_dialog中的连接也是如此。