How to compile plug-ins in QT to load plug-ins and uninstall plug-ins

Source: Internet
Author: User

QtHow to compilePlug-insLoadPlug-insUninstallPlug-insIs the content to be introduced in this article.QtA class qpluginloader is provided to load static and dynamic libraries. In QT, QT regards both the dynamic library and static library as a plug-in and uses qpluginloader to load and uninstall these libraries. The QT class library is used to develop a plug-in system during the development of the project.

I. Compile the plug-in

To compile a QT plug-in, follow these steps:

1. Declare a plug-in class,

2. Define a class to implement the interface defined by this plug-in class. The defined class must be integrated from qobject.

3. Use q_interfacesq_interfaces () to notify QT of the existence of the metadatabase interface.

3. Use macro q_export_plugin2 () to export the plug-in Interface

4. Compile the plug-in class. Pro File

The following is an example of a plug-in.

1.class FilterInterface 
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.};

 

Note: The interface class must be a pure virtual function.

  1. Q_declare_interface (filterinterface, "com. trolltech. plugandpaint. brushinterface/1.0 ")

In order for the program to check whether a plug-in implements a given interface at runtime, the macro q_declare_interface () must be used (). The first parameter is the name of this interface, and the second parameter is a string that determines the interface. For convenience, the Java package naming method is used. If we change the interface later, we must use a new string to confirm the interface. Otherwise, the application may crash, so it is a good method to include a version number.

 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 };

 

This interface class is implemented. This class that implements the interface must be derived from qobject and implement the virtual function defined in the interface class.

Q_export_plugin2 (plugextrafilters, extrafiltersplugin) because the application function uses the main () function as the entry point, the plug-in must use the macro q_export_plugin2 () to specify the class to provide the plug-in.

This line of code can appear in any row of the. cpp file implementing the interface class. The first parameter is the plug-in name, and the second parameter is the plug-in class.

2. Plug-ins

1 QPluginLoader loader(pluginsDir.absoluteFilePath(fileName)); 
2 QObject *plugin = loader.instance();
3 if (plugin) {
4 populateMenus(plugin);
5 pluginFileNames += fileName;
6 }


The qpluginloader class provides a function loader to load plug-in instances. If the dynamic link library is not a plug-in or the compiled QT version library is incorrect, the pointer to the returned qobject object is null. Use the unload function to uninstall the plug-in.

3. Preparation of pro files

1 template = lib
2 config + = plugin
3 headers = extrafiltersplugin. h
4 sources = extrafiltersplugin. cpp
5 target = pnp_extrafilters
6 destdir = the type of the http://www.cnblogs.com/plugandpaint/plugins template is Lib. config type is plugin


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.