Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Pyside6: About box in SystemTray

Pyside6: About box in SystemTray

Scheduled Pinned Locked Moved Unsolved Qt for Python
3 Posts 2 Posters 691 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Slab
    wrote on last edited by
    #1

    Hi,

    I am trying to create about box for a system tray application I am making. However, whenever I close about box the whole application quits. Can somebody guide me what needs to be done? Following is piece of code giving me trouble.

    import os
    import subprocess
    import sys
    import time
    from PySide6 import QtWidgets, QtGui
    from PySide6.QtCore import QObject, QThread, Signal
    
    class SystemTrayIcon(QtWidgets.QSystemTrayIcon):
        ICON_PATH = os.path.join(os.path.dirname(
            os.path.abspath(__file__)), 'icons')
    
        def __init__(self, icon, parent=None):
            QtWidgets.QSystemTrayIcon.__init__(self, icon, parent)
            self.setToolTip(f'Tray Service')
    
            menu = self.create_menu()
            self.setContextMenu(menu)
    
        def create_menu(self):
            menu = QtWidgets.QMenu()
    
            self.add_menu_item(menu, 'About', self.open_about, 'icon')
            self.add_menu_item(menu, 'Restart', self.restart, 'restart')
            self.add_menu_item(menu, 'Exit', self.exit)
            return menu
    
        def add_menu_item(self, menu, title, handler, icon=''):
            item = menu.addAction(title)
            item.triggered.connect(handler)
    
            if icon == '':
                icon = os.path.join(self.ICON_PATH, title.lower())
            elif not os.path.exists(icon):
                icon = os.path.join(self.ICON_PATH, icon + '.png')
            item.setIcon(QtGui.QIcon(icon))
    
        def exit(self):
            print('Exit called')
            sys.exit()
    
        def restart(self):
            print('Restarting')
            subprocess.Popen(['python', __file__])
            sys.exit()
    
        def open_about(self):
            QtWidgets.QMessageBox.about(None, 'About Tray Service',
                                        'Copyright message')
    
    
    def main():
        application = QtWidgets.QApplication(sys.argv)
        if SystemTrayIcon.isSystemTrayAvailable():
            tray_icon = SystemTrayIcon(QtGui.QIcon(
                os.path.join(SystemTrayIcon.ICON_PATH, 'icon' + '.png')))
            tray_icon.show()
    
        sys.exit(application.exec())
    
    
    if __name__ == '__main__':
        main()
    
    
    JonBJ 1 Reply Last reply
    0
    • S Slab

      Hi,

      I am trying to create about box for a system tray application I am making. However, whenever I close about box the whole application quits. Can somebody guide me what needs to be done? Following is piece of code giving me trouble.

      import os
      import subprocess
      import sys
      import time
      from PySide6 import QtWidgets, QtGui
      from PySide6.QtCore import QObject, QThread, Signal
      
      class SystemTrayIcon(QtWidgets.QSystemTrayIcon):
          ICON_PATH = os.path.join(os.path.dirname(
              os.path.abspath(__file__)), 'icons')
      
          def __init__(self, icon, parent=None):
              QtWidgets.QSystemTrayIcon.__init__(self, icon, parent)
              self.setToolTip(f'Tray Service')
      
              menu = self.create_menu()
              self.setContextMenu(menu)
      
          def create_menu(self):
              menu = QtWidgets.QMenu()
      
              self.add_menu_item(menu, 'About', self.open_about, 'icon')
              self.add_menu_item(menu, 'Restart', self.restart, 'restart')
              self.add_menu_item(menu, 'Exit', self.exit)
              return menu
      
          def add_menu_item(self, menu, title, handler, icon=''):
              item = menu.addAction(title)
              item.triggered.connect(handler)
      
              if icon == '':
                  icon = os.path.join(self.ICON_PATH, title.lower())
              elif not os.path.exists(icon):
                  icon = os.path.join(self.ICON_PATH, icon + '.png')
              item.setIcon(QtGui.QIcon(icon))
      
          def exit(self):
              print('Exit called')
              sys.exit()
      
          def restart(self):
              print('Restarting')
              subprocess.Popen(['python', __file__])
              sys.exit()
      
          def open_about(self):
              QtWidgets.QMessageBox.about(None, 'About Tray Service',
                                          'Copyright message')
      
      
      def main():
          application = QtWidgets.QApplication(sys.argv)
          if SystemTrayIcon.isSystemTrayAvailable():
              tray_icon = SystemTrayIcon(QtGui.QIcon(
                  os.path.join(SystemTrayIcon.ICON_PATH, 'icon' + '.png')))
              tray_icon.show()
      
          sys.exit(application.exec())
      
      
      if __name__ == '__main__':
          main()
      
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Slab said in Pyside6: About box in SystemTray:

      However, whenever I close about box the whole application quits.

      I would assume this is due to quitOnLastWindowClosed : bool:

      This property holds whether the application implicitly quits when the last window is closed.

      The default is true.

      If this property is true, the applications quits when the last visible primary window (i.e. window with no parent) is closed.

      Try setQuitOnLastWindowClosed(False).

      1 Reply Last reply
      1
      • S Offline
        S Offline
        Slab
        wrote on last edited by
        #3

        @JonB said in Pyside6: About box in SystemTray:

        setQuitOnLastWindowClosed

        Perfect! Thanks!

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved