qt_policy
修改 Qt CMake API 的默认行为。
该命令定义在Qt6 包的Core 组件中,可按如下方式加载:
find_package(Qt6 REQUIRED COMPONENTS Core)该命令于 Qt 6.5 中引入。
语法
qt_policy(
[SET <policy_name> behavior]
[GET <policy_name> <variable>]
)如果禁用了无版本命令,请改用qt6_policy() 。它支持与本命令相同的参数集。
描述
该命令有两种模式:
- 当使用
SET关键字时,此命令可用于选择采用Qt的CMake API中的行为变更,或明确选择不采用这些变更。 - 当使用
GET关键字时,<variable>将被设置为该策略的当前行为,即OLD或NEW。
<policy_name> 必须是Qt CMake 策略之一的名字。策略名称的格式为QTP<NNNN> ,其中 <NNNN> 是指定策略索引的整数。使用无效的策略名称会导致错误。
支持旧版 Qt 的代码在获取<policy_name> 的值或设置其行为之前,可通过检查QT_KNOWN_POLICY_<policy_name> 变量的值来确认该策略是否存在。
if(QT_KNOWN_POLICY_<policy_name>)
qt_policy(SET <policy_name> NEW)
endif()您可以将behavior 设置为以下选项之一:
NEW以启用新行为OLD以明确选择不采用新行为
注意: 策略的OLD 行为 已弃用,未来可能会被移除。
可用策略
':/qt/qml/' 是 Qml 模块的默认资源前缀 | |
指定 Android 特定路径的目标属性可能包含生成器表达式。 | |
创建 Qt 库时,请考虑 BUILD_SHARED_LIBS 的值。 | |
QML 模块中包含 QML 文件的额外目录需要额外的 qmldir 文件 | |
让 qt_add_qml_module() 依赖关系关键字接受 CMake 目标 | |
将 PRIVATE_CODE 设为 Wayland 协议源代码生成的默认值。 | |
在生成的部署脚本中,对引号和转义字符进行转义处理。 | |
在不重新评估的情况下转发部署脚本的参数。 |
© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.