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

PyQt5 application not restarting itself



  • So I followed this documentation
    https://wiki.qt.io/How_to_make_an_Application_restartable

    I need to make my software restart after doing some updates but it exists successfully but then doesn't start up anymore.

    My code looks like this:

        def restart(self):
    
            qApp.exit(-123456789)
    

    Note: The function is inside the Main class which inherits from QMainWindow



  • did you really follow that documentation? There is nothing magical about the exit code. you have to check it as a loop condition in main. I don't see enough of your code to know whether you did that.



  • Since python does not have a do while loop, here's something I tried

    if __name__ == '__main__':
        app = QApplication(sys.argv)
        current_exit_code = 0
        while True:
            ex = Main()
            ex.setFont(QFont("Consolas", 10))
            ex.setStyleSheet(blue)
            #welcome = Welcome(ex)
            ex.showMaximized()
            current_exit_code = app.exec()
            if current_exit_code == -123456789:
                break
    

    That didn't work either



  • Here's what I did to make it work:

     def restart(self):
    
            os.execl(sys.executable, sys.executable, *sys.argv)
    

    That simple function will restart the whole script



  • reread the documentation you referenced above. You should instantiate QApplication inside your while loop, not outside.



  • @Fuchsiaff
    That's fine, but it's a totally different kind of application "restart" versus what the code in the link is doing :)


Log in to reply