Qt靜態外掛開發與使用
Static Plugins
The normal and most flexible way to include a plugin with an application is to compile it into a dynamic library that is shipped separately, and detected and loaded at runtime.
Plugins can be linked statically into your application. If you build the static version of Qt, this is the only option for including Qt's predefined plugins. Using static plugins makes the deployment less error-prone, but has the disadvantage that no functionality from plugins can be added without a complete rebuild and redistribution of the application.
To link plugins statically, you need to add the required plugins to your build using QTPLUGIN
.
In the .pro
file for your application, you need the following entry:
QTPLUGIN += qjpeg \ qgif \ qkrcodecs
qmake automatically adds the plugins to QTPLUGIN that are typically needed by the Qt modules used (see
QTPLUGIN.platforms = qminimal
If you want neither the default, nor the minimal QPA plugin to be linked automatically, use:
QTPLUGIN.platforms = -
The defaults are tuned towards an optimal out-of-the-box experience, but may unnecessarily bloat the application. It is recommended to inspect the linker command line built by qmake and eliminate unnecessary plugins.
Details of Linking Static Plugins
To cause static plugins actually being linked and instantiated, Q_IMPORT_PLUGIN() macros are also needed in application code, but those are automatically generated by qmake and added to your application project.
If you do not want all plugins added to QTPLUGIN to be automatically linked, remove import_plugins
from the CONFIG
variable:
CONFIG -= import_plugins
Creating Static Plugins
It is also possible to create your own static plugins, by following these steps:
- Add
CONFIG += static
to your plugin's.pro
file. - Use the Q_IMPORT_PLUGIN() macro in your application.
- Use the Q_INIT_RESOURCE() macro in your application if the plugin ships qrc files.
- Link your application with your plugin library using
LIBS
in the.pro
file.
See the Plug & Paint example and the associated Basic Tools plugin for details on how to do this.
Note: If you are not using qmake to build your plugin you need to make sure that the QT_STATICPLUGIN
preprocessor macro is defined.
Deploying and Debugging Plugins
The Deploying Plugins document covers the process of deploying plugins with applications and debugging them when problems arise.