Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Pyside6: About box in SystemTray



  • 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()
    
    


  • @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).



  • @JonB said in Pyside6: About box in SystemTray:

    setQuitOnLastWindowClosed

    Perfect! Thanks!


Log in to reply