[PyQt] Weird problem with QSystemTrayIcon

Eric Frederich eric.frederich at gmail.com
Wed Sep 15 19:34:12 BST 2010


I looked at the example with PyQt for system tray icons and tried to use it
in my application.
I couldn't get it to work.
I couldn't find what the example was doing that I wasn't doing.  I even did
the stuff in the same order.

I wound up taking the example and in-lining a bunch of functions then bit by
bit removing things until I got a minimalistic system tray icon program.
In the end, I wound up with a small sample application where for some reason
I need to call QtGui.QGroupBox() with some string or I'll never see the
system tray icon.

Below is example code.

If you remove the line ...
self.WHY_DO_I_NEED_TO_DO_THIS = QtGui.QGroupBox("A")
... then I don't see any system tray icon.

Can anyone else verify this?
I'm on Linux 64 bit with Python 2.6.5 and PyQt 4.7.2.
Thanks,
~Eric

#!/usr/bin/env python

from PyQt4 import QtCore, QtGui

class Window(QtGui.QDialog):
    def __init__(self):
        super(Window, self).__init__()

        self.WHY_DO_I_NEED_TO_DO_THIS = QtGui.QGroupBox("A")

        self.restoreAction = QtGui.QAction("&Restore", self,
                triggered=self.showNormal)

        self.quitAction = QtGui.QAction("&Quit", self,
                triggered=QtGui.qApp.quit)

        self.trayIconMenu = QtGui.QMenu(self)
        self.trayIconMenu.addAction(self.restoreAction)
        self.trayIconMenu.addSeparator()
        self.trayIconMenu.addAction(self.quitAction)

        self.trayIcon = QtGui.QSystemTrayIcon(self)
        self.trayIcon.setContextMenu(self.trayIconMenu)

        self.trayIcon.setIcon(QtGui.QIcon("./logo.png"))

        mainLayout = QtGui.QVBoxLayout()
        mainLayout.addWidget(QtGui.QPushButton("Help Please"))
        self.setLayout(mainLayout)

        self.trayIcon.show()
        self.setWindowTitle("Systray")
        self.resize(400, 300)

if __name__ == '__main__':

    import sys

    app = QtGui.QApplication(sys.argv)

    if not QtGui.QSystemTrayIcon.isSystemTrayAvailable():
        QtGui.QMessageBox.critical(None, "Systray",
                "I couldn't detect any system tray on this system.")
        sys.exit(1)

    #QtGui.QApplication.setQuitOnLastWindowClosed(False)

    window = Window()
    window.show()
    sys.exit(app.exec_())
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20100915/3c6ad476/attachment-0001.html>


More information about the PyQt mailing list