一、使用 QMAKE_LFLAGS
直接嵌入 UAC 权限
修改 .pro
文件,在您的 Qt 项目文件(.pro
)中添加以下代码:
qmake
win32 {
QMAKE_LFLAGS += /MANIFESTUAC:\"level=\'requireAdministrator\' uiAccess=\'false\'\"
}
仅仅release版本启用UAC权限
win32 {
CONFIG(release, debug|release) {
QMAKE_LFLAGS += /MANIFESTUAC:\"level=\'requireAdministrator\' uiAccess=\'false\'\"
}
}
-
level='requireAdministrator'
:强制要求管理员权限(程序启动时弹出 UAC 提示)。 -
level='asInvoker'
:默认权限(不提升)。 -
level='highestAvailable'
:尽可能提升权限(但不强制)。 -
uiAccess='false'
:禁用 UI 自动化权限(除非需要跨进程 UI 操作,否则保持false
)。
生效方式
-
重新编译项目(
qmake
→构建
)。 -
生成的
.exe
文件运行时 会自动请求管理员权限(UAC 弹窗)。
二、修改程序清单
Windows 程序可以通过 清单文件(Manifest) 声明它需要管理员权限。在 Qt 项目中,您可以通过 .pro
文件配置,使编译后的程序自动请求管理员权限。
步骤
-
在项目目录下创建
app.manifest
文件(如YourApp.manifest
),内容如下:xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"> <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3"> <security> <requestedPrivileges> <requestedExecutionLevel level="requireAdministrator" uiAccess="false"/> </requestedPrivileges> </security> </trustInfo> </assembly>
-
level="requireAdministrator"
:强制要求管理员权限(否则程序无法启动)。 -
level="asInvoker"
:默认权限(不提升)。 -
level="highestAvailable"
:尽可能提升权限(但不强制)。
-
-
在
.pro
文件中添加以下内容:qmake
win32 { QMAKE_LFLAGS += /MANIFEST:EMBED QMAKE_LFLAGS += /MANIFESTINPUT:$$PWD/YourApp.manifest }
-
替换
YourApp.manifest
为您的清单文件名。
-
-
重新编译程序,运行时会自动请求管理员权限。
三、对比:QMAKE_LFLAGS
vs .manifest
文件
方式 | 优点 | 缺点 |
---|---|---|
QMAKE_LFLAGS | 无需额外文件,直接嵌入清单 | 灵活性较低(不能定义其他清单属性) |
.manifest 文件 | 可定制更多清单选项(如兼容性、DPI 感知) | 需要维护单独文件 |
四、含兼容性、DPI 感知等属性的完整 .manifest
文件示例
xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<!-- UAC 管理员权限 -->
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel
level="requireAdministrator"
uiAccess="false"/>
</requestedPrivileges>
</security>
</trustInfo>
<!-- DPI 感知(支持高分辨率屏幕) -->
<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">
PerMonitorV2
</dpiAwareness>
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">
True
</dpiAware>
</windowsSettings>
</application>
<!-- 启用长路径支持(>260字符) -->
<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<longPathAware xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">
true
</longPathAware>
</windowsSettings>
</application>
<!-- 兼容性模式(可选) -->
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<!-- 支持 Windows 10 和 11 -->
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"/> <!-- Win10 -->
<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}"/> <!-- Win11 -->
</application>
</compatibility>
</assembly>
验证清单是否生效
方法 1:使用 mt.exe
检查
sh
mt.exe -inputresource:YourApp.exe -out:extracted.manifest
查看生成的 extracted.manifest
是否包含配置。
方法 2:右键属性 → 兼容性
-
如果清单正确,兼容性选项卡会显示“由应用程序管理”(无法手动修改)。
方法 3:运行测试
-
程序启动时应 自动弹出 UAC 提示(管理员权限)。
-
在高分辨率屏幕上 无模糊显示(DPI 感知生效)。