2009-05-02 61 views
10

我正在使用C++和QT開發一個項目,我想打開一個新的QWidget窗口,讓用戶與它交互等,然後讓執行返回到打開窗口的方法。示例(MyClass的繼承QWidiget):等到QWidget關閉

void doStuff(){ 

    MyClass newWindow = new Myclass(); 
    /* 
     I don't want the code down here to 
     execute until newWindow has been closed 
     */ 
} 

我覺得是有最有可能是非常簡單的方法來做到這一點,但由於某種原因,我不能弄明白。我怎樣才能做到這一點?

回答

0

爲什麼不把你不希望被執行的代碼放到窗口關閉的獨立函數中,並將其作爲SLOT作爲窗口的關閉SIGNAL

+0

謝謝,這聽起來像一個好主意。我將如何做到這一點?我從來沒有硬編碼信號/插槽,只能通過QTDesigner使用它們。 – Jarek 2009-05-02 19:39:39

+0

您將不得不修改源代碼文件。 – dirkgently 2009-05-02 19:42:54

10

的另一種方法是使用一個循環等待對關閉事件:

#include <QEventLoop> 

void doStuff() 
{ 
    // Creating an instance of myClass 
    MyClass myInstance; 
    // (optional) myInstance.setAttribute(Qt::WA_DeleteOnClose); 
    myInstance.show(); 

    // This loop will wait for the window is destroyed 
    QEventLoop loop; 
    connect(this, SIGNAL(destroyed()), & loop, SLOT(quit())); 
    loop.exec(); 
}