Qt5 plug-in mechanism (7) -- plug-in development example code (Lower-level API), qt5lower-level
Plug-in code
Interface Class header file MyPluginInterface. h
# Ifndef INTERFACES_H # define INTERFACES_H # include <QtPlugin> # define QtPluginDemo_iid "org. qt-project.Qt.PluginDemo "class MyPluginInterface {public: virtual int add (int, int) = 0; // correct syntax // virtual int add (int, int ); // incorrect syntax (although this is also the correct virtual function declaration statement) // Note: all virtual functions in the interface class should have an entity or be set to 0 (recommended ), in this way, the/C ++ compiler can generate a virtual function table for the interface class. Otherwise, if the virtual function that does not implement the // interface class is not set to 0, the program may fail to be linked, // or the link is successful, but the last generated library cannot be loaded (there are undefined symbols in the library)}; Q_DECLARE_INTERFACE (MyPluginInterface, QtPluginDemo_iid); # endif
Plug-In header file MyPlugin. h
#ifndef MYPLUGIN__H#define MYPLUGIN__H#include <QObject>#include <QDebug>#include "MyPluginInterface.h"class MyPlugin : public QObject, public MyPluginInterface{ Q_OBJECT Q_PLUGIN_METADATA ( IID QtPluginDemo_iid FILE "MyPlugin.json") Q_INTERFACES(MyPluginInterface)public:int add(int,int);};#endif
Plug-in source file MyPlugin. cpp
#include "MyPlugin.h"int MyPlugin::add(int a , int b){return a+b ;}#include "moc_MyPlugin.cpp"
JSON file MyPlugin. json, Which is empty in this example.
Project File MyPlugin. pro
TEMPLATE = libCONFIG += plugin consoleQT += coreHEADERS = MyPlugin.h MyPluginInterface.hSOURCES = MyPlugin.cppOTHER_FILES = MyPlugin.jsonTARGET = MyPluginDESTDIR = ./INCLUDEPATH += ./# installtarget.path = ./installINSTALLS += target
Application code
Interface Class header file MyPluginInterface. h, which is consistent with the plug-in code.
Main program file main. cpp
# Include "MyPluginInterface. h "# include <QtPlugin> # include <QApplication> # include <QWidget> # include <QPluginLoader> # include <QString> # include <QtDebug> int main (int argc, char * argv []) {QApplication app (argc, argv); QWidget w; QObject * object; w. show (); app. addLibraryPath (QString (".. /MyPlugin/install "); // Add the library path // load the plug-in to obtain the instance QPluginLoader l (QString (" MyPlugin ")); // QPluginLoader l (QString ("libMyPlugin. so ") ); If (object = l. instance ())! = NULL) {qDebug ("plugin loaded. "); // use the plug-in int a = 14, B = 23; MyPluginInterface * plugin = qobject_cast <MyPluginInterface *> (object); if (plugin) qDebug ("% d + % d = % d", a, B, plugin-> add (a, B);} else {qDebug ("failed to load plugin !! "); QString errorStr = l. errorString (); qDebug () <errorStr;} return app.exe c ();}
MyApp. pro
####################################################################### Automatically generated by qmake (3.0) ?? 11? 19 02:26:33 2014######################################################################TEMPLATE = appQT += gui core widgetsCONFIG += consoleTARGET = MyAppINCLUDEPATH += .# InputHEADERS += MyPluginInterface.hSOURCES += main.cpp