PDA

View Full Version : QDialog not closing/going out of scope?



Shouri
8th January 2012, 19:35
I don't know why a.exec() won't return and end the process! It's a simple code:


int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QDialog w;
w.setAttribute(Qt::WA_QuitOnClose);
w.exec();
return a.exec();
}
It was supposed to open a dialog and when I hit the 'X' button it should close and end the process, but instead the process keeps running!.. Anyone knows why?
P.S: I know I could've used QDIalog::show() method instead of QDIalog::exec() and it would work fine, but I really want to understand why it won't work this way. This is just a simple code, but I'll need it to construct more complex ones.

Thanks o/

Zlatomir
8th January 2012, 20:07
It's because you create two separate event-loops:
First here


w.exec(); //This can't close the application since application event loop didn't start.

and the second one here:


return a.exec();

And this loop is practically waiting for some window to show and after that window (that never show - because your dialog is already closed when execution reaches this second event-loop) is closed the application finishes.

So you don't need to use w.exec() there, just use show, you can use exec() if you create a second dialog and you want that dialog to be modal.

Shouri
8th January 2012, 20:45
I see!! Thanks :D

hvengel
16th February 2012, 16:15
snip
So you don't need to use w.exec() there, just use show, you can use exec() if you create a second dialog and you want that dialog to be modal.

I think this should be "... use exec() if you create a second dialog and you don't want that dialog to be modal."

Zlatomir
17th February 2012, 18:11
I think this should be "... use exec() if you create a second dialog and you don't want that dialog to be modal." No, calling exec() will create a modal dialog.
Or to rephrase it: you call exec() when you need to create a second dialog and you want that second dialog to be modal.