How to restart the application in qt?

I do this to restart my game, but the program has an error. I want to show QDialogwhen user loss. In this QDilagI put two pushbuttonfor repeat and exit. In addition, I have QDialogto start the game. Where is my mistake? (I read similar questions and am doing this, but still I have a problem)

extern  int const EXIT_CODE_REBOOT;
mydialog_end::mydialog_end(QWidget *parent) :
QDialog(parent
{
  retry=new QPushButton(this);
  exit=new QPushButton(this);
  retry->setText("RETRY");
  exit->setText("EXIT");
  connect(retry,SIGNAL(clicked()),this,SLOT(on_retry_clicked()));
  connect(exit,SIGNAL(clicked()),this,SLOT(on_exit_clicked()));
 }
 void mydialog_end::on_retry_clicked()
 {
   qApp->exit(EXIT_CODE_REBOOT);
   accept();
  }
  void mydialog_end::on_exit_clicked()
  {
    //what do i do for end of game?
    reject();
  }
  //////////////in class myenemy///////
  public slots:
  void loss();
  void Myenemy1::loss()
  {
    if(this->collidesWithItem(_mario))
    {
      //do something....
      mydialog_end dialog;
      dialog.exec();
     }
    }
    //////////////in main////////////
 extern int const RESTART_CODE;
 int main(int argc, char *argv[])
{
  Mydialogstart dlg;//a dialog for beginning game
  int state= dlg.exec();
  int return_from_event_loop_code=0;
 do
{
    QApplication a(argc, argv);
    MainWindow w;
    if( state==QDialog::Accepted)
    {
        w.show();
        qDebug()<<"accepted";
    }
    else if(state==QDialog::Rejected)
    {
        qDebug()<<"rejected";
        dlg.close();
        return 0;
    }
    return_from_event_loop_code = a.exec();

} while(return_from_event_loop_code==RESTART_CODE);

  return return_from_event_loop_code;
}
+4
source share
2 answers

You can use QProcess::startDetachedto start an instance of your application in a new process and disconnect from it. After that you should exit the application:

QProcess process;
process.startDetached("myApp",QStringList());

qApp->quit();

Here myAppis the name of the application executable. On Windows, it can be myApp.exe.

+3

... , A, A B. A , B QDialog. Retry, A .

+2

Source: https://habr.com/ru/post/1547316/


All Articles