Qt QSystemTrayIcon change menu items

天大地大妈咪最大 提交于 2019-12-24 10:20:26

问题


I am using Pyqt however c++ code is fine. I am trying to change a menu item in QSystemTrayIcon using the QT framework in Linux (Ubuntu 11.10). Currently I have tried to reset the QMenu that I initially set:

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

I place this in my class and make tray a class variable. I was thinking that if I just change the tray to set a new menu it would update:

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

However that doesn't work and the tray menu is still the same as it was initially made. How could I be able to rebuild the menu to change it?


回答1:


I tested with the following code and it seems 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_())

Are you sure there is only one QSystemTrayIcon object ? (In your snippets, there is both self.tray and tray).



来源:https://stackoverflow.com/questions/9338196/qt-qsystemtrayicon-change-menu-items

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!