Jakes
Jakes

Reputation: 105

Update menu in QT system tray application

I need to update the existing menu items for a system tray application. At first when the app loads, there will be two menu items. Later when I click a button these menu items need to be replaced with new menu items. How can I achieve that ? Here is my code.

from PySide.QtGui import *
import sys

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

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

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

  def onClick(self):
    self.m.clear()
    self.m.addAction('First')
    self.m.addAction('Third')
    self.tray.setContextMenu(self.m)

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

However this is not working. If I try removing self.m.clear()the new menu items will append to the existing (Which is the normal behaviour in this case). Isn't menu.clear() clears the current menu & the new menu should be populated here ?

I have seen this similar question Qt QSystemTrayIcon change menu items and the solution doesn't work for me. I am running Ubuntu 14.04.

Upvotes: 4

Views: 1959

Answers (1)

qurban
qurban

Reputation: 3945

I figured it out, the problem is due to the self.tray.setContextMenu(self.m). Remove this line from onClick method. This should work fine on Ubuntu.

Upvotes: 2

Related Questions