QPluginLoader Class

QPluginLoader 在运行时加载插件. 更多...

头文件: #include <QPluginLoader>
qmake: QT += core
基类: QObject

Note: All functions in this class are reentrant.

属性

公有函数

QPluginLoader(QObject *parent = Q_NULLPTR)
QPluginLoader(const QString &fileName, QObject *parent = Q_NULLPTR)
~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()
  • 32 个公有函数继承自 QObject

静态公有成员

QObjectList staticInstances()
QVector<QStaticPlugin> staticPlugins()
  • 11 个静态公有成员继承自 QObject
void qRegisterStaticPluginFunction(QStaticPlugin plugin)

其他继承的成员

  • 1 个公有槽函数继承自 QObject
  • 2 个信号继承自 QObject
  • 9 个受保护的函数继承自 QObject

详细描述

QPluginLoader 在运行时加载插件.

QPluginLoader 提供对 Qt 插件的访问. Qt 插件存储在共享库 (DLL) 中, 而相比使用 QLibrary 访问的共享库, 它具有以下优点:

  • QPluginLoader 检查插件是否链接到与应用程序相同的 Qt 版本.
  • QPluginLoader 提供对根组件对象的直接访问 (instance()), 而无需手动解析C函数.

QPluginLoader 对象的实例在被称为插件的单个共享库文件上运行. 它以独立于平台的方式提供对插件中功能的访问. 要指定加载的插件, 可以在构造函数中传递文件名, 或者通过 setFileName()设置.

最重要的函数有: 用来动态加载插件文件的 load(), 用来检查加载是否成功的 isLoaded(), 以及用来访问插件根组件的 instance(). 如果尚未加载插件, 则 instance() 函数会隐式尝试加载该插件. 可以使用 QPluginLoader 的多个实例来访问同一个实际的插件.

加载后, 插件将保留在内存中, 直到所有 QPluginLoader 实例都已卸载, 或者应用程序终止为止. 你可以使用 unload()尝试卸载插件, 但如果有其它 QPluginLoader 实例正在使用同一个库, 那么这一函数调用会失败, 而当所有实例都调用了 unload()后插件才会真正被卸载. 在卸载发生之前, 根组件也将被删除.

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

注意, 如果你的应用程序与 Qt 静态链接, 则无法使用 QPluginLoader. 在这种情况下,你还必须静态链接到插件. 如果需要在静态链接的应用程序中加载动态库, 则可以使用 QLibrary.

参见 QLibraryPlug & Paint Example.

属性

fileName : QString

该属性记录插件的文件名

我们建议在文件名中省略文件的后缀, 因为 QPluginLoader 将自动查找具有适当后缀的文件 (参见 QLibrary::isLibrary()).

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

如果文件名不存在, 改属性将不会设置, 并包含一个空字符串.

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

访问函数:

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

参见 load().

loadHints : QLibrary::LoadHints

load() 函数提供一些有关其行为方式的提示.

你可以提供有关如何解析插件中符号的提示. 从 Qt 5.7 起, 默认设置为 QLibrary::PreventUnloadHint is set.

有关该属性如何工作的完整说明, 参见 QLibrary::loadHints.

该属性在 Qt 4.4 中引入.

访问函数:

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

参见 QLibrary::loadHints.

成员函数

QPluginLoader::QPluginLoader(QObject *parent = Q_NULLPTR)

Constructs a plugin loader with the given parent.

QPluginLoader::QPluginLoader(const QString &fileName, QObject *parent = Q_NULLPTR)

Constructs a plugin loader with the given parent that will load the plugin specified by fileName.

To be loadable, the file's suffix must be a valid suffix for a loadable library in accordance with the platform, e.g. .so on Unix, - .dylib on macOS and iOS, and .dll on Windows. The suffix can be verified with QLibrary::isLibrary().

参见 setFileName().

QPluginLoader::~QPluginLoader()

Destroys the QPluginLoader object.

Unless unload() was called explicitly, the plugin stays in memory until the application terminates.

参见 isLoaded() and unload().

QString QPluginLoader::errorString() const

Returns a text string with the description of the last error that occurred.

This function was introduced in Qt 4.2.

QObject *QPluginLoader::instance()

Returns the root component object of the plugin. The plugin is loaded if necessary. The function returns 0 if the plugin could not be loaded or if the root component object could not be instantiated.

If the root component object was destroyed, calling this function creates a new instance.

The root component, returned by this function, is not deleted when the QPluginLoader is destroyed. If you want to ensure that the root component is deleted, you should call unload() as soon you don't need to access the core component anymore. When the library is finally unloaded, the root component will automatically be deleted.

The component object is a QObject. Use qobject_cast() to access interfaces you are interested in.

参见 load().

bool QPluginLoader::isLoaded() const

Returns true if the plugin is loaded; otherwise returns false.

参见 load().

bool QPluginLoader::load()

Loads the plugin and returns true if the plugin was loaded successfully; otherwise returns false. Since instance() always calls this function before resolving any symbols it is not necessary to call it explicitly. In some situations you might want the plugin loaded in advance, in which case you would use this function.

参见 unload().

QJsonObject QPluginLoader::metaData() const

Returns the meta data for this plugin. The meta data is data specified in a json format using the Q_PLUGIN_METADATA() macro when compiling the plugin.

The meta data can be queried in a fast and inexpensive way without actually loading the plugin. This makes it possible to e.g. store capabilities of the plugin in there, and make the decision whether to load the plugin dependent on this meta data.

[static] QObjectList QPluginLoader::staticInstances()

Returns a list of static plugin instances (root components) held by the plugin loader.

参见 staticPlugins().

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

Returns a list of QStaticPlugins held by the plugin loader. The function is similar to staticInstances() with the addition that a QStaticPlugin also contains meta data information.

参见 staticInstances().

bool QPluginLoader::unload()

Unloads the plugin and returns true if the plugin could be unloaded; otherwise returns false.

This happens automatically on application termination, so you shouldn't normally need to call this function.

If other instances of QPluginLoader are using the same plugin, the call will fail, and unloading will only happen when every instance has called unload().

Don't try to delete the root component. Instead rely on that unload() will automatically delete it when needed.

参见 instance() and load().

相关非成员

void qRegisterStaticPluginFunction(QStaticPlugin plugin)

Registers the plugin specified with the plugin loader, and is used by Q_IMPORT_PLUGIN().

This function was introduced in Qt 5.0.