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> 将被设置为策略的当前行为,即OLDNEW

<policy_name> 必须是Qt CMake 策略之一的名称。策略名称的形式为 ,其中 <NNNNN> 是一个整数,指定了策略的索引。使用无效的策略名称会导致错误。QTP<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_standard_project_setup

© 2025 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.