mingxiao
mingxiao

Reputation: 1802

QSystemTrayIcon determine if it is clicked or in focus

With a QSystemTrayIcon object how can I determine if it is currently clicked? Also how can I determine when a user has clicked away from the tray icon and its associated menu? I'm thinking it involves installing an event filter?

There's UI I want to show, but I only want to show it if the tray icon is not currently activated, meaning its associated tray icon is not in focus. So I can use the activated signal to determine when someone has clicked it, but how do I determine when its no longer active?

I am using python 2.7.3 and PyQt5

Upvotes: 3

Views: 3093

Answers (1)

qurban
qurban

Reputation: 3945

There is a singnal in QSystemTrayIcon named activated passing a reason to it's connected slot. I have created a simple example for you:

from PyQt4 import QtGui
import sys

class Window(QtGui.QMainWindow):

    def __init__(self, parent=None):
        super(Window, self).__init__(parent)
        icon = QtGui.QSystemTrayIcon(self)
        icon.setIcon(QtGui.QIcon(r"path/to/icon.png"))
        icon.show()
        icon.activated.connect(self.systemIcon)

    def systemIcon(self, reason):
        if reason == QtGui.QSystemTrayIcon.Trigger:
            print 'Clicked'

if __name__ == "__main__":
    application = QtGui.QApplication(sys.argv)
    win = Window()
    win.show()
    sys.exit(application.exec_())

You can change QSystemTrayIcon.Trigger to QSystemTrayIcon.DoubleClick, QSystemTrayIcon.Context, QSystemTrayIcon.MiddleClick if you need these events.

Upvotes: 2

Related Questions