2012-03-27 22 views
0

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

對話框。 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_di考勤記錄:

#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」,「我剛剛銷燬了進度」);

但是,當我第二次(按主對話框上的按鈕,然後關閉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

您需要在每次創建新的progress_dialog時重新連接信號 - 當您銷燬舊信號時,連接丟失(否則可能會導致信號源丟失)。

所以當你創建一個新的對象時,在create_and_show_progress_dialog中做連接。

+0

墊感謝很多! – smallB 2012-03-27 17:01:33