how to restart my own qt application?

后端 未结 8 1783
再見小時候
再見小時候 2020-11-30 09:39

i just asking myself how to restart my own qt application?

Can somebody please show me an example?

8条回答
  •  没有蜡笔的小新
    2020-11-30 10:18

    I just used the method described above and I noticed that my application crashes on restart. ...then I switched the following lines of code:

    if(app) delete app;
    if(main_window) delete main_window;
    

    to:

    if(main_window) delete main_window;
    if(app) delete app;
    

    and it behaves OK. For some reason the window must be deleted first. Just a note for future readers.


    EDIT: ...and a different approach for those who want a real process-restart: You can declare a myApp::Restart() method in your subclass of QApplication. The following version works OK on both MS-Windows & MacOS:

    // Restart Application
    void myApp::Restart(bool Abort)
    {
        // Spawn a new instance of myApplication:
        QProcess proc;
    #ifdef Q_OS_WIN
        proc.start(this->applicationFilePath());
    #endif    
    
    #ifdef Q_OS_MAC
        // In Mac OS the full path of aplication binary is:
        //    /myApp.app/Contents/MacOS/myApp
        QStringList args;
        args << (this->applicationDirPath() + "/../../../myApp.app");
        proc.start("open", args);
    #endif
    
        // Terminate current instance:
        if (Abort) // Abort Application process (exit immediattely)
            ::exit(0);
        else
            this->exit(0); // Exit gracefully by terminating the myApp instance
    }
    

提交回复
热议问题