此话题阐述如何为 Qt 部署插件库或在运行时加载应用程序。若使用 静态插件 ,则插件代码已经是应用程序可执行文件的一部分,且不需要单独的部署步骤。
在 Qt 中,当应用程序启动时,应用程序的可执行文件目录是 Qt 搜索插件的基目录。
例如,在 Windows,若应用程序在
C:\Program Files\MyApp
且它拥有风格插件,Qt 查找
C:\Program Files\MyApp\styles
.
要找出应用程序可执行文件的位置,见 QCoreApplication::applicationDirPath ().
Qt also looks in the directory specified by
QLibraryInfo::path
(
QLibraryInfo::PluginsPath
),通常位于
QTDIR/plugins
;
QTDIR
是 Qt 的安装目录。若想要 Qt 查找其它地方,可以添加尽可能多的所需路径通过调用
QCoreApplication::addLibraryPath
()。若想要设置自己的路径,可以使用
QCoreApplication::setLibraryPaths
().
另外,可以使用
qt.conf
文件以覆盖编译进 Qt 库的硬编码路径。更多信息,见
使用 qt.conf
.
Another possibility is to set the
QT_PLUGIN_PATH
environment variable before you run the application; multiple paths can be separated with a system path separator. When set, Qt looks for plugins in the paths specified in this variable.
注意:
Do not export
QT_PLUGIN_PATH
as a system-wide environment variable because it can interfere with other Qt installations.
When loading plugins, the Qt library does some sanity checking to determine whether the plugin can be loaded and used. This sanity check enables you to have multiple Qt versions and configurations installed side by side.
The following rules apply:
Plugins linked with a Qt library that has a higher version number will not be loaded by a library with a lower version number.
范例: Qt 5.0.0 will not load a plugin built with Qt 5.0.1.
Plugins linked with a Qt library that has a lower major version number will not be loaded by a library with a higher major version number.
范例:
Qt 5.0.1 will
not
load a plugin built with Qt 4.8.2.
范例:
Qt 5.1.1 will load plugins built with Qt 5.1.0 and Qt 5.0.3.
When building plugins to extend an application, it's important to ensure that the plugin is configured in the same way as the application. This means that if the application was built in release mode, plugins should be built in release mode, too. Except for Unix operating systems, where the plugin system will not load plugins built in a different mode from the application.
If you configure Qt to be built in both debug and release modes, but only build your applications in release mode, you need to ensure that your plugins are also built in release mode. By default, if a debug build of Qt is available, plugins will
only
be built in debug mode. To force the plugins to be built in release mode, add the following line to the plugin's project (
.pro
) file:
CONFIG += release
This ensures that the plugin is compatible with the version of the library used in the application.
There are a number of issues that may prevent correctly-written plugins from working with the applications that are designed to use them. Many of these are related to differences in the way that plugins and applications have been built, often arising from separate build systems and processes.
To obtain diagnostic information from Qt, about each plugin it tries to load, use the
QT_DEBUG_PLUGINS
environment variable. Set this variable to a non-zero value in the environment where your application is launched.
The following table describes the common causes of problems developers experience when creating plugins and possible solutions.
Problem | Cause | Solution |
---|---|---|
Plugins sliently fail to load even when opened directly by the application. Qt Designer shows the plugin libraries in its Help|About Plugins dialog, but no plugins are listed under each of them. | The application and its plugins are built in different modes. |
Either share the same build information or build the plugins in both debug and release modes by appending the
debug_and_release
到
CONFIG
variable in each of their project files.
|