I have a Qt application (A) that runs as a Windows system tray application.
I also have another Qt application (B) that launches/closes any executable.
If I try to close application (A) with its system tray context menu, everything works as expected (process is killed, system tray icon disappears).
However, if I try to close application (A) via application (B), the system tray icon of application (A) disappears, but its process remains running in the background. I have to force kill the process to get it to quit.
Application (B) uses QProcess to launch executables. When requested, it calls QProcess::terminate() to close the launched application.
There is no issue closing applications that actually have windows (forms). It is only this one windowless system tray application (A) that is having issues.
I have overridden the QWidget::closeEvent(...), which is never triggered/called.
How can I get the closeEvent(...) to be triggered in application (A)?
Qt documentation implies that the closeEvent is triggered by a window close request, which may be the problem since application (A) has no windows. Do I need to give application (A) a form and hide it?
Turns out a form is required to receive the closeEvent.
[closeEvent] is called with the given event when Qt receives a window close request for a top-level widget from the window system .
I added a form to application (A) and hid it.
setStyleSheet( "background: transparent;" );
setAttribute( Qt::WA_TranslucentBackground );
Now when application (B) sends the QProcess::terminate() the closeEvent is triggered and everything shuts down as expected!
Well, almost... One thing I didn't expect is that, even with the window hidden, it still had a taskbar button. To remove it, I added the following to the constructor:
setWindowFlags( Qt::SubWindow );
show();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.