[PyQt] Problem/Bug with QSystemTrayIcon and QMenu

Adam Batkin adam at batkin.net
Thu Sep 6 13:47:39 BST 2007


I have reduced my problem down to just a few lines of code, but the basic summary is that 
when trying to add a QMenu to a QSystemTrayIcon (tray.setContextMenu()), sometimes the 
menu deletes itself.

Here is some sample code:
# -----------------------------
def itdied ():
         print "died"

def buildMenu ():
         menu = QtGui.QMenu()
         menu.addAction("Exit", lambda: exit(0))
         QtCore.QObject.connect(menu,QtCore.SIGNAL("destroyed()"),itdied)
         return menu

app = QtGui.QApplication([])

tray = QtGui.QSystemTrayIcon()

QtCore.QObject.connect(tray,QtCore.SIGNAL("activated(QSystemTrayIcon::ActivationReason)"),activated)

tray.setContextMenu(buildMenu())
print "menu=%s" % (tray.contextMenu())

tray.show()
app.exec_()
# -----------------------------

If I run the above, I get the following output:
died
menu=None

Which means that the menu's destroyed() signal was fired, and tray.contextMenu() returned 
None (even though it was just set).

If I replace the tray.setContextMenu(buildMenu()) line with:
menu = buildMenu()
tray.setContextMenu(menu)

then my output is:
menu=<PyQt4.QtGui.QMenu object at 0xb7f1c8ac>

and "died" isn't printed until the menu is actually destroyed (on program exit), which all 
makes perfect sense since menu is set to an actual object.

Thoughts?

-Adam Batkin

My Configuration:
qt4: 4.3.1
PyQt4: PyQt-x11-gpl-4.3-snapshot-20070905 (also tested with 4.3 release, same results)
Python: 2.5
SIP: 4.7


More information about the PyQt mailing list