Uploaded image for project: 'Qt for Python'
  1. Qt for Python
  2. PYSIDE-2196

System tray menu not created Ubuntu and Windows 10

    XMLWordPrintable

Details

    • Bug
    • Resolution: Invalid
    • Not Evaluated
    • None
    • 6.4.2
    • PySide
    • None
    • Linux/Wayland, Linux/X11, Windows

    Description

      The following example code creates a system tray icon with a quit button:

      • Placing the exec() function in main() does not work.
      • Placing it inside the constructor of the Application class works.
      from PySide6.QtWidgets import QApplication, QWidget, QSystemTrayIcon, QMenu, \
          QMessageBox, QSizePolicy, QPushButton, QHBoxLayout
      from PySide6.QtGui import QIcon, QAction
      import sys
      
      class Application(QApplication):
          def __init__(self, args):
              super().__init__(args)
              
              # Keep application running when all windows are closed
              self.setQuitOnLastWindowClosed(False)
        
              # Create the menu
              menu = QMenu()
      
              # Create quit button
              btn_quit = QAction('Quit')
              btn_quit.triggered.connect(QApplication.quit)
      
              # Add buttons to menu
              menu.addAction(btn_quit)
      
              # Create system tray icon with menu
              # https://doc.qt.io/qtforpython/PySide6/QtWidgets/QSystemTrayIcon.html
              system_tray = QSystemTrayIcon()
      
              # Set system tray icon (make sure path is correct, otherwise icon is not created)
              system_tray.setIcon(QIcon("icon.png"))
              
              # Add the menu to the system tray
              system_tray.setContextMenu(menu)
      
              # Make system tray icon with menu visible
              system_tray.setVisible(True)
      
              # Show system tray
              system_tray.show()
              
              # Workaround to place the loop here
              # sys.exit(self.exec())
              
      
      def main():
          app = Application(sys.argv)
          
          # Ubuntu: Menu in system tray not created
          # Windows: Icon in system tray not created
          sys.exit(app.exec())
      
      
      if __name__ == '__main__':
          main()
      

      Documentation note is followed:

      > The system tray icon does not take ownership of the menu. You must ensure that it is deleted at the appropriate time by, for example, creating the menu with a suitable parent object.

      Why can't app.exec() placed in main()?

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            crmaurei Cristian Maureira-Fredes
            erriez Erriez Ew
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes