qt_import_plugins

指定要导入或排除的自定义插件集。

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

find_package(Qt6 REQUIRED COMPONENTS Core)

此命令在 Qt 5.14 中引入。

简介

qt_import_plugins(target
                  [INCLUDE plugin ...]
                  [EXCLUDE plugin ...]
                  [INCLUDE_BY_TYPE plugin_type plugin ...]
                  [EXCLUDE_BY_TYPE plugin_type]
                  [NO_DEFAULT]
)

如果禁用了无版本命令,请使用qt6_import_plugins() 代替。它支持与此命令相同的参数集。

说明

指定要导入的自定义插件集。可选参数INCLUDE,EXCLUDE,INCLUDE_BY_TYPE, 和EXCLUDE_BY_TYPE, 可多次使用。

  • INCLUDE - 可用于指定要导入的插件列表。
  • EXCLUDE - 可用于指定要排除的插件列表。
  • INCLUDE_BY_TYPE - 可用于覆盖特定插件类型的导入插件列表。
  • EXCLUDE_BY_TYPE - 可用于指定要排除的插件类型;这样就不会导入该类型的插件。
  • NO_DEFAULT - 防止自动包含默认插件(例如,默认平台插件)。

Qt XML 提供的插件类型有imageformats,platforms, 和sqldrivers

动态插件

如果插件是动态库,则该函数可控制插件的部署。例如,使用该函数,您可以排除将特定插件类型打包到 Android APK 中:

qt_add_executable(MyApp ...)
...
qt_import_plugins(MyApp EXCLUDE_BY_TYPE imageformats)

在上面的代码段中,部署MyApp 时将排除所有具有imageformats 类型的插件。生成的 Android APK 将不包含任何imageformats 插件。

如果不使用该命令,目标程序会自动部署属于目标程序所链接的 Qt 模块的所有插件。

静态插件

如果未使用该命令,目标机将自动针对目标机所链接的每个 Qt 模块,链接一组合理的默认静态插件。更多信息,请参阅target_link_libraries

每个插件都带有一个 C++ 存根文件,可自动初始化静态插件。因此,任何与插件链接的目标都会将该 C++ 文件添加到其SOURCES 中。

示例

qt_add_executable(myapp main.cpp)
target_link_libraries(myapp Qt6::Gui Qt6::Sql)
qt_import_plugins(myapp
    INCLUDE Qt6::QCocoaIntegrationPlugin
    EXCLUDE Qt6::QMinimalIntegrationPlugin
    INCLUDE_BY_TYPE imageformats Qt6::QGifPlugin Qt6::QJpegPlugin
    EXCLUDE_BY_TYPE sqldrivers
)

在上面的代码段中,可执行的myapp 发生了以下情况:

  • Qt6::QCocoaIntegrationPlugin 被导入到 myapp 中。
  • Qt6::QMinimalIntegrationPlugin 插件不被自动导入到 myapp 中。
  • imageformats 的默认插件列表被覆盖,只包括Qt6::QGifPluginQt6::QJpegPlugin
  • 所有sqldrivers 插件都不会被自动导入。

另请参阅 qt_import_qml_plugins()

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