工程结构:
源代码:
-----------------PluginPerson.pro---------------
TEMPLATE = subdirs
SUBDIRS += xiaoming \
person
#-------------------------------------------------
#
# Project created by QtCreator 2015-07-27T11:41:44
#
#-------------------------------------------------
QT += core
QT -= gui
DESTDIR = ../../PluginPerson/exe
TARGET = person
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
HEADERS += \
personinterface.h
---------------personinterface.h-----------------
#ifndef PERSONINTERFACE_H
#define PERSONINTERFACE_H
#include <QString>
#include <QtPlugin>
#define PersonInterface_IID "org.qt-project.Qt.Person"
class PersonInterface
{
public:
virtual QString name()=0;
virtual int age()=0;
virtual void sing()=0;
};
Q_DECLARE_INTERFACE(PersonInterface, PersonInterface_IID)
#endif // PERSONINTERFACE_H
------------main.cpp------------
#include <QCoreApplication>
#include <QDebug>
#include <QPluginLoader>
#include <QFile>
#include "../person/personinterface.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qDebug()<<"person....";
QPluginLoader loader(QCoreApplication::applicationDirPath().append("/xiaoming.dll"));
QObject *obj=qobject_cast<QObject *>(loader.instance());
if(obj){
PersonInterface *person=qobject_cast<PersonInterface *>(obj);
if(person){
qDebug()<<"load success!";
qDebug()<<"name="<<person->name()<<", age="<<person->age();
person->sing();
}else{
qDebug()<<"person error!";
}
}else{
qDebug()<<"object error!";
}
return 0;
}
-----------------xiaoming.pro-----------------
#-------------------------------------------------
#
# Project created by QtCreator 2015-07-27T14:05:56
#
#-------------------------------------------------
QT -= gui
TARGET = xiaoming
TEMPLATE = lib
DESTDIR = ../../PluginPerson/exe
SOURCES += xiaoming.cpp
HEADERS += xiaoming.h
unix {
target.path = /usr/lib
INSTALLS += target
}
OTHER_FILES += \
Person.json
#ifndef XIAOMING_H
#define XIAOMING_H
#include "../person/personinterface.h"
class Xiaoming : public QObject, PersonInterface
{
Q_OBJECT
Q_PLUGIN_METADATA(IID PersonInterface_IID FILE "Person.json")
Q_INTERFACES(PersonInterface)
public:
QString name();
int age();
void sing();
};
#endif // XIAOMING_H
---------------xiaoming.cpp--------------
#include "xiaoming.h"
#include <QDebug>
QString Xiaoming::name()
{
return "xiaoming";
}
int Xiaoming::age()
{
return 10;
}
void Xiaoming::sing()
{
qDebug()<<"xiaoming sing ...";
}
运行效果:
附:
下面是Qt官方文档上的一个例子
class FilterInterface
{
public:
virtual ~FilterInterface() {}
virtual QStringList filters() const = 0;
virtual QImage filterImage(const QString &filter, const QImage &image,
QWidget *parent) = 0;
};
#include <QObject>
#include <QtPlugin>
#include <QStringList>
#include <QImage>
#include <plugandpaint/interfaces.h>
class ExtraFiltersPlugin : public QObject, public FilterInterface
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.Examples.PlugAndPaint.FilterInterface" FILE "extrafilters.json")
Q_INTERFACES(FilterInterface)
public:
QStringList filters() const;
QImage filterImage(const QString &filter, const QImage &image,
QWidget *parent);
};
注意:经过测试,在Qt5中Q_PLUGIN_METADATA这一行是必须有的,否则插件会加载不上。
(-----------完---------)