QSystemTrayIcon example

Trying to extend the QSystemTrayIcon example by using a for loop to populate the menu only the last item in the list gets used in the menu. Why is this, and what would be the proper way to do this? In the real world, the contents of entries would be determined programatically rather than hardcoded.

#!/usr/bin/env python3

from PyQt5.QtWidgets import QApplication, QSystemTrayIcon, QMenu, QAction
from PyQt5.QtGui import QIcon


app = QApplication([])
app.setQuitOnLastWindowClosed(False)

icon = QIcon.fromTheme("application")
tray = QSystemTrayIcon()
tray.setIcon(icon)

tray.setVisible(True)

menu = QMenu()
entries = ["One", "Two", "Three"]
for entry in entries:
    action = QAction(entry)
    menu.addAction(action)
    action.triggered.connect(app.quit)

tray.setContextMenu(menu)

app.exec_()

Results in:

image

What happened to One and Two?

% python3 --version
Python 3.7.9

% pkg search Qt | grep py37-qt5-5
py37-qt5-5.15.2

Hi @probono

Normally these sorts of issues are the result of things going out of scope. In Python, unless you retain a reference to an object in the current scope it will be garbage collected. In your code, the action object is being replaced on each iteration – so on the second loop, the first action object no longer has a reference anywhere, and is deleted. This in turn deletes the Qt object, and removes it from the menu.

All you need to do is keep a reference to each action, for example in a list (here actions).

from PyQt5.QtWidgets import QApplication, QSystemTrayIcon, QMenu, QAction
from PyQt5.QtGui import QIcon


app = QApplication([])
app.setQuitOnLastWindowClosed(False)

icon = QIcon.fromTheme("application")
tray = QSystemTrayIcon()
tray.setIcon(icon)

tray.setVisible(True)

menu = QMenu()
entries = ["One", "Two", "Three"]
actions = []
for entry in entries:
    action = QAction(entry)
    menu.addAction(action)
    action.triggered.connect(app.quit)
    actions.append(action)

tray.setContextMenu(menu)

app.exec_()
1 Like

Sneaky! Thank you very much.
With this trick it works.