Qt5 plug-in mechanism (7) -- plug-in development sample code (Lower-levelAPI)

Source: Internet
Author: User
Plug-in code interface class header file MyPluginInterface. h # ifndefINTERFACES_H # defineINTERFACES_H # include # defineQtPluginDemo_iidorg.qt-project.Qt.PluginDemoclassMyPluginInterface {publ plug-in code

Interface class header file MyPluginInterface. h

# Ifndef INTERFACES_H # define INTERFACES_H # include
 
  
# 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 functions that do not implement the // interface class are 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 
 
  #include 
  
   #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
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
        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
       
         (Object); if (plugin) qDebug ("% d + % d = % d", a, B, plugin-> add (a, B ));} else {qDebug ("failed to load plugin !! "); QString errorStr = l. errorString (); qDebug () <
        
         
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


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.