您的位置:首页 > 编程语言 > Qt开发

qt实现重新登录

2015-07-14 17:33 351 查看

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编译运行正常

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  qt 重新登录