How to Restart Qt Application
How to restart QtApplication
As we know, Restarting Application means to exit current application, then to call another application. So, let's see how exit of one application.
Qt Application
int main(int argc, char** argv)
{
QApplication app(argc, argv);
Widget w;
w.show()
return app.exec();
}
The code snippet,last line return app.exec()
let Qt application in main event loop. if you want to exit main event loop, Just call QCoreApplication::exit(int);
or QCoreApplication::quit();
QApplication
has one property called quitOnLastWindowClosed
, it means the application will exit after last window closed.
About how to close all windows, we can use QApplication::closeAllWindows()
, It is better way for using quit
to exit application. That way, We can let all windows object revice close event, and do some thing before destroy.
Call another Application
In Qt, It is better let QProcess
to do this. It let another application to run and not has father-son relationship.
QProcess::startDetached(qApp->applicationFilePath(), QStringList());
if applicationFilePath not continues space char, we can use
QProcess::startDetached(qApp->applicationFilePath());
Example one
qApp->quit();
QProcess::startDetached(qApp->applicationFilePath(), QStringList());
or
qApp->closeAllWindow();
QProcess::startDetached(qApp->applicationFilePath(), QStringList());
Example two
/**773 = 'r'+'e'+'s'+'t'+'a'+'r'+'t' */
qApp->exit(773);
int main(int argc, char** argv)
{
int ret = app.exec();
if (ret == 773) {
QProcess::startDetached(qApp->applicationFilePath(), QStringList());
return 0;
}
return ret;
}
版权声明:本博文属于作者原创或从其他地方学习而来的博文,未经许可不得转载.
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 现代计算机视觉入门之:什么是图片特征编码
· .NET 9 new features-C#13新的锁类型和语义
· Linux系统下SQL Server数据库镜像配置全流程详解
· 现代计算机视觉入门之:什么是视频
· 你所不知道的 C/C++ 宏知识
· 不到万不得已,千万不要去外包
· C# WebAPI 插件热插拔(持续更新中)
· 会议真的有必要吗?我们产品开发9年了,但从来没开过会
· 【译】我们最喜欢的2024年的 Visual Studio 新功能
· 如何打造一个高并发系统?