class MyInterface{public: virtual ~MyInterface(); virtual QString doSomething( int value ) = 0;};Q_DECLARE_INTERFACE( MyInterface, "unique_keyword.MyInterface.version-1.0")
// myplugin.hclass MyPlugin : public QObject, MyInterface{ Q_OBJECT Q_INTERFACES( MyInterface )public: QString doSomething( int value ) { return QString::number( value ); }};
//myplugin.cppQ_EXPORT_PLUGIN2( my_plugin, MyPlugin )
QPluginLoader loader( "myplugin.dll" );if ( !loader.load() ){ qWarning( "This is not Qt plugin or plugin not match out Qt Libraries" ); return;}MyInterface * i = qobject_cast<MyInterface*>( loader.instance() );if ( !i ){ qWarning( "This is not plugin for interface MyInterface" ); return;}QString text = i->doSomething( 123 ); // text = "123"
TEMPLATE = libCONFIG += dll releaseHEADERS += deconvolution.hSOURCES += deconvolution.cpp
#define DECONV_DLL __declspec(dllexport)#endif#include "deconvolution.h"extern "C" DECONV_DLL void funcDeconv(QVector<CurveData> &curveMatrix){ int size = curveMatrix.size();}
#ifndef __DECONVOLUTION1_H#define __DECONVOLUTION1_H#include <QVector>typedef QVector<double> CurveData;#endif
QLibrary deconvLib("deconvolution"); typedef double (*deconvFunction)(QVector<CurveData> &curveMatrix); deconvFunction funcDeconv = (deconvFunction) deconvLib.resolve("funcDeconv"); if (!funcDeconv) statusBar()->showMessage(tr("Can't load deconvolution library."), 15000); else funcDeconv(curveMatrixForDeconv);
C++ (Qt)class IModule{public: virtual ~IModule(){} virtual QString moduleName()=0;};Q_DECLARE_INTERFACE(IModule,"1.0")
C++ (Qt)class DBEngine: public QObject, public IModule { Q_OBJECT Q_INTERFACES(IModule)public: DBEngine(); QString moduleName();};
C++ (Qt)int Modules::loadModule(QString fileName){ QPluginLoader loader(fileName); QObject *obj = loader.instance(); if (IModule *interface = qobject_cast<IModule *>(obj) ) {#ifdef QT_DEBUG qDebug()<<"Module loaded";#endif return 0; } else {#ifdef QT_DEBUG qDebug()<<"Module not loaded";#endif return 1; }
C++ (Qt)class DBEngine: public QObject, public IModule { Q_OBJECT Q_INTERFACES(IModule)public: DBEngine(); QString moduleName();};Q_EXPORT_PLUGIN2(dbengine, DBEngine)
debug/moc_dbengine.o: In function `qt_plugin_query_verification_data':H:\QProject\src\DBEngine/../Core/interfaces.h:6: multiple definition of `qt_plugin_query_verification_data'debug/dbengine.o:H:\QProject\src\DBEngine/dbengine.h:14: first defined heredebug/moc_dbengine.o: In function `qt_plugin_instance':H:\QProject\src\DBEngine/debug//../dbengine.h:14: multiple definition of `qt_plugin_instance'debug/dbengine.o:H:\QProject\src\DBEngine/dbengine.h:14: first defined herecollect2: ld returned 1 exit statusmingw32-make[2]: *** [..\..\bin\plugins\DBEngine.dll] Error 1mingw32-make[1]: *** [debug] Error 2mingw32-make: *** [sub-DBEngine-make_default] Error 2Завершено с кодом 2.