Simulate user clicking in QSystemTrayIcon

前端 未结 1 890
无人及你
无人及你 2021-01-24 19:58

Even through the activated slot is being executed, the menu is still not showing. I traced through manually clicking the tray icon and the simulated click, and its

相关标签:
1条回答
  • 2021-01-24 20:43

    Here is how I made the context menu associated with the tray icon pop up

    class MyClass(QObject):
       def __init__():
           self._testSignal.connect(self._test_show)
           self.myTrayIcon.activated.connect(lambda reason: self._update_menu_and_show(reason))
    
       def show():
           self._testSignal.emit()
    
       @pyqtSlot()
       def _test_show():
           self._trayIcon.activated.emit(QSystemTrayIcon.Context)
    
       @QtCore.pyqtSlot()
       def _update_menu_and_show(reason):
           if reason in (QSystemTrayIcon.Trigger, QSystemTrayIcon.Context):
               mySystemTrayIcon._update_menu()
               # Trigger means user initiated, Context used for simulated
               # if simulated seems like we have to tell the window to explicitly show
    
               if reason == QSystemTrayIcon.Context:
                   mySystemTrayIcon.contextMenu().setWindowFlags(QtCore.Qt.WindowStaysOnTopHint|QtCore.Qt.FramelessWindowHint)
                   pos = mySystemTrayIcon.geometry().bottomLeft()
                   mySystemTrayIcon.contextMenu().move(pos)
                   mySystemTrayIcon.contextMenu().show()
    
    ...
    class MySystemTrayIcon(QSystemTrayIcon):
    
       def _update_menu(self):
          # logic to populate menu
          self.setContextMenu(menu)
    ...
    MyClass().show()
    

    It seems you have to set the WindowStaysOnTopHint on the context menu so that it will appear. This solution is specific to mac since it assumes the taskbar is on the top.

    One side effect is that the context menu is always on top, even if the user clicks somewhere else. I placed an event filter on the context menu, the only useful event that it registered was QEvent.Leave

    0 讨论(0)
提交回复
热议问题