QT plug-in Introduction
What is a plug-in?
The plug-in mechanism is a mechanism to expand existing programs. The plug-in allows third-party developers to expand the program without having to access the source code of the main program. Various services that the plug-in can apply, including the provision of loading methods so that the plug-in can be loaded into the application and network transmission protocol for data exchange with the plug-in. In short, plug-ins are libraries that provide specific interfaces.
Two types of QT plug-ins:
Like other types of plug-ins, Qt Plugin is a computer application that interacts with the main application (host application) to provide specific functions. There are many reasons why applications support Plugin, including the ability of third-party developers to expand applications to provide unexpected features and reduce the size of applications; source code and applications are shared due to incompatibility between software copyrights. Qt Plugin is divided into two types: Dynamic plug-in and static plug-in.
1. The static plug-in can be statically linked to the application, making the deployment less error, but it is difficult to add new functions when the application is re-built and released;
2. Dynamic plug-ins are more common and flexible. They can be released separately and can be detected and loaded at runtime;
Steps for implementing the QT plug-in
How to implement a dynamic plug-in
1. Define the interface class:
1. define a common interface (pure virtual class ):
Programs must be aware of plug-ins and must comply with certain rules. Therefore, you need to define a common interface in the main program, which communicates directly with the plug-in class;
This example defines a QContactPlugin interface
// QContactPluginInterface. h
# Include
Class QContactPluginInterface
{
Public:
Virtual ~ QContactPluginInterface (){}
Virtual int getContact (int v) = 0;
};
1. 2. Use macro Q_DECLARE_INTERFACE ()
Add the following code to QContactPluginInterface. h:
Q_DECLARE_INTERFACE (QContactPluginInterface, "com. intel. Plugin. QContactPluginInterface ");
The Q_DECLARE_INTERFACE is defined in qobject. h to tell Qt meta-object system the interface name.
2. Main program section:
The main program dynamically loads the plug-in Code as follows:
QDir pluginsDir (qApp-> applicationDirPath ());
Foreach (QString fileName, pluginsDir. entryList (QDir: Files )){
QPluginLoader pluginLoader (pluginsDir. absoluteFilePath (fileName ));
QObject * plugin = pluginLoader. instance ();
If (plugin ){
QContactPluginInterface * interface = qobject_cast (plugin );
If (interface ){
QDebug () <getContact (10 );
}
}
}
The above code mainly includes the following steps:
2. 1. Search for plug-ins in the specified path
QDir pluginsDir (qApp-> applicationDirPath ());
Foreach (QString fileName, pluginsDir. entryList (QDir: Files )){
2. Detect and load the plug-in.
QPluginLoader pluginLoader (pluginsDir. absoluteFilePath (fileName ));
2. 3. test whether the plug-in is valid
Use qobject_cast () to test whether the plug-in provides the corresponding interface and converts the interface type to interface object pointer.
QContactPluginInterface * interface = qobject_cast (plugin );
If (interface ){
QDebug () <getContact (10 );
}