问题原因:需要提升用户权限
解决办法:
.cpp文件
void XXX::StartPocess()
{
QDir filedir;
QString filePath,exeName,exepath;;
exeName = "/XXX.exe"; //此处更改为你自己的exe文件
filePath = QCoreApplication::applicationDirPath();//filedir.currentPath();
exepath = filePath + exeName;
#ifdef Q_OS_WIN
SHELLEXECUTEINFO sei = { sizeof(SHELLEXECUTEINFO) };
sei.lpVerb = TEXT("runas");
sei.lpFile = exepath.toStdWString().c_str();
sei.nShow = SW_SHOWNORMAL;//without this,the windows will be hiden
if (!ShellExecuteEx(&sei))
{
DWORD dwStatus = GetLastError();
if (dwStatus == ERROR_CANCELLED)
{
qDebug() <<"提升权限被用户拒绝\n";
}
else if (dwStatus == ERROR_FILE_NOT_FOUND)
{
qDebug() <<"所要执行的文件没有找到\n";
}
}
#else
qDebug()<<"Not windows os!"
return ;
#endif
QStringList arguments;
arguments << "-style" << "fusion";
if(NULL == myProcess)
{
myProcess = new QProcess(this);
myProcess->start(exepath, arguments);
}else {
myProcess->start(exepath, arguments);
}
}
.h文件
#include <QCoreApplication>
#include <qdebug.h>
#ifdef Q_OS_WIN
#include <windows.h>
#endif
//此部分放入类中
QProcess *myProcess;
借鉴:https://blog.csdn.net/github_35960067/article/details/90410523