Search code examples
qtoperating-systemsignalsgnomeshutdown

Allow user interaction when application is asked to exit


I'd like to write a Qt application which main purpose is to warn the user that there are things to do before he should shut down the computer. I thought this is possible, since a lot of applications ask the user to save before quitting when the computer is about to be shut down. I also want the user to be able to interrupt the shut down process, like those applications allow the user to say "Cancel".

Is there a way to do this in Qt?

If not, how to do this at least in a gnome session? (Support for more desktop environments would be nice, but currently this application is for me and my friends only, and we all use gnome.)

I read about the signal QCoreApplication::aboutToQuit(), but the documentation says that it doesn't allow user interaction. My application doesn't use any widget (maybe only a system tray icon), so the QWidget::closeEvent isn't of any use either.

Will handling the appropriate posix signal help? But as far as I know, such a signal handler may only contain trivial statements and asking the user whether to really shut down isn't trivial.

Here are some details if it helps: When the user wants to shut down the computer, my application will check if the repository (he configured to watch) is "clean", i.e. there is nothing to commit. If there is something to be committed, the application should warn the user and let him choose to ignore the uncommitted files or to abort the shut down process (in order to commit the changes).


Solution

  • You should implement session handling. When the operating system shuts down, QApplication::commitData() is called and you can ask the session manager to allow user interaction:

    Within this function, no user interaction is possible, unless you ask the manager for explicit permission.

    There is also an example about exactly your use case here.