如何在qt中重新启动应用程序? [英] how to restart an application in qt?
问题描述
我这样做是为了重新启动我的游戏,但程序有错误。我想显示一个 QDialog
当用户丢失。在 QDilag
我放两个
按钮
为retry和exit.also我有一个 QDialog
游戏开始。我的错误在哪里? (我读了类似的问题,并根据这些,但我有问题)
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;
}
推荐答案
$ c> QProcess :: startDetached 在新进程中运行应用程序的实例并从其中分离。之后,您应该退出应用程序:
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();
这里 myApp
文件。在Windows上,它可以 myApp.exe
。
Here myApp
is the name of the executable file of the application. On Windows it can be myApp.exe
.
这篇关于如何在qt中重新启动应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!