Relrin
Relrin

Reputation: 790

Handlers for menu in tray

I'm trying to write simplest app on PyQt, but get a little issue: I dont understand why after add handler for "exit" panel, handler didnt work? After click on "Exit" in menu nothing happens. How i can fix this, than when click on "Exit", my app will be closed?

#! /usr/bin/env python
from PyQt4 import QtGui, QtCore

class RightClickMenu(QtGui.QMenu):
    def __init__(self, parent=None):
        iconMenu = QtGui.QMenu.__init__(self, "Edit", parent)
        icon = QtGui.QIcon.fromTheme("edit-cut")
        self.addAction(QtGui.QAction(icon, "&Cut", self))

        icon = QtGui.QIcon.fromTheme("edit-copy")
        self.addAction(QtGui.QAction(icon, "Copy (&X)", self))

        icon = QtGui.QIcon.fromTheme("edit-paste")
        self.addAction(QtGui.QAction(icon, "&Paste", self))

        icon = QtGui.QIcon.fromTheme("edit-paste")
        self.addAction(QtGui.QAction(icon, "&Exit", self))
        self.connect(self, QtCore.SIGNAL('clicked()'), self.exit)

    def exit(self):
        QtCore.QCoreApplication.instance().quit()

class LeftClickMenu(QtGui.QMenu):
    def __init__(self, parent=None):
        QtGui.QMenu.__init__(self, "File", parent)

        icon = QtGui.QIcon.fromTheme("document-new")
        self.addAction(QtGui.QAction(icon, "&New", self))

        icon = QtGui.QIcon.fromTheme("document-open")
        self.addAction(QtGui.QAction(icon, "&Open", self))

        icon = QtGui.QIcon.fromTheme("document-save")
        self.addAction(QtGui.QAction(icon, "&Save", self))

class SystemTrayIcon(QtGui.QSystemTrayIcon):
    def __init__(self, parent=None):
        QtGui.QSystemTrayIcon.__init__(self, parent)
        self.setIcon(QtGui.QIcon("tray.png"))

        self.right_menu = RightClickMenu()
        self.setContextMenu(self.right_menu)

        self.left_menu = LeftClickMenu()
        self.activated.connect(self.click_trap)

    def click_trap(self, value):
        if value == self.Trigger: #left click!
            self.left_menu.exec_(QtGui.QCursor.pos())

    def welcome(self):
        self.showMessage("Hello", "I should be aware of both buttons")

    def show(self):
        QtGui.QSystemTrayIcon.show(self)
        QtCore.QTimer.singleShot(100, self.welcome)


def main():
    app = QtGui.QApplication([])
    tray = SystemTrayIcon()
    tray.show()
    app.exec_()

if __name__ == '__main__':
    main()

Upvotes: 0

Views: 458

Answers (2)

qurban
qurban

Reputation: 3945

Change the following line:

self.addAction(QtGui.QAction(icon, "&Exit", self))
self.connect(self, QtCore.SIGNAL('clicked()'), self.exit)

As:

exitAction = QtGui.QAction(icon, "&Exit", self)
self.addAction(exitAction)
exitAction.triggered.connect(self.exit)

The problem is that you are connect the clicked signal of QMenu to the exit method. When you click on the action, the action's triggered signal is emited not menu's clicked because the action is at the top of the menu.

Upvotes: 1

Relrin
Relrin

Reputation: 790

Just changed this lines

self.addAction(QtGui.QAction(icon, "&Exit", self))
self.connect(self, QtCore.SIGNAL('clicked()'), self.exit)

on

action = QtGui.QAction(icon, "&Exit", self)
self.addAction(action)
self.connect(action, QtCore.SIGNAL("triggered()"), self.exit)

and all now great works!

Upvotes: 0

Related Questions