'How to distinguish different sources of spontaneous QCloseEvent in Qt5?
in my application, I override QMainWindow::closeEvent in order to do some logic before closing the app.
The only argument this function gets is of type QCloseEvent, and it's called whenever the user tries to close the app.
It happens when we click the X button, of course, but depending on platform we also have things like alt+f4, rightclick->close and possibly more.
My goal here is to distinguish between these. More specifically, I need to know if just the X button was pressed or not.
From to the docs, which can be found here: https://doc.qt.io/qt-5/qcloseevent-members.html
I can't see anything that would help me get this information.
There is a function spontaneous(), which allows me to check if the event was triggered programmatically or by the environment/user, but in the case I'm presenting, every event has this value as true.
Unfortunately, I don't even know if this is possible. I expect that it could work in a way that X11 or some other component in the system would just send the same signal for every case, but I don't really know where to look for this information.
Other questions that I've found on stack cover the spontaneous vs non-spontaneous cases, thus the didn't help me.
Solution 1:[1]
closeEvent() is called after the window is closed, not necessarily when the application is closed. Therefore, it's not called until after all the logic for the key is processed.
Override keyEvent() and use that function to run your key press logic.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Peter Csala |
