qt_deploy_qml_imports

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

该命令在Qt6 软件包的Qml 组件中定义,可以像这样加载:

find_package(Qt6 REQUIRED COMPONENTS Qml)

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

警告: 如果您使用的 CMake 版本低于 3.19,请确保向qt6_add_executable() 传递MANUAL_FINALIZATION 选项,然后在调用此函数前调用qt6_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 模块。qt6_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 模块。

默认情况下,导入的 QML 模块的运行时部分将部署到 MacOS 应用程序捆绑目标的Resources/qml 目录,其他平台则部署到基本安装位置下的qml 目录。在非 macOS 情况下,可使用QML_DIR 选项覆盖这一默认选择。

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

示例

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

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