Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wait until QWidget closes

Tags:

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?

like image 442
Jarek Avatar asked May 02 '09 18:05

Jarek


2 Answers

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

void MainWindow::createMyDialog()
{
  MyClass dialog(this);
  dialog.exec();
}

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

like image 120
Mark Beckwith Avatar answered Sep 23 '22 16:09

Mark Beckwith


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);  
    myInstance.show();

    // This loop will wait for the window is destroyed
    QEventLoop loop;
    connect(this, SIGNAL(destroyed()), & loop, SLOT(quit()));
    loop.exec();
}
like image 44
56ka Avatar answered Sep 21 '22 16:09

56ka