PDA

View Full Version : WA_QuitOnClose weird in Qt5 ?



drhex
24th March 2013, 09:49
I'm having problems with this simple program:


#include <QApplication>
#include <QLabel>

int main( int argc, char **argv )
{
QApplication app( argc, argv );

QLabel *lab = new QLabel("Very important Main Window");
lab->setAttribute(Qt::WA_QuitOnClose, true);
lab->show();

QLabel *lab2 = new QLabel("less important auxilliary window");
lab2->setAttribute(Qt::WA_QuitOnClose, false);
lab2->show();

app.setQuitOnLastWindowClosed(true);

return app.exec();
}

The idea is that there are two windows. One important and one less important. The application is supposed to quit when the last "important" window is closed.
When run in Qt4, the program behaves the way I want it to: when I close the "important" window the app quits regardless of whether the less important windows was alive then.

In Qt5 things go differently:

1. close less important window
2. close important window
3. --> app exits : OK

1. close important window
2. (app doesn't exit)
3. close less important window
4. (app still doesn't exit)

The documentation for WA_QuitOnClose and setQuitOnLastWindowClosed() seems to be unchanged between Qt4 and Qt5.
So is there a bug in Qt5 or have I misunderstood these attributes?

wysota
24th March 2013, 11:10
I don't know why this happens yet, but with your code there is no signal-slot connection in the application object between lastWindowClosed() and quit(). If you add that connection manually then everything works as expected.

Edit: Yeah, I think there is a bug in QApplicationPrivate::shouldQuit(). It calls QGuiApplication::shouldQuit() which will prevent quitting if there are any visible windows.

drhex
24th March 2013, 17:54
Adding the signal as you suggested works, thanks.