Qt5.3.2插件式开发入门例程--仅供参考

工程结构:

Qt5.3.2插件式开发入门例程--仅供参考_第1张图片

源代码:

-----------------PluginPerson.pro---------------

TEMPLATE = subdirs

SUBDIRS += xiaoming \
    person


-----------------person.pro-------------------

#-------------------------------------------------
#
# 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 
#include 

#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 
#include 

#include 
#include 

#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(loader.instance());
    if(obj){
        PersonInterface *person=qobject_cast(obj);
        if(person){
            qDebug()<<"load success!";
            qDebug()<<"name="<name()<<", age="<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


----------------xiaoming.h------------------

#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 

QString Xiaoming::name()
{
    return "xiaoming";
}

int Xiaoming::age()
{
    return 10;
}

void Xiaoming::sing()
{
    qDebug()<<"xiaoming sing ...";
}


--------------Person.json是一个空文件--------------

运行效果:

Qt5.3.2插件式开发入门例程--仅供参考_第2张图片



 附:

下面是Qt官方文档上的一个例子

class FilterInterface
{
public:
    virtual ~FilterInterface() {}

    virtual QStringList filters() const = 0;
    virtual QImage filterImage(const QString &filter, const QImage &image,
                               QWidget *parent) = 0;
};
#include 
#include 
#include 
#include 

#include 

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这一行是必须有的,否则插件会加载不上。

(-----------完---------)



你可能感兴趣的:(Qt)