Important: Please read the Qt Code of Conduct -

Showing and then closing a QDialog in an application with hidden main window results in the app exiting

  • I have a tray icon-based application. Most of the time its main window is hidden, and only comes up when needed. Now, when I exec a QDialog from the tray menu, the app exits once the dialog closes, which is wrong.
    I guess I know where this issue is coming from: the application closes once the last of its windows has been closed. The catch is that the main window has never been shown in the first place yet! I don't call any of the show...() functions after creating the window. When I need the window, I show it and then hide back when I no longer need it.

    Can I circumvent the app exit issue? I really need a couple dialogs.

  • Lifetime Qt Champion


    You must set the quitOnLastWindowClosed property to false to avoid your application to quit.

  • Hi, thank you, that is an acceptable workaround. But I believe it's a bug in Qt. The docs say: "The application quits when the last visible primary window (i.e. window with no parent) is closed." Not only has my main window not been closed (because it's never even been shown yet), but also, the dialogs that I create and closing which causes the app to quit are NOT primary windows. They all have the main window as a parent.

  • Lifetime Qt Champion

    No it's not, take a look at the System Tray Icon example. That's exactly how it's done.

    Your dialog is the last visible primary window since it's the only window shown by your application. What's between the parenthesis in the documentation just state the usual case: you start your application by showing your main window which generally doesn't have any parent.

  • I see. Then it's just the case of the docs not being clear.

Log in to reply