[Development] Avoiding 'random' app.quit() in QtQuick applications.

Stephen Kelly stephen.kelly at kdab.com
Fri May 18 14:01:37 CEST 2012

Hi there,

The issue raised in this thread:


Can be simplified to this example:

    QWindow window;
    QWidget widget;


    // This should not quit the application, because 
    // the QWindow is still open. It does quit though, which is the bug.
    QTimer::singleShot(2000, &widget, SLOT(close()));

Because quitOnLastWindowClosed is true by default, closing the QWidget causes 
a count of the remaining top level widgets, and if there are none visible 
left, the application quits. 

In Qt 5 we can have top-level QWindows as well as QWidgets, so we need to 
count both. Currently that is not done, but [1] fixes that.

[1] https://codereview.qt-project.org/#change,26167

The problem is that there is an existing unit test which fails when QWindows 
are also counted. The reason for that can be seen with the patch and the 
behavior in this code:

    QWidget widget;
    QPointer<QWidget> childWindow = new QWidget(&widget, Qt::Window);


    // This should quit the application, but with the patch does not. The  
    // problem is that the childWindow also has a QWindow backing it which 
    // does not get hidden as a result of the childWindow parent being closed.
    QTimer::singleShot(2000, &widget, SLOT(close()));

Before the patch, the closing of widget in this snippet would cause the loop 
in QApplication::shouldQuit to be executed, and eventually return true. Now it 
returns 'count the top-level QWindows and use that result'.

To work around this issue I created another patch [2] to close the child 
windows of a widget if that widget itself is closed. I'm not certain it's the 
right solution though. Some code out there could be relying on the existing 
behavior that one can close a Dialog without its children closing.

[2] https://codereview.qt-project.org/#change,26466

I'm looking for opinions and ideas for other ways to solve this problem. I 
think the patch at [1] should be applied. 

I'm not certain if [2] needs to be applied at all. If the parent of a 
QWidget(Qt::Window) is closed, and it is the last top-level window, is there 
any reason for the application not to quit (if quitOnLastWindowClosed is 
true)? I think it should quit, and then applying [1] would be enough.


Stephen Kelly <stephen.kelly at kdab.com> | Software Engineer
KDAB (Deutschland) GmbH & Co.KG, a KDAB Group Company
www.kdab.com || Germany +49-30-521325470 || Sweden (HQ) +46-563-540090
KDAB - Qt Experts - Platform-Independent Software Solutions
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 3636 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/development/attachments/20120518/3aa5436c/attachment.bin>

More information about the Development mailing list