qt 定义插件
2024-09-05 19:58:00
定义的接口
----------------------------------------------
#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
最新文章
- winform中button点击后再点击其他控件致使button失去焦点,此时button出现黑色边线,去掉黑色边线的方法
- ACID
- Support Vector Machine (1) : 简单SVM原理
- itoa
- Linux 套接字编程中的 5 个隐患
- Docker学习笔记(4) — 开启Docker远程访问
- malloc内存分配与free内存释放的原理
- HDU 1890 Robotic Sort | Splay
- 整理JRE瘦身或精简JRE
- Java 计算两个日期相差月数、天数
- Git(介绍和安装)
- Cygwin命令
- GooglePlay发布应用后,支持的 Android 设备 0 台设备
- 【Codeforces 332C】Students&#39; Revenge
- linux下比较两个文本文件的不同——diff命令
- httppost core net
- POJ 1743 Musical Theme (字符串HASH+二分)
- 2018-2019-1 1723《程序设计与数据结构》第1&;2周作业 总结
- Codeforces Round #417 (Div. 2) B. Sagheer, the Hausmeister
- Google Voice号码使用说明及用途