您的位置:首页 > 编程语言 > Qt开发

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

2015-07-28 11:05 513 查看
工程结构:



源代码:

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

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


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: