Wait until QWidget closes


I'm working on a project in C++ and QT, and I want to open a new QWidget window, have the user interact with it, etc, then have execution return to the method that opened the window. Example (MyClass inherits QWidiget):

void doStuff(){

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

I feel like there is most likely a really easy way to do this, but for some reason I can't figure it out. How can I do this?

5/2/2009 6:52:15 PM

Accepted Answer

Have MyClass inherit QDialog. Then open it as a modal dialog with exec().

void MainWindow::createMyDialog()
  MyClass dialog(this);

Check out http://qt-project.org/doc/qt-4.8/qdialog.html

11/16/2013 9:01:21 PM

An other way is to use a loop which waits on the closing event :

#include <QEventLoop>

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow