Wednesday, 30 June 2010

Session Managers and PyQt Applications

X11 window managers commonly have an option to "remember" applications which are running when a session ends.

Here's the gnome-checkbox (System -> Preferences -> Startup Applications)

but for this to work, the applications themselves need to comply. Qt does this with the QSessionManager class.

The class is not used directly, but is passed as an argument into functions of qApp, namely the saveState and commitData functions.
qApp therefore needs to be subclassed, and these two functions overwritten.

here's a working example. Note the simplicity of the functions!

#! /usr/bin/env python

from PyQt4 import QtGui, QtCore
import sys

class RestorableApplication(QtGui.QApplication):
    def __init__(self):
        super(RestorableApplication, self).__init__(sys.argv)

    def commitData(self, session):
        '''re-implement this method, called on quit'''

    def saveState(self, session):
        '''re-implement this method, called on run'''

app = RestorableApplication()

mw = QtGui.QMainWindow()
mw.setWindowTitle("I live on after a logout!")

label = QtGui.QLabel(
'''Leave me running and log off
I will survive! (on X11 systems)
Window size and position will be restored
... cool eh?''', mw)




No comments: