How to Build Qt plug-in

Source: Internet
Author: User

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:

 
 
  1. Class MyStylePlugin: public QStylePlugin
  2. {
  3. Public:
  4. QStringList keys () const; // return the list of style names that this plug-in can use.
  5. QStyle * create (const QString & key); // return the style based on the input style name.
  6. };
  7.  
  8. Mystyleplugin. cpp
  9. # Include "mystyleplugin. h"
  10.  
  11. QStringList MyStylePlugin: keys () const
  12. {
  13. Return QStringList () <"MyStyle ";
  14. }
  15.  
  16. QStyle * MyStylePlugin: create (const QString & key)
  17. {
  18. If (key. toLower () = "mystyle ")
  19. Return new MyStyle;
  20. Return 0;
  21. }
  22.  
  23. Q_EXPORT_PLUGIN2 (pnp_mystyleplugin, MyStylePlugin)

Style implementation

File mystyle. h:

 
 
  1. class MyStyle : public QWindowsStyle  
  2. {  
  3. Q_OBJECT  
  4.  
  5. public:  
  6. MyStyle() {};  
  7.  
  8. void polish(QPalette &palette);  
  9. }; 

Inherited from QWindowsStyle

Style implementation

 
 
  1. void MyStyle::polish(QPalette &palette)  
  2. {  
  3. 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:

 
 
  1. QApplication::setStyle(QStyleFactory::create("MyStyle")); 

Main. c file:

 
 
  1. Int main (int argv, char * args [])
  2. {
  3. QApplication app (argv, args); // QT loads the plug-in
  4. QApplication: setStyle (QStyleFactory: create ("simplestyle "));
  5.  
  6. StyleWindow window;
  7. Window. resize (200, 50 );
  8. Window. show ();
  9.  
  10. Return app.exe c ();
  11. }

Project files:

 
 
  1. TEMPLATE    = lib 
  2. CONFIG     += plugin  
  3. HEADERS     = simplestyle.h \  
  4. simplestyleplugin.h  
  5. SOURCES     = simplestyle.cpp \  
  6. simplestyleplugin.cpp  
  7. 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:

 
 
  1. Class FilterInterface // class with only virtual functions
  2. {
  3. Public:
  4. Virtual ~ FilterInterface (){}
  5.  
  6. Virtual QStringList filters () const = 0;
  7. Virtual QImage filterImage (const QString & filter, const QImage & image,
  8. QWidget * parent) = 0;
  9. };

Plug-in class to implement interfaces

 
 
  1. #include <QObject> 
  2. #include <QStringList> 
  3. #include <QImage> 
  4.  
  5. #include <plugandpaint/interfaces.h> 
  6.  
  7. class ExtraFiltersPlugin : public QObject, public FilterInterface  
  8. {  
  9. Q_OBJECT  
  10. Q_INTERFACES(FilterInterface)  
  11.  
  12. public:  
  13. QStringList filters() const;  
  14. QImage filterImage(const QString &filter, const QImage &image,  
  15. QWidget *parent);  
  16. }; 

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

 
 
  1. #include <QApplication> 
  2. #include <QtPlugin> 
  3.  
  4. Q_IMPORT_PLUGIN(qjpeg)  
  5. Q_IMPORT_PLUGIN(qgif)  
  6. Q_IMPORT_PLUGIN(qkrcodecs)  
  7.  
  8. int main(int argc, char *argv[])  
  9. {  
  10. QApplication app(argc, argv);  
  11. ...  
  12. return app.exec();  

Engineering File

 
 
  1. QTPLUGIN     += qjpeg \  
  2. qgif \  
  3. qkrcodecs 

Summary: explains how to establishQt pluginThe course content has been introduced. I hope this article will help you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.