QPluginLoader 类

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

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

注意: 此类的所有函数 可重入 .



QPluginLoader (const QString & fileName , QObject * parent = nullptr)
QPluginLoader (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 plugin 。Qt 插件存储在 DLL 共享库中并提供这些好处,相比访问共享库使用 QLibrary :

  • QPluginLoader 校验插件是否链接到如应用程序的相同 Qt 版本。
  • QPluginLoader 提供对根组件对象的直接访问 ( instance ()),而不是强制手动解析 C 函数。

QPluginLoader 对象实例操作称为插件的单个共享库文件。它以独立于平台的方式提供对插件功能的访问。要指定加载哪个插件,在构造函数中传递文件名或设置它采用 setFileName ().

最重要功能是 load () 用于动态加载插件文件, isLoaded () 用于校验加载是否成功,及 instance () 用于访问插件中的根组件。 instance () 函数隐式试着加载插件若插件尚未被加载。QPluginLoader 的多个实例可用于访问同一物理插件。

Once loaded, plugins remain in memory until all instances of QPluginLoader has been unloaded, or until the application terminates. You can attempt to unload a plugin using unload (), but if other instances of QPluginLoader are using the same library, the call will fail, and unloading will only happen when every instance has called unload (). Right before the unloading happens, the root component will also be deleted.

如何创建 Qt 插件 for more information about how to make your application extensible through plugins.

Note that the QPluginLoader cannot be used if your application is statically linked against Qt. In this case, you will also have to link to plugins statically. You can use QLibrary if you need to load dynamic libraries in a statically linked application.

另请参阅 QLibrary and 插件和描绘范例 .


fileName : QString


We recommend omitting the file's suffix in the file name, since QPluginLoader will automatically look for the file with the appropriate suffix (see QLibrary::isLibrary ()).

When loading the plugin, QPluginLoader searches in all plugin locations specified by QCoreApplication::libraryPaths (), unless the file name has an absolute path. After loading the plugin successfully, fileName() returns the fully-qualified file name of the plugin, including the full path to the plugin if one was given in the constructor or passed to setFileName().

If the file name does not exist, it will not be set. This property will then contain an empty string.



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

另请参阅 load ().

loadHints : QLibrary::LoadHints

Give the load () function some hints on how it should behave.

You can give hints on how the symbols in the plugin are resolved. By default since Qt 5.7, QLibrary::PreventUnloadHint 有设置。

See the documentation of QLibrary::loadHints for a complete description of how this property works.


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

另请参阅 QLibrary::loadHints .


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

构造插件加载器采用给定 parent 将加载指定插件通过 fileName .

要可加载,文件后缀必须是与平台一致的有效可加载库后缀,如 .so 在 Unix, .dylib 在 macOS 和 iOS,及 .dll 在 Windows。可以验证后缀采用 QLibrary::isLibrary ().

另请参阅 setFileName ().

QPluginLoader:: QPluginLoader ( QObject * parent = nullptr)

构造插件加载器采用给定 parent .

[virtual] QPluginLoader:: ~QPluginLoader ()

销毁 QPluginLoader 对象。

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

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

QString QPluginLoader:: errorString () const

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

QObject *QPluginLoader:: instance ()

返回插件的根组件对象。插件被加载若有必要。函数返回 nullptr 若插件无法被加载或根组件对象无法被实例化。


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

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

另请参阅 load ().

bool QPluginLoader:: isLoaded () const

返回 true 若插件被加载;否则返回 false .

另请参阅 load ().

bool QPluginLoader:: load ()

加载插件并返回 true 若插件加载成功;否则返回 false 。由于 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] QList < 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 ()

卸载插件并返回 true 若插件可以被卸载;否则返回 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 () 和 load ().


[since 5.0] void qRegisterStaticPluginFunction ( QStaticPlugin plugin )

注册 plugin 指定采用插件加载器,并用于 Q_IMPORT_PLUGIN ().

该函数在 Qt 5.0 引入。