Qt QSystemTrayIcon更改菜单项 [英] Qt QSystemTrayIcon change menu items

查看:402
本文介绍了Qt QSystemTrayIcon更改菜单项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Pyqt,但是c ++代码很好.我正在尝试使用Linux(Ubuntu 11.10)中的QT框架更改QSystemTrayIcon中的菜单项.目前,我已尝试重置最初设置的QMenu:

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?

推荐答案

我使用以下代码进行了测试,但似乎可以正常工作:

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_())

您确定只有一个QSystemTrayIcon对象吗? (在您的代码段中,同时有self.traytray).

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

这篇关于Qt QSystemTrayIcon更改菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆