Reputation: 1330
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?
Upvotes: 21
Views: 18522
Reputation: 1982
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
Upvotes: 31
Reputation: 1565
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();
}
Upvotes: 15
Reputation: 111130
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
?
Upvotes: 0