how to restart my own qt application?

后端 未结 8 1686
再見小時候
再見小時候 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:14

    Here is the code:

    main.cpp:

    int main(int argc, char *argv[])
    {
        int currentExitCode = 0;
    
        do {
         QApplication a(argc, argv);
         MainWindow w;
         w.show();
         currentExitCode = a.exec();
        } while( currentExitCode == MainWindow::EXIT_CODE_REBOOT );
    
        return currentExitCode;
    
    }
    

    mainwindow.h

        class MainWindow : public QMainWindow
        {
            Q_OBJECT
    
        public:
            explicit MainWindow(QWidget *parent = 0);
            static int const EXIT_CODE_REBOOT;//THIS IS THE IMPORTANT THING TO ADD TO YOUR CODE
            ~MainWindow();
        private slots:
            void slotReboot();//AND THIS ALSO
    
        //ALL THE OTHER VARIABLES
        }
    

    The slotReboot() is the slot that will receive the signal of the QAction I'm going to show in the mainwindow.cpp

    mainwindow.cpp

    First initialize EXIT_CODE_REBOOT :

    int const MainWindow::EXIT_CODE_REBOOT = -123456789;
    

    and declare a QAction pointer:

    QAction* actionReboot;
    

    then in the MainWindow constructor:

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
         actionReboot = new QAction( this );
         actionReboot->setText( tr("Restart") );
         actionReboot->setStatusTip( tr("Restarts the application") );
         connect( actionReboot, SIGNAL (triggered()),this, SLOT (slotReboot()));
    }
    

    And finally you need to send the signal (in the part of your code you need), in this way:

    actionReboot->trigger();
    

    I did the code I showed following these instructions: How to make an application restartable - Qt Wiki

    0 讨论(0)
  • 2020-11-30 10:15

    I'm taking the other answers solutions, but better. No need for pointers, but there is a need for a ; after the while statement of a do { ... } while( ... ); construct.

    int main(int argc, char *argv[])
    {
        const int RESTART_CODE = 1000;
    
        do
        {
            QApplication app(argc, argv);
            MainWindow main_window(app);
        } while( app.exec() == RESTART_CODE);
    
        return return_from_event_loop_code;
    }
    
    0 讨论(0)
  • 2020-11-30 10:15

    Assuming that 1337 is your restart code:

    main.cxx

    int main(int argc, char * argv[])
    {  
      int result = 0;
    
      do
      {
         QCoreApplication coreapp(argc, argv);
         MyClass myObj;
         result = coreapp.exec();
      } while( result == 1337 );
    
      return result;
    }
    

    myClass.cxx

    qApp->exit(1337);
    
    0 讨论(0)
  • 2020-11-30 10:16

    To restart application, try:

    #include <QApplication>
    #include <QProcess>
    
    ...
    
    // restart:
    qApp->quit();
    QProcess::startDetached(qApp->arguments()[0], qApp->arguments());
    
    0 讨论(0)
  • 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:
        //    <base-path>/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
    }
    
    0 讨论(0)
  • 2020-11-30 10:24

    Take a look at How to restart an application thread on qtcentre.org, where muisei gives this code

    #define RESTART_CODE 1000
    int main(int argc, char *argv[])
    {
      int return_from_event_loop_code;
      QPointer<QApplication> app;
      QPointer<MainWindow> main_window;
      do
      {
        if(app) delete app;
        if(main_window) delete main_window;
    
        app = new QApplication(argc, argv);
        main_window = new MainWindow(app);
        return_from_event_loop_code = app->exec();
      }
      while(return_from_event_loop_code==RESTART_CODE)
    
      return return_from_event_loop_code;
    }
    
    0 讨论(0)
提交回复
热议问题