【问题标题】:Reject signal seems to be emitted just once拒绝信号似乎只发出一次
【发布时间】:2012-03-27 16:28:58
【问题描述】:

我在 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()
{
}

进度对话框:

#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 destroy","Ive 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");
}

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

【问题讨论】:

    标签: c++ qt


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2011-09-23
      • 1970-01-01
      • 2019-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多