qt 定义插件
2023-09-14 09:12:37 时间
定义的接口
----------------------------------------------
#ifndef REGEXPINTERFACE_H
#define REGEXPINTERFACE_H
#include <QString>
class RegExpInterface
{
public:
virtual ~RegExpInterface()
{
}
virtual QString regexp(const QString &message)=0;
};
Q_DECLARE_INTERFACE(RegExpInterface, "org.myplugin.RegExpInterface")
#endif // REGEXPINTERFACE_H
实现的接口的类 头文件
---------------------------------------------
#define REGEXPPLUGIN_H
#include <QObject>
#include <QString>
#include "regexpinterface.h"
class RegExpPlugin : public QObject, RegExpInterface
{
Q_OBJECT
Q_PLUGIN_METADATA (IID "org.myplugin.RegExpInterface")
Q_INTERFACES(RegExpInterface)
public:
QString regexp(const QString &message);
};
#endif // REGEXPPLUGIN_H
源文件
-------------------------------------
#include "regexpplugin.h"
QString RegExpPlugin::regexp(const QString &message)
{
return "hello world";
}
pro 文件
-------------------------------------
HEADERS += \
regexpplugin.h \
regexpinterface.h
SOURCES += \
regexpplugin.cpp
TEMPLATE=lib
CONFIG +=plugin
TARGET =regexpplugin
测试插件的窗口
窗口的头文件
-----------------------------------------
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "regexpinterface.h"
#include <QPluginLoader>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_clicked();
private:
RegExpInterface *regInterface;
Ui::MainWindow *ui;
bool loadPlugin();
};
#endif // MAINWINDOW_H
窗口的源文件
----------------------------------------
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
bool MainWindow::loadPlugin()
{
QPluginLoader loader("../plug/plugin.dll");
QObject *obj= loader.instance();
if(obj)
{
regInterface=qobject_cast<RegExpInterface *>(obj);
if(regInterface)
{
QString str= regInterface->regexp("jjjkk");
ui->Result->setText(str);
}
}
}
void MainWindow::on_pushButton_clicked()
{
this->loadPlugin();
}
插件中常用的pri文件
------------------------------------------------
HEADERS += \
$$PWD/common/HpQRCodeInterface.h
PROJECT_COMPONENTSOURCE = $$PWD/common
PLUGIN_INSTALL_DIRS = $$[QT_INSTALL_LIBS]/ukui-demo-plugin
相关文章
- [Qt教程] 第20篇 2D绘图(十)图形视图框架(下)
- Qt-qt creator 运行 出现 "can not open .... jom for write 解决方案
- Qt-QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connect...
- Qt-char*与QString互转 floatToqstring char*与QString互转 qt字符串与字符串数组操作 Qt 的QString类的使用
- Qt编写自定义控件32-等待进度条控件
- Qt编写自定义控件插件路过的坑及注意事项
- Qt编写安防视频监控系统49-多数据库支持
- Qt编译数据库插件通用步骤说明
- Qt模式视图设计
- Qt QCheckBox QRadioButton
- Qt .pro和.qrc创建及介绍
- 设置Qt插件路径
- qt操作json(QT 教程)
- 【Qt】在ubuntu上为可执行程序qt创建图标(亲测)
- 2、QT基础——创建Qt项目
- Qt 之自定义界面(添加自定义标题栏)
- Qt中使用vlc-qt处理音视频
- 关于Visual Studio下载Qt插件很慢的解决方案之一
- Qt模拟鼠标点击事件
- Qt creator 编译错误 :cannot find file .pro qt
- Qt学习一门:直接使用QT具