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);
dialog.exec();
}
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);
myInstance.show();
// This loop will wait for the window is destroyed
QEventLoop loop;
connect(this, SIGNAL(destroyed()), & loop, SLOT(quit()));
loop.exec();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With