本页内容

qt_deploy_qml_imports

部署可执行文件所需的 QML 模块的 Runtime 组件。

该命令定义在Qt6 包的Qml 组件中,可按如下方式加载:

find_package(Qt6 REQUIRED COMPONENTS Qml)

与 Qt 提供的其他大多数 CMake 命令不同,qt_deploy_qml_imports 只能从部署脚本中调用。项目无法直接调用它。

警告:若 您使用的 CMake 版本低于 3.19,请确保在调用此函数之前,先向`qt_add_executable()` 传递 `MANUAL_FINALIZATION ` 选项,并调用`qt_finalize_target()`。

语法概述

qt_deploy_qml_imports(
    TARGET target
    [QML_DIR qml_dir]
    [PLUGINS_FOUND var_name]
    [NO_QT_IMPORTS]
)

描述

注意: 通常无需直接调用此 命令。它由其他更高级别的命令在内部使用,但希望实现更多自定义部署逻辑的项目可能会发现它很有用。

在安装使用 QML 的应用程序时,确定哪些 QML 模块以及这些 QML 模块的哪些部分也需要安装,可能并非易事。 由于 QML 插件并未直接链接到应用程序的可执行文件,因此qt_deploy_runtime_dependencies()无法找到这些 QML 模块。qt_deploy_qml_imports() 命令提供了必要的逻辑,该逻辑与qt_deploy_runtime_dependencies()相辅相成,并部署应用程序所导入的所有 QML 模块的运行时组件。

TARGET 选项是必需的,应指定一个target ,该文件既是一个可执行文件(在macOS上应为应用程序包),同时也是一个QML模块。所有通过qt_add_qml_module() 或qt_target_qml_sources()添加到target 中的QML源文件都将被递归扫描,以查找QML导入语句。 调用qt_add_qml_module() 时,不得指定NO_IMPORT_SCAN 选项。来自导入的 QML 模块的qmldir 文件和插件将被部署。可以指定NO_QT_IMPORTS 选项,以跳过部署 Qt 提供的任何 QML 模块。

默认情况下,对于 macOS 应用包目标,导入的 QML 模块的运行时组件将部署到Resources/qml 目录;对于其他平台,则部署到基础安装位置下的qml 目录。对于非 macOS 情况,可使用QML_DIR 选项来覆盖此默认选择。

如果指定了PLUGINS_FOUND 选项,该命令会将部署的所有QML插件列表存储在由该选项命名的变量中。该变量通常作为ADDITIONAL_MODULES 参数传递给后续的qt_deploy_runtime_dependencies()调用。

示例

cmake_minimum_required(VERSION 3.16...3.22)
project(MyThings)

find_package(Qt6 6.3 REQUIRED COMPONENTS Core Qml)
qt_standard_project_setup()

qt_add_executable(MyApp main.cpp)
qt_add_qml_module(MyApp
    URI Application
    VERSION 1.0
    QML_FILES main.qml MyThing.qml
)

# The following script must only be executed at install time
set(deploy_script "${CMAKE_CURRENT_BINARY_DIR}/deploy_MyApp.cmake")

file(GENERATE OUTPUT ${deploy_script} CONTENT "
include(\"${QT_DEPLOY_SUPPORT}\")
qt_deploy_qml_imports(
    # Deploy QML modules used by MyApp
    TARGET MyApp

    # The found QML plugins are stored in the plugins_found variable
    PLUGINS_FOUND plugins_found

    # The QML modules will be deployed into a custom directory
    QML_DIR \"myqmldir\"

    # Qt QML modules will be skipped, only project-created QML modules will be deployed
    NO_QT_IMPORTS
)
# Deploy application runtime dependencies and runtime dependencies
# of the found QML module plugins.
qt_deploy_runtime_dependencies(
    EXECUTABLE $<TARGET_FILE:MyApp>
    ADDITIONAL_MODULES \${plugins_found}
)
")

install(TARGETS MyApp)
install(SCRIPT ${deploy_script})

另请参阅 qt_generate_deploy_qml_app_script()、qt_deploy_runtime_dependencies() 以及QT_DEPLOY_QML_DIR。

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