其实很简单,其实并不难。- 《空白格》
QTCreator
mingw32编译器
创建 menifest.xml
并添加到工程。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity version="1.0.0.0" processorArchitecture="X86" name="applicationname" type="win32"></assemblyIdentity>
<description>Description of application</description>
<!-- Identify the application security requirements. -->
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges>
<requestedExecutionLevel level="requireAdministrator" uiAccess="false"></requestedExecutionLevel>
</requestedPrivileges>
</security>
</trustInfo>
</assembly>
pro 中添加:
win32 {
QMAKE_POST_LINK += mt -manifest $$PWD/menifest.xml -outputresource:$$OUT_PWD/release/$$TARGET".exe" $$escape_expand(\n\t)
}
首先找到 mt.exe
并添加到环境变量 ,添加完记得重启生效 验证 mt
:
若实在捋不清路径:
pro
中添加以下语句打印验证
message($$OUT_PWD/release/$$TARGET".exe")
- 将
menifest.xml
移到至test.exe
所在路径下,执行
mt -manifest menifest.xml -outputresource:test.exe
MSVC编译器
pro中加入:
QMAKE_LFLAGS_WINDOWS += "/MANIFESTUAC:\"level='requireAdministrator' uiAccess='false'\""
若修改后无错误也无效,修改一下构建目录即可。
修改完构建目录后,若出现 启动程序失败,路径或者权限错误 的问题,则需进行以下操作:
-
先删除构建目录下的所以文件,包括exe;并注释掉
QMAKE_LFLAGS_WINDOWS
,以确保不是该宏引起的错误。 -
重新构建成功后,再删除构建目录下的所有文件(包括exe)再加上
QMAKE_LFLAGS_WINDOWS
重新构建。
QT+VS
VS中实现很简单,一个配置即可: