1.在windows环境下,我们通过QProcess仅仅能够启动应用程序,却无法以管理员身份运行该可执行文件。
2.上代码。需要包含头文件
#ifdef Q_OS_WIN
#include <windows.h>
#endif
#ifdef Q_OS_WIN
QString myexe = (path + "/" + program);
SHELLEXECUTEINFO sei = { sizeof(SHELLEXECUTEINFO) };
sei.lpVerb = TEXT("runas");
sei.lpFile = myexe.toStdWString().c_str();
sei.nShow = SW_SHOWNORMAL;//without this,the windows will be hiden
if (!ShellExecuteEx(&sei))
{
DWORD dwStatus = GetLastError();
if (dwStatus == ERROR_CANCELLED)
{
printf("提升权限被用户拒绝\n");
}
else if (dwStatus == ERROR_FILE_NOT_FOUND)
{
printf("所要执行的文件没有找到\n");
}
}
#else
qDebug()<<"Not windows os!"
return ;
#endif