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 notation//virtual int Add (int, int);//improper notation (although this is also the correct virtual function declaration statement)//NOTE: The virtual function in the interface class should have an entity, or set to 0 (recommended) so that the/C compiler can generate a virtual function table for the interface class. Otherwise, if the virtual function is not implemented//or the interface class is not set to 0, the program may fail at link time, or the link succeeds but the last generated library fails to load (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
Plugin source Files MyPlugin.cpp
#include "MyPlugin.h" int myplugin::add (int a, int b) {return a+b;} #include "Moc_myplugin.cpp"
JSON file Myplugin.json, the file is empty in this example.
Project Document 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 library path//load plug-in, get instance qpluginloader L (QString (" Myplugin ")),//qpluginloader L (QString (" Libmyplugin.so ")), if ((Object=l.instance ()) = NULL) {qdebug (" plugin loaded. "); /use plug-in int a = +, 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.exec ();}
Myapp.pro
####################################################################### automatically generated by Qmake (3.0)?? 11? 02:26:33 2014##################################################################### #TEMPLATE = AppQT + = GUI Core Widgetsconfig + = Consoletarget = Myappincludepath + =. # Inputheaders + = myplugininterface.hsources + main.cpp
QT5 plug-in mechanism (7)-Plug-in Development sample code (Lower-level API)