c++ - 拒绝信号似乎只发出一次

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

对话框.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

对话框.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()
{
}

进度对话:

#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(this,"Progress destroyed","I've just destroyed progress");

但是当我第二次这样做时(按下主对话框上的按钮,然后关闭 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");
}

但是这个断点只是第一次被命中,之后就没有再命中这个断点了。
怎么回事?

最佳答案

每次创建新的 progress_dialog 时都需要重新连接信号 - 当您销毁旧的时,连接会丢失(否则怎么可能,您刚刚关闭了源信号)。

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

关于c++ - 拒绝信号似乎只发出一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9893711/

相关文章:

c++ - 哪一种是在 C++ 中分配未初始化内存的最惯用的方法

c++ - 调试中的 Eclipse CDT View 结构

c++ - Qt QPainter在paintEvent中消除用paintGL绘制的图形

c++ - 如何将参数从构造函数传递到插槽中?

c# - 内存泄漏检测器

c++ - 在 OSX 上打开 SDL 窗口时不要打开终端窗口

c++ - 词法分析器不断跳过最终标记

c++ - QRegularExpression 和 QRegExp 之间有什么区别吗?

c++ - 将问题与 libharu 联系起来

c++ - 如何刷新QGraphicsView以显示QGraphicsScene的背景更改