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

工程结构:


源代码:

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


----------------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 <QDebug>

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

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

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


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

运行效果:



 附:

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

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值