导入目标

每个加载的 Qt 模块都定义了一个 CMake 库目标。目标名称以Qt6:: 开头,后跟模块名称。例如Qt6::Core,Qt6::Gui 。将目标库的名称传入target_link_libraries ,即可使用相应的库。

注意: 目标也可以使用Qt:: 前缀:Qt::Core,Qt::Gui 等。另请参阅Qt 5 和 Qt 6 兼容性

导入目标创建时的配置与配置 Qt 时的配置相同。也就是说:

  • 如果 Qt 是用-debug 开关配置的,则会创建一个带有 DEBUG 配置的导入目标。
  • 如果 Qt 是通过-release 开关配置的,则会创建具有 RELEASE 配置的导入目标。
  • 如果 Qt 是通过-debug-and-release 开关配置的,则会创建具有 RELEASE 和 DEBUG 配置的导入目标。

如果您的项目有自定义的CMake 构建配置,您必须将自定义配置映射到调试或发布 Qt 配置。

find_package(Qt6 REQUIRED COMPONENTS Core)

set(CMAKE_CXX_FLAGS_COVERAGE "${CMAKE_CXX_FLAGS_RELEASE} -fprofile-arcs -ftest-coverage")

# set up a mapping so that the Release configuration for the Qt imported target is
# used in the COVERAGE CMake configuration.
set_target_properties(Qt6::Core PROPERTIES MAP_IMPORTED_CONFIG_COVERAGE "RELEASE")

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