qt_import_qml_plugins
确保在静态构建时导入目标所需的 QML 插件。
该命令在 Qt 6.0 中引入。
该命令定义在Qt6 包的Qml 组件中,可按以下方式加载:
find_package(Qt6 REQUIRED COMPONENTS Qml)用法
qt_import_qml_plugins(target)如果禁用了无版本号的命令,请改用 `qt6_import_qml_plugins() `。它支持与本命令相同的参数集。
说明
注意:此 命令仅在 Qt 采用静态链接方式构建时才生效。若使用非静态 Qt 调用,则不会执行任何操作并立即返回。
qt_import_qml_plugins() 该命令会立即作为调用的一部分,在target 上运行qmlimportscanner (除非在定义QML模块时,已向qt_add_qml_module()传递了NO_IMPORT_SCAN 选项)。它会查找target 所使用的静态QML插件,并将该模块链接到这些插件,从而使它们成为target 所代表的可执行文件或共享库的一部分。 该搜索会递归地追踪 QML 模块的导入。
由于对qmlimportscanner 的调用发生在 configure 阶段,而非生成或构建阶段,因此qt_import_qml_plugins() 仅了解在调用qt_import_qml_plugins() 时target (或其链接或导入的其他目标)上记录的信息。在此调用之后添加的任何链接或导入关系都不会被考虑。 因此,应在target 的目录作用域内尽可能晚地调用此命令,以确保所有链接和导入关系均已被识别。
如果target 是使用qt_add_executable()创建的,项目通常无需直接调用qt_import_qml_plugins() 。 当 Qt 以静态方式构建时,如果target 链接到 Qml 库,则该命令会作为目标最终化过程的一部分自动调用。默认情况下,此最终化过程发生在创建target 的同一目录作用域结束时。如果target 是使用标准的 CMakeadd_executable() 命令创建的,则项目需要自行调用qt_import_qml_plugins() 。
另请参阅 Q_IMPORT_QML_PLUGIN 。
© 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.