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?


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

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()));

Why not put the code you don't want to be executed till the window has closed in a separate function and connect this as a SLOT for the window's close SIGNAL?

Need Your Help

WPF binding to Listbox selectedItem

wpf binding listbox selecteditem

Can anyone help with the following - been playing about with this but can't for the life of me get it to work.