Table Of Contents

Previous topic


Next topic



Inheritance diagram of QSessionManager



Detailed Description

The PySide.QtGui.QSessionManager class provides access to the session manager.

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 PySide.QtCore.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.

PySide.QtGui.QSessionManager provides an interface between the application and the session manager so that the program can work well with the session manager. In Qt, session management requests for action are handled by the two virtual functions QApplication.commitData() and QApplication.saveState() . Both provide a reference to a session manager object as argument, to allow the application to communicate with the session manager. The session manager can only be accessed through these functions.

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

You can try to abort the shutdown process by calling PySide.QtGui.QSessionManager.cancel() . The default commitData() function does this if some top-level window rejected its closeEvent().

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

See also

PySide.QtGui.QApplication Session Management


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

Constant Description
QSessionManager.RestartIfRunning If the application is still running when the session is shut down, it wants to be restarted at the start of the next session.
QSessionManager.RestartAnyway The 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.RestartImmediately The application wants to be started immediately whenever it is not running.
QSessionManager.RestartNever The application does not want to be restarted automatically.

The default hint is RestartIfRunning .

Return type:PySide.QtCore.bool

Returns true if error interaction is permitted; otherwise returns false.

This is similar to PySide.QtGui.QSessionManager.allowsInteraction() , but also enables the application to tell the user about any errors that occur. Session managers may give error interaction requests higher priority, which means that it is more likely that an error interaction is permitted. However, you are still not guaranteed that the session manager will allow interaction.

Return type:PySide.QtCore.bool

Asks the session manager for permission to interact with the user. Returns true if interaction is permitted; otherwise returns false.

The rationale behind this mechanism is to make it possible to synchronize user interaction during a shutdown. Advanced session managers may ask all applications simultaneously to commit their data, resulting in a much faster shutdown.

When the interaction is completed we strongly recommend releasing the user interaction semaphore with a call to PySide.QtGui.QSessionManager.release() . This way, other applications may get the chance to interact with the user while your application is still busy saving data. (The semaphore is implicitly released when the application exits.)

If the user decides to cancel the shutdown process during the interaction phase, you must tell the session manager that this has happened by calling PySide.QtGui.QSessionManager.cancel() .

Here’s an example of how an application’s QApplication.commitData() might be implemented:

class MyApplication (QApplication):
# ...
    def commitData(QSessionManager& manager)
        if manager.allowsInteraction():
            ret = QMessageBox.warning(
          "My Application"),
          "Save changes to document?"),
                    QMessageBox.Save | QMessageBox.Discard | QMessageBox.Cancel)

            if ret == QMessageBox.Save:
                if not saveDocument():
            elif ret == QMessageBox.Discard:
            # we did not get permission to interact, then
            # do something reasonable instead

If an error occurred within the application while saving its data, you may want to try PySide.QtGui.QSessionManager.allowsErrorInteraction() instead.


Tells the session manager to cancel the shutdown process. Applications should not call this function without asking the user first.

Return type:list of strings

Returns the currently set discard command.

To iterate over the list, you can use the foreach pseudo-keyword:

for command in mySession.discardCommand():
Return type:PySide.QtCore.bool

Returns true if the session manager is currently performing a second session management phase; otherwise returns false.


Releases the session manager’s interaction semaphore after an interaction phase.


Requests a second session management phase for the application. The application may then return immediately from the QApplication.commitData() or QApplication.saveState() function, and they will be called again once most or all other applications have finished their session management.

The two phases are useful for applications such as the X11 window manager that need to store information about another application’s windows and therefore have to wait until these applications have completed their respective session management tasks.


If another application has requested a second phase it may get called before, simultaneously with, or after your application’s second phase.

Return type:list of strings

Returns the currently set restart command.

To iterate over the list, you can use the foreach pseudo-keyword:

for command in mySession.restartCommand():
Return type:PySide.QtGui.QSessionManager.RestartHint

Returns the application’s current restart hint. The default is RestartIfRunning .

Return type:unicode

Returns the identifier of the current session.

If the application has been restored from an earlier session, this identifier is the same as it was in the earlier session.

Return type:unicode

Returns the session key in the current session.

If the application has been restored from an earlier session, this key is the same as it was when the previous session ended.

The session key changes with every call of commitData() or saveState().

Parameters:arg__1 – list of strings

Sets the discard command to the given list .

PySide.QtGui.QSessionManager.setManagerProperty(name, value)
  • name – unicode
  • value – list of strings

Low-level write access to the application’s identification and state record are kept in the session manager.

The property called name has its value set to the string list value .

PySide.QtGui.QSessionManager.setManagerProperty(name, value)
  • name – unicode
  • value – unicode

This is an overloaded function.

Low-level write access to the application’s identification and state records are kept in the session manager.

The property called name has its value set to the string value .

Parameters:arg__1 – list of strings

If the session manager is capable of restoring sessions it will execute command in order to restore the application. The command defaults to

appname -session id

The -session option is mandatory; otherwise PySide.QtGui.QApplication cannot tell whether it has been restored or what the current session identifier is. See QApplication.isSessionRestored() and QApplication.sessionId() for details.

If your application is very simple, it may be possible to store the entire application state in additional command line options. This is usually a very bad idea because command lines are often limited to a few hundred bytes. Instead, use PySide.QtCore.QSettings , temporary files, or a database for this purpose. By marking the data with the unique PySide.QtGui.QSessionManager.sessionId() , you will be able to restore the application in a future session.


Sets the application’s restart hint to hint . On application startup, the hint is set to RestartIfRunning .


These flags are only hints, a session manager may or may not respect them.

We recommend setting the restart hint in QApplication.saveState() because most session managers perform a checkpoint shortly after an application’s startup.