how to restart my own qt application?

37.1k views Asked by At

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

Can somebody please show me an example?

10

There are 10 answers

3
Piotr Dobrogost On

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;
}
4
masoud On

To restart application, try:

#include <QApplication>
#include <QProcess>

...

// restart:
qApp->quit();
QProcess::startDetached(qApp->arguments()[0], qApp->arguments());
0
Miguel Angel Pons On

You can use my open source library:

https://marketplace.qt.io/products/main-loop-wdt-for-qt-qml

It's a watchdog timer for the main qt loop, but I have a function for forced reboot, with different strategies: startdetached + exit, exec system call on Linux / macOS, and delayed restart (for example, exit and restart after 3 seconds)

1
Angie Quijano On

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
trig-ger On

To restart a running Qt application (at least in Qt 5.15.2) you can do the following:

#include <QApplication>
#include <QProcess>

//...

QString program = qApp->arguments()[0];
QStringList arguments = qApp->arguments().mid(1); // remove the 1st argument - the program name
qApp->quit();
QProcess::startDetached(program, arguments);
0
Eike On

This slight variation on Rubenvb's idea works with PyQt. clearSettings is the method that triggers the restart.

class GuiMain

    #Most of implementation missing

    def clearSettings(self):
        #Clearing the settings missing
        QApplication.exit(GuiMain.restart_code)

    restart_code = 1000

    @staticmethod
    def application_main():
        """
        The application's main function. 
        Create application and main window and run them.
        """
        while True:
            app = QApplication(sys.argv)
            window = GuiMain()
            window.show()
            ret = app.exec_()
            if ret != GuiMain.restart_code:
                break
            del window
            del app
0
braggPeaks On

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);
2
Fivos Vilanakis On

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
}
8
rubenvb On

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;
}
2
Aise On

Doing a real process restart without subclassing:

QCoreApplication a(argc, argv);
int returncode = a.exec();
if (returncode == -1)
{
  QProcess* proc = new QProcess();
  proc->start(QCoreApplication::applicationFilePath());
}
return returncode;

Edit for Mac OS like earlier example.

To restart call

QCoreApplication::exit(-1);

somewhere in your code.