Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to restart an application in qt?

I do this works for restarting my game but program has error .I want to show a QDialog when user losses .In this QDilag i put two pushbutton for retry and exit.also i have a QDialog for beginning of game.Where is my mistake? (I read similar questions and do according these but yet i have 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;
}
like image 565
maryam Avatar asked Jul 06 '14 14:07

maryam


People also ask

How do I restart a program in C#?

The easiest way to restart an application in C# is to use the Application. Restart() function. The Application. Restart() function is used to restart an application in C#.

How do I change my Qt application name?

You can go to the Designer in Qt Creator and change the title of the object in settings,which are on the right side.


3 Answers

You can use QProcess::startDetached to run an instance of your application in a new process and detach from it. After that you should exit the application :

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

qApp->quit();

Here myApp is the name of the executable file of the application. On Windows it can be myApp.exe.

like image 185
Nejat Avatar answered Oct 10 '22 10:10

Nejat


On this one, I would make a little inception... let's say your main application is called A then you should run A in a global B application. When A crashes, B throws the QDialog. If the use click on Retry then kill the old instance of A and start a new one.

like image 2
Thomas Ayoub Avatar answered Oct 10 '22 11:10

Thomas Ayoub


There is a Qt Wiki entry that explains what you need to do in quite a lot of detail.

As it seems you have at least partially taken inspiration from there, but from what you post here, you seem to have never initialized the values for EXIT_CODE_REBOOT and RESTART_CODE in your code sample, or at least linked them to one another (which I would expect you'd want to do in some way)

like image 1
Sty Avatar answered Oct 10 '22 11:10

Sty