How to establishQt pluginThe course is the content to be introduced in this article.QTMediumPlug-insAnd how to establish the application. This article provides a detailed description of the implementation of the specific content.
QT provides two APIs to build plug-ins
1. Extended QT library through high-level APIs. For example, custom database driver, image format, character encoding, and custom styles
2. Extended QT applications using underlying APIs
For example, if you want to write a subclass of a custom QStyle and load the application dynamically, you need to use high-level API functions.
Because the high-level APIs are constructed on the basis of the underlying APIs, you need to pay attention to some issues.
To provide the QT Designeder plug-in, see the QtDesigner model document.
High-level API
To implement plug-ins by inheriting specific base classes, you need to implement some of the functions and add a macro
QT has many plug-in base classes for use. The inherited plug-ins are stored in the subdirectory of the standard plug-in directory by default. Otherwise, Qt cannot be found.
Design a plug-in named MyStyle for the style class
File mystyleplugin. h:
- Class MyStylePlugin: public QStylePlugin
- {
- Public:
- QStringList keys () const; // return the list of style names that this plug-in can use.
- QStyle * create (const QString & key); // return the style based on the input style name.
- };
-
- Mystyleplugin. cpp
- # Include "mystyleplugin. h"
-
- QStringList MyStylePlugin: keys () const
- {
- Return QStringList () <"MyStyle ";
- }
-
- QStyle * MyStylePlugin: create (const QString & key)
- {
- If (key. toLower () = "mystyle ")
- Return new MyStyle;
- Return 0;
- }
-
- Q_EXPORT_PLUGIN2 (pnp_mystyleplugin, MyStylePlugin)
Style implementation
File mystyle. h:
- class MyStyle : public QWindowsStyle
- {
- Q_OBJECT
-
- public:
- MyStyle() {};
-
- void polish(QPalette &palette);
- };
Inherited from QWindowsStyle
Style implementation
- void MyStyle::polish(QPalette &palette)
- {
- palette.setBrush(QPalette::Button, Qt::red);
- }
Note the case sensitivity mode ).
When implementing database-driven, image format, text encoding, and most other plug-in types, you generally do not need to create objects. Qt will find them and create their objects. Style is a special case, because it can be called in the program as follows:
- QApplication::setStyle(QStyleFactory::create("MyStyle"));
Main. c file:
- Int main (int argv, char * args [])
- {
- QApplication app (argv, args); // QT loads the plug-in
- QApplication: setStyle (QStyleFactory: create ("simplestyle "));
-
- StyleWindow window;
- Window. resize (200, 50 );
- Window. show ();
-
- Return app.exe c ();
- }
Project files:
- TEMPLATE = lib
- CONFIG += plugin
- HEADERS = simplestyle.h \
- simplestyleplugin.h
- SOURCES = simplestyle.cpp \
- simplestyleplugin.cpp
- TARGET = simplestyleplugin
Note: The plug-in needs to set a TEMPLATE, because we need to share the library rather than execute the program. You must also set CONFIG. You need to save this plug-in to the folder where the style folder application is located ). In this way, the application can be detected.
Underlying API: no detailed research
Both QT and QT applications can be extended through plug-ins. This requires the application to detect and load through QPluginLoader. Therefore, the plug-in can provide any function, not just the plug-in mentioned above.
There are four steps to extend the plug-in application.
1. Define the interface set of the plug-in (in fact, there is only one class of the virtual function)
2. The Q_DECLARE_INTERFACE macro tells the meta-object system that this interface exists.
3. Use QPluginLoader to load the plugin
4. Use the qobject_cast () function to test the plug-in.
Four steps of plug-in Encoding
1. inherit from QObject to define a plug-in class and define the interfaces required by the plug-in
2. Use the Q_InterFaces () macro to tell the meta-object System Interface
3. Use the Q_EXPORT_PLUGIN2 () macro output plug-in
4. compile the project
For example:
Interface Class:
- Class FilterInterface // class with only virtual functions
- {
- Public:
- Virtual ~ FilterInterface (){}
-
- Virtual QStringList filters () const = 0;
- Virtual QImage filterImage (const QString & filter, const QImage & image,
- QWidget * parent) = 0;
- };
Plug-in class to implement interfaces
- #include <QObject>
- #include <QStringList>
- #include <QImage>
-
- #include <plugandpaint/interfaces.h>
-
- class ExtraFiltersPlugin : public QObject, public FilterInterface
- {
- Q_OBJECT
- Q_INTERFACES(FilterInterface)
-
- public:
- QStringList filters() const;
- QImage filterImage(const QString &filter, const QImage &image,
- QWidget *parent);
- };
Locating plug-ins
The QT application automatically loads the plug-in because the plug-in exists in the subdirectory of the standard plug-in.
During development, QDIR is the directory installed by QT in QTDIR/plugins ). If you want the application to use or not use the standard plug-in, the installation will get the path to install the plug-in, and save the path. For example, the application uses QSettings to read it at startup)
The application can use QCoreApplication: addLibraryPath to make the plug-in visible to the application. Note that the final path cannot be changed.
If you want the plug-in to be loaded, you can save the plug-in a subdirectory in the directory where the application is located. To release any plug-ins that come with QT, copy the sub-directories of plugins to the root directory of the application. Instead of the directory containing the plug-in)
Static plug-in
The general method is to make the plug-in a dynamic library and publish it together with the application. Dynamically detects and loads plug-ins.
Applications can be statically linked. If the static QT library is compiled, the static plug-in is the only choice. The use of static plug-ins can reduce the error probability. However, the disadvantage is that you need to recompile the entire application to modify the plug-in.
QT provides some static plug-ins:
To statically link the plug-in, you must use the Q_IMPORT_PLUGIN macro in the program and use the QTPLUGIN parameter during compilation. For example, in main. cpp
- #include <QApplication>
- #include <QtPlugin>
-
- Q_IMPORT_PLUGIN(qjpeg)
- Q_IMPORT_PLUGIN(qgif)
- Q_IMPORT_PLUGIN(qkrcodecs)
-
- int main(int argc, char *argv[])
- {
- QApplication app(argc, argv);
- ...
- return app.exec();
- }
Engineering File
- QTPLUGIN += qjpeg \
- qgif \
- qkrcodecs
Summary: explains how to establishQt pluginThe course content has been introduced. I hope this article will help you!