QPluginLoader 類

QPluginLoader 類在運行時加載插件。 更多...

頭: #include <QPluginLoader>
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 ()
QVector<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.

該特性在 Qt 4.4 引入。

訪問函數:

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 .

[虛擬] 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.

該函數在 Qt 4.2 引入。

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] 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 ()

卸載插件並返迴 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 ().

相關非成員

void qRegisterStaticPluginFunction ( QStaticPlugin plugin )

注冊 plugin 指定采用插件加載器,並用於 Q_IMPORT_PLUGIN ().

該函數在 Qt 5.0 引入。