#ifndef MAINWINDOW_H#define MAINWINDOW_H#include <QMainWindow>#include "interface.h" //без этого не работает, а надо без негоQT_BEGIN_NAMESPACEQT_END_NAMESPACEclass MainWindow : public QMainWindow{ Q_OBJECTpublic: MainWindow();#endif // MAINWINDOW_H
#ifndef INTERFACE_H#define INTERFACE_Hclass MyModuleInterface{public: virtual ~MyModuleInterface() {} virtual QString run() = 0;};Q_DECLARE_INTERFACE(MyModuleInterface, "mywww.My.MyModuleInterface/1.0")#endif // INTERFACE_H
#include "mainwindow.h"#include <QtGui>MainWindow::MainWindow(){QPluginLoader loader( ".../libplugin.so" ); if ( !loader.load() ) { MainWindow::setWindowTitle("This is not Qt plugin or plugin not match out Qt Libraries" ); return; } MyModuleInterface * i = qobject_cast<MyModuleInterface*>( loader.instance() ); if ( !i ) { MainWindow::setWindowTitle( "This is not plugin for interface MyInterface" ); return; } MainWindow::setWindowTitle( i->run());
#include <QApplication>#include "mainwindow.h"int main(int argc, char *argv[]){ QApplication a(argc, argv); MainWindow w; w.show(); return a.exec();}
QT += core guiTARGET = projectTEMPLATE = appSOURCES += main.cpp mainwindow.cppHEADERS += mainwindow.h interface.h
#ifndef PLUGIN_H#define PLUGIN_H#include <QObject>#include "interface.h"class MyModulePlugin : public QObject, public MyModuleInterface{ Q_OBJECT Q_INTERFACES(MyModuleInterface)public: QString run() { return QString("Ready!"); };};#endif // PLUGIN_H
#include <QtGui>#include "plugin.h"Q_EXPORT_PLUGIN2(mymoduleplugin, MyModulePlugin)
TEMPLATE = libCONFIG += pluginVERSION = 1.0.0DEFINES += D_SHARED_LIBTARGET = pluginHEADERS += interface.h plugin.hSOURCES += plugin.cpp
typedef Qstring (*MyPrototype)(); MyPrototype myFunction = (MyPrototype) QLibrary::resolve(".../libplugin.so", "run"); if (myFunction) MainWindow::setWindowTitle(myFunction());
extern "C" MY_EXPORT int avg(int a, int b) { return (a + b) / 2; }
#ifdef Q_WS_WIN #define MY_EXPORT __declspec(dllexport) #else #define MY_EXPORT #endif
#ifndef PLUGIN_H#define PLUGIN_H#ifdef Q_WS_WIN #define MY_EXPORT __declspec(dllexport) #else #define MY_EXPORT #endif#include <QObject>#include <QString>class MyModulePlugin : public QObject{ Q_OBJECTpublic: extern "C" MY_EXPORT QString run() { return QString("Ready!"); };};#endif // PLUGIN_H
#include <QtGui>Q_EXPORT_PLUGIN2(mymoduleplugin, MyModulePlugin)
extern "C" MY_EXPORT int run2(){ MyModulePlugin*myPlugin = new MyModulePlugin; int result = myPlugin->run(); qDebug()<<result;//12 return 13; //13}