定义的接口
----------------------------------------------
#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,RegExpInterface,regexpplugin,ui,QString,include,MainWindow,定义 From: https://blog.51cto.com/u_4018548/6430542