QPluginLoader Class

QPluginLoader 类可在运行时加载插件。更多

头文件: #include <QPluginLoader>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core
继承: QObject

注意:该类中的所有函数都是可重入的

属性

公共函数

QPluginLoader(QObject *parent = nullptr)
QPluginLoader(const QString &fileName, QObject *parent = nullptr)
virtual ~QPluginLoader()
QString errorString() const
QString fileName() const
QObject *instance()
bool isLoaded() const
bool load()
QLibrary::LoadHints loadHints() const
QJsonObject metaData() const
void setFileName(const QString &fileName)
void setLoadHints(QLibrary::LoadHints loadHints)
bool unload()

静态公共成员

QObjectList staticInstances()
QList<QStaticPlugin> staticPlugins()
void qRegisterStaticPluginFunction(QStaticPlugin plugin)

详细说明

QPluginLoader 提供对Qt 插件的访问。Qt 插件存储在共享库(DLL)中,与使用QLibrary 访问的共享库相比,Qt 插件具有这些优点:

  • QPluginLoader 会检查插件是否与应用程序的 Qt 版本相同。
  • QPluginLoader 可直接访问根组件对象 (instance()) ,而不是强制您手动解析 C 函数。

QPluginLoader 对象的实例在单个共享库文件上运行,我们称之为插件。它以与平台无关的方式提供对插件中功能的访问。要指定加载哪个插件,可以在构造函数中传递文件名,或使用setFileName() 进行设置。

最重要的函数是load() 用来动态加载插件文件,isLoaded() 用来检查加载是否成功,instance() 用来访问插件中的根组件。如果插件尚未加载,则instance() 函数会隐式尝试加载插件。多个 QPluginLoader 实例可用于访问同一个物理插件。

插件一旦加载,就会一直保留在内存中,直到 QPluginLoader 的所有实例都已卸载,或应用程序终止。您可以尝试使用unload() 卸载插件,但如果 QPluginLoader 的其他实例正在使用同一个库,调用就会失败,只有当每个实例都调用了unload() 后,卸载才会发生。就在卸载发生之前,根组件也将被删除。

有关如何通过插件扩展应用程序的更多信息,请参阅如何创建 Qt 插件

请注意,如果您的应用程序是静态链接 Qt 的,则不能使用 QPluginLoader。在这种情况下,您还必须静态链接插件。如果需要在静态链接的应用程序中加载动态库,可以使用QLibrary

另请参阅 QLibrary

属性文档

fileName : QString

该属性包含插件的文件名。

我们建议在文件名中省略文件的后缀,因为QPluginLoader 会自动查找具有相应后缀的文件(参见QLibrary::isLibrary() )。

加载插件时,QPluginLoader 会搜索QCoreApplication::libraryPaths() 指定的所有插件位置,除非文件名有绝对路径。成功加载插件后,fileName() 将返回插件的全限定文件名,包括插件的完整路径(如果在构造函数中给出了路径或传递给了 setFileName())。

如果文件名不存在,则不会设置。该属性将包含一个空字符串。

默认情况下,该属性包含一个空字符串。

访问函数:

QString fileName() const
void setFileName(const QString &fileName)

另请参见 load()。

loadHints : QLibrary::LoadHints

load() 函数一些提示,告诉它应该如何运行。

您可以就如何解析插件中的符号给出提示。自 Qt XML 5.7 起,默认设置为QLibrary::PreventUnloadHint

有关该属性如何工作的完整说明,请参阅QLibrary::loadHints 文档。

访问函数:

QLibrary::LoadHints loadHints() const
void setLoadHints(QLibrary::LoadHints loadHints)

另请参见 QLibrary::loadHints

成员函数文档

[explicit] QPluginLoader::QPluginLoader(QObject *parent = nullptr)

用给定的parent 构建插件加载器。

[explicit] QPluginLoader::QPluginLoader(const QString &fileName, QObject *parent = nullptr)

使用给定的parent 构建插件加载器,加载fileName 指定的插件。

要实现可加载,文件的后缀必须是与平台相关的可加载库的有效后缀,例如,Unix 上为.so ,- macOS 和 iOS 上为.dylib ,Windows 上为.dll 。后缀可通过QLibrary::isLibrary() 验证。

另请参见 setFileName() 。

[virtual noexcept] QPluginLoader::~QPluginLoader()

销毁QPluginLoader 对象。

除非明确调用unload() ,否则插件将保留在内存中,直到应用程序终止。

另请参阅 isLoaded() 和unload()。

QString QPluginLoader::errorString() const

返回一个文本字符串,说明上次发生的错误。

QObject *QPluginLoader::instance()

返回插件的根组件对象。如有必要,将加载插件。如果插件无法加载或根组件对象无法实例化,则函数返回nullptr

如果根组件对象已被销毁,调用该函数将创建一个新实例。

QPluginLoader 销毁时,该函数返回的根组件不会被删除。如果要确保根组件被删除,应在不再需要访问核心组件时立即调用unload() 。当库最终卸载时,根组件将被自动删除。

组件对象是一个QObject 。使用qobject_cast() 访问您感兴趣的接口。

另请参阅 load() 。

bool QPluginLoader::isLoaded() const

如果插件已加载,则返回true ;否则返回false

另请参阅 load() 。

bool QPluginLoader::load()

加载插件,如果成功加载则返回true ,否则返回false 。由于instance() 总是在解析任何符号之前调用该函数,因此无需明确调用。在某些情况下,您可能希望提前加载插件,这时就需要使用此函数。

另请参见 unload()。

QJsonObject QPluginLoader::metaData() const

返回此插件的元数据。元数据是在编译插件时使用Q_PLUGIN_METADATA() 宏以 json 格式指定的数据。

元数据可以在不实际加载插件的情况下以快速、低成本的方式进行查询。这样就可以存储插件的功能,并根据这些元数据决定是否加载插件。

[static] QObjectList QPluginLoader::staticInstances()

返回插件加载器持有的静态插件实例(根组件)列表。

另请参见 staticPlugins()。

[static] QList<QStaticPlugin> QPluginLoader::staticPlugins()

返回插件加载器持有的 QStaticPlugins 列表。该函数与staticInstances() 类似,但QStaticPlugin 还包含元数据信息。

另请参见 staticInstances()。

bool QPluginLoader::unload()

卸载插件,如果插件可以卸载,则返回true ;否则返回false

卸载会在应用程序终止时自动进行,因此通常不需要调用此函数。

如果QPluginLoader 的其他实例正在使用同一个插件,则调用将失败,只有当每个实例都调用 unload() 后,才会卸载。

不要尝试删除根组件。相反,unload() 会在需要时自动删除它。

另请参阅 instance() 和load()。

相关非成员

void qRegisterStaticPluginFunction(QStaticPlugin plugin)

注册插件加载器指定的plugin ,并由Q_IMPORT_PLUGIN() 使用。

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