I have created a very simple GUI project in Qt as follows:
main:
#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);
}
So the problem is that after running this app and pressing push_button on main dialog, progress_dialog is being displayed. After clicking on push_buttong on progress_dialog which is connected to a reject slot this dialog is closing and message is being displayed: QMessageBox::information(this,”Progress destroyed”,”I’ve just destroyed progress”);
But when I do this second time (press button on main dialog and then close the progress_dialog) no message is being displayed. Tried to debug this and set breakpoint on:
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");
}
but this breakpoint is being hit just first time, and after that no hits on this breakpoint are performed.
What’s going on?
You need to reconnect the signal each time you create a new
progress_dialog– when you destroy the old one, the connection is lost (how could that be otherwise, you’ve just zapped the source of the signal).So do the connection in
create_and_show_progress_dialogwhen you create a new object.