Qt QSystemTrayIcon changes menu items

I am using Pyqt, but C ++ code is fine. I am trying to change a menu item in QSystemTrayIcon using the QT framework on Linux (Ubuntu 11.10). Currently, I tried to reset QMenu, which I originally installed:

self.tray = QSystemTrayIcon()
m = QMenu()
m.addAction('First')
m.addAction('Second')
tray.setContextMenu(m)

I put this in my class and make the class variable tray. I thought that if I just change the tray to install a new menu, it will update:

new_m = QMenu()
new_m.addAction('First')
new_m.addAction('Third')
self.tray.setContextMenu(new_m)

However, this does not work, and the tray menu is still the same as it was originally made. How can I restore a menu to change it?

+1
source share
1 answer

I tested the following code and it seemed to work fine:

from PyQt4.QtGui import *
import sys

class MainWindow(QMainWindow):
  def __init__(self):
    super(MainWindow, self).__init__()

    self.tray = QSystemTrayIcon(QApplication.style().standardIcon(QStyle.SP_DriveDVDIcon), self)
    m = QMenu()
    m.addAction('First')
    m.addAction('Second')
    self.tray.setContextMenu(m)
    self.tray.show()

    p = QPushButton("test", self)
    self.setCentralWidget(p)
    p.clicked.connect(self.onClick)

  def onClick(self):
    new_m = QMenu()
    new_m.addAction('First')
    new_m.addAction('Third')
    self.tray.setContextMenu(new_m)

app = QApplication(sys.argv)
w = MainWindow()
w.show();
sys.exit(app.exec_())

, QSystemTrayIcon? ( self.tray, tray).

+1

All Articles