TheBeardedBerry
TheBeardedBerry

Reputation: 1764

How to show context menu in QSystemTrayIcon on left click

QSystemTrayIcon Docs

Ive been looking through the documentation for this widget. I have a working icon and a context menu, however i would like to be able to also show the context menu when someone left clicks. Is this possible?

Edit:

Upvotes: 3

Views: 1288

Answers (1)

alec
alec

Reputation: 6112

You could connect the QSystemTrayIcon.activated signal to call QMenu.popup when the activation reason is QSystemTrayIcon.Trigger.

import sys
from PySide2.QtWidgets import *
from PySide2.QtGui import *

class TrayIcon(QSystemTrayIcon):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.activated.connect(self.showMenuOnTrigger)

    def showMenuOnTrigger(self, reason):
        if reason == QSystemTrayIcon.Trigger:
            self.contextMenu().popup(QCursor.pos())


if __name__ == '__main__':
    app = QApplication(sys.argv)
    tray = TrayIcon(QIcon('icon.png'))
    menu = QMenu()
    menu.addAction('Action 1')
    menu.addAction('Action 2')
    tray.setContextMenu(menu)
    tray.show()
    sys.exit(app.exec_())

Upvotes: 5

Related Questions