QApplication handling unexpected X server shutdown?
-
I have a Qt application that runs on a linux machine that displays UI windows on a Windows machine running an X server. There's a strong possibility that the users of my application will logout of Windows or kill the X server without quitting my application first.
Is there a way to handle the unexpected shutdown of the X server, so that the application can gracefully shutdown, save stored data, etc? So far I've tried tying QApplication's aboutToQuit signal to a slot in my app, plus catching all exceptions in main. Neither worked as I expected.
-
You should look into signal handling in your process. When the X Server process terminates all its child processes (including your application) are sent a signal (I don't know which one maybe SIGKILL or SIGTERM) that you can catch and gracefully handle if you want to, default behavior is to terminate the process.
Note that you must not call Qt code from signal handlers (they interrupt anything running in the process). You can however post messages to QObjects (that is threadsafe!) and perform an asynchronous operation in the main thread that will eventually call QApplication::quit() and terminate the process.