The QPluginLoader class loads a plugin at run-time. 更多...
| 头: | #include <QPluginLoader> |
| 继承: | QObject |
注意: 此类的所有函数 可重入 .
| QPluginLoader (QObject * parent = 0) | |
| QPluginLoader (const QString & fileName , QObject * parent = 0) | |
| ~QPluginLoader () | |
| QString | errorString () const |
| QString | fileName () const |
| QObject * | instance () |
| bool | isLoaded () const |
| bool | load () |
| QLibrary::LoadHints | loadHints () const |
| void | setFileName (const QString & fileName ) |
| void | setLoadHints (QLibrary::LoadHints loadHints ) |
| bool | unload () |
| QObjectList | staticInstances () |
| void | qRegisterStaticPluginInstanceFunction (QtPluginInstanceFunction function ) |
The QPluginLoader class loads a plugin at run-time.
QPluginLoader provides access to a Qt plugin 。Qt 插件存储在 DLL 共享库中并提供这些好处,相比访问共享库使用 QLibrary :
An instance of a QPluginLoader object operates on a single shared library file, which we call a plugin. It provides access to the functionality in the plugin in a platform-independent way. To specify which plugin to load, either pass a file name in the constructor or set it with setFileName ().
最重要功能是 load () 用于动态加载插件文件, isLoaded () 用于校验加载是否成功,及 instance () 用于访问插件中的根组件。 instance () function implicitly tries to load the plugin if it has not been loaded yet. Multiple instances of QPluginLoader can be used to access the same physical plugin.
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 happen, the root component will also be deleted.
In order to speed up loading and validation of plugins, some of the information that is collected during loading is cached in persistent memory (through QSettings ). For instance, the result of a load operation (e.g. succeeded or failed) is stored in the cache, so that subsequent load operations don't try to load an invalid plugin. However, if the "last modified" timestamp of a plugin has changed, the plugin's cache entry is invalidated and the plugin is reloaded regardless of the values in the cache entry. The cache entry is then updated with the new result of the load operation.
This also means that the timestamp must be updated each time the plugin or any dependent resources (such as a shared library) is updated, since the dependent resources might influence the result of loading a plugin.
见 如何创建 Qt 插件 for more information about how to make your application extensible through plugins.
注意, 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.
注意: In Symbian the plugin stub files must be used whenever a path to plugin is needed. For the purposes of loading plugins, the stubs can be considered to have the same name as the actual plugin binary. In practice they have ".qtplugin" extension instead of ".dll", but this difference is handled transparently by QPluginLoader and QLibrary to avoid need for Symbian specific plugin handling in most Qt applications. Plugin stubs are needed because Symbian Platform Security denies all access to the directory where the actual plugin binaries are located.
This property holds the file name of the plugin.
要可加载,文件后缀必须是与平台一致的有效可加载库后缀,如
.so
on Unix,
.dylib
on Mac OS X, and
.dll
在 Windows。可以验证后缀采用
QLibrary::isLibrary
().
If the file name does not exist, it will not be set. This property will then contain an empty string.
默认情况下,此特性包含空字符串。
Note: In Symbian the fileName must point to plugin stub file.
访问函数:
| QString | fileName () const |
| void | setFileName (const QString & fileName ) |
另请参阅 load ().
This property holds 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, none of the hints are set.
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 .
构造插件加载器采用给定 parent .
构造插件加载器采用给定 parent 将加载指定插件通过 fileName .
要可加载,文件后缀必须是与平台一致的有效可加载库后缀,如
.so
在 Unix,
.dylib
on Mac OS X, and
.dll
在 Windows。可以验证后缀采用
QLibrary::isLibrary
().
Note: In Symbian the fileName must point to plugin stub file.
另请参阅 setFileName ().
销毁 QPluginLoader 对象。
除非 unload () was called explicitly, the plugin stays in memory until the application terminates.
Returns a text string with the description of the last error that occurred.
该函数在 Qt 4.2 引入。
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.
若根组件对象被销毁,调用此函创建新实例。
不会删除由此函数返回的根组件当 QPluginLoader 被销毁时。若想要确保删除根组件,应该调用 unload () 只要不再需要访问核心组件。当库被最终卸载时,将自动删除根组件。
组件对象是 QObject 。使用 qobject_cast () 访问感兴趣接口。
另请参阅 load ().
Returns true if the plugin is loaded; otherwise returns false.
另请参阅 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 ().
[static]
QObjectList
QPluginLoader::
staticInstances
()
Returns a list of static plugin instances (root components) held by the plugin loader.
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.
注册给定 function with the plugin loader.
该函数在 Qt 4.4 引入。