日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Qt文档阅读笔记-Qt4 Lower-Level API扩展Qt Applications(Qt4中Plugin的使用)解析与实例

發布時間:2025/3/15 编程问答 13 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Qt文档阅读笔记-Qt4 Lower-Level API扩展Qt Applications(Qt4中Plugin的使用)解析与实例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目錄

?

?

官方解析

博主栗子


?

官方解析

Qt應用程序可以對插件進行擴展,要使用QPluginLoader這個類進行加載。插件可以提供任意的功能,而且不限制數據庫驅動,圖像格式,以及其他的Qt功能插件。

當調用插件時要有如下的過程:

? ? ? ? ? ? 1. 定義一個抽象類作為接口,用于調用插件;

? ? ? ? ? ? 2. 使用Q_DECLARE_INTERFACE()宏告訴Qt元對象系統,這是一個接口類;

? ? ? ? ? ? 3. 使用QPluginLoader加載插件;

? ? ? ? ? ? 4. 使用qobject_cast去嘗試把插件實例轉換為 接口。

?

寫插件包含的過程:

? ? ? ? ? ? 1. 寫一個插件類,這個類繼承了QObject和調用插件時寫的那個接口類;

? ? ? ? ? ? 2. 使用Q_INTERFACES()宏告訴元對象,這個哪個類是接口;

? ? ? ? ? ? 3. 使用Q_EXPORT_PLUGIN2()宏用于導出插件;

? ? ? ? ? ? 4. 配置好.pro文件。

?

?

下面是一個接口類的代碼:

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 <QStringList>#include <QImage>#include <plugandpaint/interfaces.h>class ExtraFiltersPlugin : public QObject, public FilterInterface{Q_OBJECTQ_INTERFACES(FilterInterface)public:QStringList filters() const;QImage filterImage(const QString &filter, const QImage &image,QWidget *parent);};

?

博主栗子

下面舉一個插件生成和調用的例子,程序運行截圖如下:

調用插件端:

當點擊Test后,調用插件界面:

插件端源碼如下:

AppInterface.h

#ifndef APPINTERFACE_H #define APPINTERFACE_H#include <QtPlugin>class AppInterface{public:virtual ~AppInterface(){}virtual QString pluginName() const = 0;virtual QWidget *createWidget() = 0; };Q_DECLARE_INTERFACE(AppInterface, "com.AppInterface");#endif //APPINTERFACE_H

HelloWidget.h

#ifndef HELLOWIDGET_H #define HELLOWIDGET_H#include <QObject> #include "AppInterface.h"class HelloWidget: public QObject, public AppInterface{Q_OBJECTQ_INTERFACES(AppInterface)public:HelloWidget();~HelloWidget();QString pluginName() const;QWidget *createWidget(); };#endif

HelloWidget.cpp

#include "HelloWidget.h" #include "MyWidget.h"HelloWidget::HelloWidget(){}HelloWidget::~HelloWidget(){}QString HelloWidget::pluginName() const{return "Plugin"; }QWidget *HelloWidget::createWidget(){return new MyWidget; } Q_EXPORT_PLUGIN2("helloWidget", HelloWidget);

?

MyWidget.h

#ifndef MYWIDGET_H #define MYWIDGET_H#include <QWidget>class MyWidget : public QWidget{Q_OBJECTpublic:MyWidget(QWidget *parent = 0);~MyWidget(); };#endif

MyWidget.cpp

#include "MyWidget.h" #include <QPushButton> #include <QVBoxLayout>MyWidget::MyWidget(QWidget *parent): QWidget(parent){QPushButton *button = new QPushButton("Test Button");QVBoxLayout *layout = new QVBoxLayout;layout->addWidget(button);setLayout(layout);}MyWidget::~MyWidget(){}

MyWidget.pro

TARGET = HelloWidget TEMPLATE = lib CONFIG += pluginSOURCES += \MyWidget.cpp\HelloWidget.cppHEADERS += \AppInterface.h\HelloWidget.h\MyWidget.h

?

插件調用端:

AppInterface.h

#ifndef APPINTERFACE_H #define APPINTERFACE_H#include <QtPlugin>class AppInterface{public:virtual ~AppInterface(){}virtual QString pluginName() const = 0;virtual QWidget *createWidget() = 0; };Q_DECLARE_INTERFACE(AppInterface, "com.AppInterface");#endif //APPINTERFACE_H

Widget.h

#ifndef WIDGET_H #define WIDGET_H#include <QWidget>class Widget : public QWidget{Q_OBJECTpublic:Widget(QWidget *parent = 0);~Widget();protected slots:void btnClicked();};#endif //WIDGET_H

Widget.cpp

#include <QDir> #include <QApplication> #include <QPluginLoader> #include <QDebug>Widget::Widget(QWidget *parent) : QWidget(parent){QPushButton *button = new QPushButton("Test");QVBoxLayout *layout = new QVBoxLayout;layout->addWidget(button);setLayout(layout);connect(button, SIGNAL(clicked()), this, SLOT(btnClicked())); }Widget::~Widget(){}void Widget::btnClicked(){//QMessageBox::information(this, "tip", "HelloWidget");QDir pluginsDir(qApp->applicationDirPath() + "/Plugin");foreach(QString fileName, pluginsDir.entryList(QDir::Files)){QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(fileName));QObject *plugin = pluginLoader.instance();if(plugin){qDebug() << plugin;AppInterface *app = qobject_cast<AppInterface*>(plugin);if(app){QWidget *widget = qobject_cast<QWidget*>(app->createWidget());widget->show();}}else{QMessageBox::information(this, "tip", "load plugin error");}}}

?

main.cpp

#include "Widget.h" #include <QApplication>int main(int argc, char *argv[]){QApplication a(argc, argv);Widget w;w.show();return a.exec(); }

CallPlugin.pro

QT += core guiTARGET = CallPlugin TEMPLATE = appSOURCES += Widget.cpp\main.cppHEADERS += Widget.h\AppInterface.h

?

?

?

?

?

?

總結

以上是生活随笔為你收集整理的Qt文档阅读笔记-Qt4 Lower-Level API扩展Qt Applications(Qt4中Plugin的使用)解析与实例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。