Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Exclude a window from being checked if it is closed or not?



  • Curious question: Is it possible to exclude a QWidget from being checked if it is closed or not when quitting an application?
    I am using a window to show some of the tools and when the main window is closed while this tool window is being shown, the application doesn't quit. I would like the application to quit even if this window is being shown, i.e. this window to be excluded from being counted in if it's open or not to quit the application. There is a quitOnLastWindowClosed property in QGuiApplication, if I could exclude my QWidget window from this being counted in for this property then I guess it can be achieved. Is there any way I could do that?



  • @CJha
    No, else quitOnLastWindowClosed would document how you could exclude a window from being counted, or there would be a quitOnAllButOneWindowClosed property!

    I suggest you write some code to detect when a window is closed and close the tool window yourself if appropriate, or handle the application quit yourself instead of using that behaviour. You could also look at Qt's source code for how it handles that property and use that in your own code.

    Note also its docs state

    If this property is true, the applications quits when the last visible primary window (i.e. window with no parent) is closed.

    I don't know whether your tool window could be set as having a parent, so that it would not count for that check.


  • Lifetime Qt Champion

    Hi
    You cannot exclude any widgets. You can handle it yourself

    void MainWindow::closeEvent(QCloseEvent *event)
    {
    myToolWindow->close();
    event->accept();
    } 
    


  • @CJha said in Exclude a window from being checked if it is closed or not?:

    Is it possible to exclude a QWidget from being checked if it is closed or not when quitting an application?

    Isn't that the normal behavior for all widgets which have a parent?

    If you make you MainWindow the parent of your toolWidget, it will close with its parent and your app will exit.



  • @JonB Thanks! I will see if I can achieve the same result by any other means.



  • @Pl45m4 My tool widget is an independent window, I cannot assign MainWIndow as its parent.



  • @CJha said in Exclude a window from being checked if it is closed or not?:

    I cannot assign MainWIndow as its parent.

    You cannot or you don't want to (due to your app design structure)?
    Because I believe you can :) Even a QMainWindow can have a parent



  • @Pl45m4 Yes, I don't want to because of my app structure. This tool window is supposed to be completely independent of any other part of the app. MainWindow is not aware that this tool window exists, and the tool window is not aware that MainWindow exists.



  • @CJha said in Exclude a window from being checked if it is closed or not?:

    MainWindow is not aware that this tool window exists

    So @mrjj 's solution won't work either?!

    Tricky. Then it might not be possible ;-)


  • Lifetime Qt Champion

    @CJha
    Hi
    But there is not that many options.
    Either the tool window is closed by being owned by a parent or
    you close it manually when you want to.
    If you don't want a pointer in MainWindow to the tool, you could use signal / slot
    so MainWindow will emit ImClosing and others can react to that.



  • @Pl45m4 Yes, that's right. That's why I posted this question :)



  • @mrjj Thanks! that's a solution. But, I am put a lot of constraints on the tool window. It is created just after QApplication object is created and before the QMainWindow object is created. I would like it to be completely independent, kind of "make it and forget it" fashion, and so I am not connecting any signals to my tool window except for signals from QApplication (and therefore from QGuiApplication and QCoreApplication). I am trying right now to see if I can detect inside my tool window if it's the only window, by hooking up to the QGuiApplication::focusWindowChanged signal, if it is the only window then I will close it.


  • Lifetime Qt Champion

    @CJha
    Hi
    Ok. no signals then :)

    Try use
    QWidgetList QApplication::topLevelWidgets

    and see if ToolWindow is listed.

    I imagine then when MainWin is close the size of list is 1

    focusWindowChanged could also work. if its not fired if the user clicks on some other window.



  • @mrjj Thanks, I need a signal to know when to check QApplication::topLevelWidgets, I think I will create an event filter and apply it to QApplication and this way I can hook on to any close event that is generated in the application. I just tried with focusWindowChanged signal, it works as long my tool window is not minimized.


Log in to reply