[PyQt] Problem/Bug with QSystemTrayIcon and QMenu

Phil Thompson phil at riverbankcomputing.co.uk
Thu Sep 6 14:42:02 BST 2007


On Thursday 06 September 2007, Adam Batkin wrote:
> 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::Activ
>ationReason)"),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?

Your "workaround" is the correct thing to do (or give the menu a parent). 
setContextMenu() doesn't take ownership of the menu.

Phil


More information about the PyQt mailing list