1.需求

须要实现程序操作过程中的又一次启动,即常常说的又一次登录功能

2.解决

在main函数中检測exec的返回值决定是关闭还是重新启动。使用注冊函数atexit(relogin)来实现这个功能

3.代码

main.cpp

#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);
}

函数调用

#include <QtCore/QCoreApplication>
qApp->exit(2);

4.备注

1.atexit函数比較实用mfc上也能够採用这个做又一次登录

2.在win7+vs2010+qt5.40编译执行正常

05-17 10:16