How to restart qt application after crash?

Is it possible to restart the qt application after it crashes? As well as those Windows services that will restart on their own. If so, how can I do this? I tried the code like this:

#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;
}

      

But it doesn't work ... What should I do now?

+3


source to share


1 answer


create another qt app that launches your app.

#include <QApplication>
#include "QProcess"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QProcess aProcess;
    QString exe = "Your process name with exact path";
    QByteArray myOut;
    while(1)
    {
        aProcess.start(exe);
        //aProcess.waitForStarted();
        //myOut = aProcess.readAllStandardOutput();
        //printf("%s",myOut.constData());
        aProcess.waitForFinished(-1);

        //QString output(aProcess.readAllStandardOutput());

        myOut = aProcess.readAllStandardOutput();
        printf("%s",myOut.constData());
    }
    enter code here
    return a.exec();
}

      



this program will restart your application when it crashed or closed

+2


source







All Articles