hnasarat
hnasarat

Reputation: 191

How do I handle detect logout/shutdown with PyQt4 from a closeEvent?

I want to handle when the user logs out or shuts down with my application, but I'm running into trouble because my application minimizes to the tray when the MainWindow is closed. When I log out, the closeEvent is called, but I can't tell if it was from the OS trying to quit or the user who pressed the 'x' (close) button.

Only when it's the OS will I want to close my connections and actually terminate the application. Unfortunately, the closeEvent gets called before any of the other functions. In the example below, ONLY if you comment out event.ignore() will the commitData or aboutToQuit be called.

Any help would be appreciated :)

http://pyqt.sourceforge.net/Docs/PyQt4/qapplication.html#commitData

http://pyqt.sourceforge.net/Docs/PyQt4/qcoreapplication.html#aboutToQuit

http://qt-project.org/doc/qt-4.8/qapplication.html#details

import sys
from PyQt4 import QtGui


class myWidget(QtGui.QWidget):
    def closeEvent(self, event):
        print "CLOSING"
        event.ignore()


class myApp(QtGui.QApplication):
    def __init__(self, argv):
        QtGui.QApplication.__init__(self, argv)
        print "CREATED"

    def commitData(self, session_manager):
        print "COMMITTING"


def cd(session_manager):
    print "ALSO CMOMMITTING"


def about_to_quit():
    print "QUITTING"


def main():
    app = myApp(sys.argv)
    app.aboutToQuit.connect(about_to_quit)
    w = myWidget()
    w.resize(250, 150)
    w.setWindowTitle('Simple')
    w.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

Upvotes: 1

Views: 564

Answers (1)

hnasarat
hnasarat

Reputation: 191

Well I suppose I wasted at least 5 hours on this then. I tried it in windows and it works fine. Then I did some more web searching and came across these bugs:

https://bugreports.qt-project.org/browse/QTBUG-10280 (Qt4)

https://bugreports.qt-project.org/browse/QTBUG-33034 (Qt5)

The documentation says it works in OSX though!!

So ATM it's not possible to have an app run in the background via the 'x' (close) button, AND cleanly handle log out in osx.

Upvotes: 2

Related Questions