1.需求
需要实现程序操作过程中的重新启动,即经常说的重新登录功能
2.解决
在main函数中检测exec的返回值决定是关闭还是重启,使用注册函数atexit(relogin)来实现这个功能
3.代码
main.cpp
<code class="hljs" vala="">#include myrelogin.h#include<qtwidgets qapplication="">#include<qprocess>QString gstrFilePath = ;void relogin(void);int main(int argc, char *argv[]){ QApplication a(argc, argv); gstrFilePath = QCoreApplication::applicationFilePath(); myrelogin w; w.show(); int nret = a.exec(); if (nret == 2) { atexit(relogin); } return nret;}//这里启用重新启动一个新的示例void relogin(){ QProcess process; process.startDetached(gstrFilePath);}</qprocess></qtwidgets></code>函数调用
<code class="hljs" cpp="">#include<qtcore qcoreapplication="">qApp->exit(2);</qtcore></code>
4.备注
1.atexit函数比较有用mfc上也可以采用这个做重新登录
2.在win7+vs2010+qt5.40编译运行正常