gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 08:07:48 25 4
gpt4 key购买 nike

我在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com