QSessionManager Class

The QSessionManager class provides access to the session manager. More...

Header: #include <QSessionManager>
qmake: QT += gui
Inherits: QObject

Public Types

enum RestartHint { RestartIfRunning, RestartAnyway, RestartImmediately, RestartNever }

Detailed Description

A session manager in a desktop environment (in which Qt GUI applications live) keeps track of a session, which is a group of running applications, each of which has a particular state. The state of an application contains (most notably) the documents the application has open and the position and size of its windows.

The session manager is used to save the session, e.g., when the machine is shut down, and to restore a session, e.g., when the machine is started up. We recommend that you use QSettings to save an application's settings, for example, window positions, recently used files, etc. When the application is restarted by the session manager, you can restore the settings.

QSessionManager provides an interface between the application and the platform's session manager. In Qt, session management requests for action are handled by the two signals QGuiApplication::commitDataRequest() and QGuiApplication::saveStateRequest(). Both provide a reference to a QSessionManager object as argument. The session manager can only be accessed in slots invoked by these signals.

Warning: If you use QSessionManager, you should disable fallback session management: QGuiApplication::setFallbackSessionManagementEnabled().

No user interaction is possible unless the application gets explicit permission from the session manager. You ask for permission by calling allowsInteraction() or, if it is really urgent, allowsErrorInteraction(). Qt does not enforce this, but the session manager may.

You can try to abort the shutdown process by calling cancel().

For sophisticated session managers provided on Unix/X11, QSessionManager offers further possibilities to fine-tune an application's session management behavior: setRestartCommand(), setDiscardCommand(), setRestartHint(), setProperty(), requestPhase2(). See the respective function descriptions for further details.

See also QGuiApplication and Session Management.

Member Type Documentation

enum QSessionManager::RestartHint

This enum type defines the circumstances under which this application wants to be restarted by the session manager. The current values are:

ConstantValueDescription
QSessionManager::RestartIfRunning0If the application is still running when the session is shut down, it wants to be restarted at the start of the next session.
QSessionManager::RestartAnyway1The application wants to be started at the start of the next session, no matter what. (This is useful for utilities that run just after startup and then quit.)
QSessionManager::RestartImmediately2The application wants to be started immediately whenever it is not running.
QSessionManager::RestartNever3The application does not want to be restarted automatically.

The default hint is RestartIfRunning.