Qt5.3.2插件式开发入门例程--仅供参考
2015-07-28 11:05
513 查看
工程结构:
源代码:
-----------------PluginPerson.pro---------------
-----------------person.pro-------------------
---------------personinterface.h-----------------
------------main.cpp------------
-----------------xiaoming.pro-----------------
----------------xiaoming.h------------------
---------------xiaoming.cpp--------------
--------------Person.json是一个空文件--------------
运行效果:
附:
下面是Qt官方文档上的一个例子
注意:经过测试,在Qt5中Q_PLUGIN_METADATA这一行是必须有的,否则插件会加载不上。
(-----------完---------)
源代码:
-----------------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这一行是必须有的,否则插件会加载不上。
(-----------完---------)
相关文章推荐
- Qt 安装包制作
- Qt浅谈之三十一数据表格(QTableWidget)
- Deploying Qt or how to avoid “could not find or load the Qt platform plugin”
- Qt实战之开发软件数据获取助手
- 转:qt超强精美绘图控件 - QCustomPlot一览 及 安装使用教程
- Qt项目中遇到的一些小问题汇总
- gps定位Qt界面百度地图api的介绍
- Qt因为链接问题出错
- QT QSS 使用
- Qt鼠标双击事件mouseDoubleClickEvent(QMouseEvent* event)
- Qt creator 加入资源后无法使用
- Installing PyQt5 part1
- Qt_套接字
- QT之write and read
- QT把widget转换成图片后打印
- Qt鼠标事件
- QT学习二:一个简单的记事本
- Qt Creator 乱码问题
- Mqtt协议IOS移植完1
- 使用Qtip2来开发功能强大的删除和信息提示功能