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 aquitOnLastWindowClosed
property inQGuiApplication
, if I could exclude myQWidget
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, elsequitOnLastWindowClosed
would document how you could exclude a window from being counted, or there would be aquitOnAllButOneWindowClosed
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.
-
Hi
You cannot exclude any widgets. You can handle it yourselfvoid 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.
-
@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 aQMainWindow
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
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. -
@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 theQMainWindow
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 fromQApplication
(and therefore fromQGuiApplication
andQCoreApplication
). 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 theQGuiApplication::focusWindowChanged
signal, if it is the only window then I will close it. -
@CJha
Hi
Ok. no signals then :)Try use
QWidgetList QApplication::topLevelWidgetsand 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 toQApplication
and this way I can hook on to any close event that is generated in the application. I just tried withfocusWindowChanged
signal, it works as long my tool window is not minimized.