Background: It's quite common for applications that hide itself (or minimize it, or just close the "main window") when "x" button is clicked, instead of closing the whole application. In mac, closing window is "command+w" while closing application is "command+q". They are different.
Problem: closeEvent
is inadequate. In PyQT5, wo can hook the closeEvent in whatever QWindow/QDialog/QWidget and reject the event. We can hide the window by self.hide()
.
def closeEvent(self, event):
event.reject()
self.hide()
However, if we do that, the window becomes unstoppable because other ways of closing (Command+Q or right click the icon then quit or quit via menubar) are all blocked. (That is funny because the app cannot be closed anyway.)
Potential solution:
- Hide the x button. It is an expedient way but is off this question because what I want is to hide the window via x button rather than removing the button.
- Find a different event other than closeEvent which can distinguish X button from others and block that event. But unforunately I cannot see such events. If there exists the event, maybe I will have another problem[1]
- Continue to block closeEvent and hook other events that are binded to "command+Q" or "Quit by right click". Then force quit the application in that even hook. In this case my question becomes: can I connect quit or (command+Q) via some Qt objects?
- Maybe there are other better solutions.
This post might be similar to How to write event for window close(X) button in pyqt4 python but is absolutely different from the so-called "duplicate" of "How to disable the close button of window using Qt"
[1]. It's not a big problem but relevent. Once the window is hidden, we need a "click to the icon in taskbar/dock" event to show it. Is there some object binding to the event?