A plug-in is a program (written in accordance with a certain standard application program interface), which is positioned to develop a program that implements functions that the application software platform does not have.
The plug-in must rely on the application to perform its own functions, and the plug-in alone cannot run normally; on the contrary, the application does not need to rely on the plug-in to run, so that the plug-in can be loaded on the application and updated dynamically. Does not cause any changes to application integrity (hot update).
Just like a hardware plug-in card, the plug-in can be deleted, inserted and modified at any time, so the structure is very flexible, easy to modify, and convenient for software upgrades and maintenance.
The author of this article is original, please attach the source of the article and the link of this article for reprinting.
QT plugin development directory
1.4 DeclareInterface.h virtual function file
1 Create a new QT project
New QMainPlugin
1.1 Pro file
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
DeclareInterface.h \
mainwindow.h
FORMS += \
mainwindow.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
1.2 .h files
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QDir>
#include <QDebug>
#include <QMessageBox>
#include <QPluginLoader>
#include "DeclareInterface.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
bool loadPlugin(); //加载插件
DeclareInterface* m_pInterface = nullptr; //获取插件类型
};
#endif // MAINWINDOW_H
1.3 .cpp files
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
if(!loadPlugin()){
QMessageBox::warning(this, "Error", "Could not load the plugin");
}
}
MainWindow::~MainWindow()
{
delete ui;
}
bool MainWindow::loadPlugin(){
QPluginLoader pluginLoader("HelloCTK.dll");
QObject *plugin = pluginLoader.instance();
qDebug() << __FUNCTION__ << pluginLoader.errorString();
if (plugin) {
m_pInterface = qobject_cast<DeclareInterface *>(plugin);
if (m_pInterface)
return true;
}
return false;
}
void MainWindow::on_pushButton_clicked()
{
int a = 5;
int b = 5;
int r = -1;
if(m_pInterface){
r = m_pInterface->add(a, b);
}
QMessageBox::warning(this, "插件调用成功", QString::number(r));
}
1.4 DeclareInterface.h virtual function file
//declareinterface.h
#ifndef DECLAREINTERFACE_H
#define DECLAREINTERFACE_H
#include <QWidget>
//定义接口
class DeclareInterface
{
public:
virtual ~DeclareInterface() {}
virtual int add(int a,int b) = 0;
};
//一定是唯一的标识符
#define DeclareInterface_iid "Examples.Plugin.DeclareInterface"
QT_BEGIN_NAMESPACE
Q_DECLARE_INTERFACE(DeclareInterface, DeclareInterface_iid)
QT_END_NAMESPACE
#endif // DECLAREINTERFACE_H
2 Create a new Plugin
Create a new QPluginA project
2.1 QPluginA.pro
QT += core
QT += gui
QT += widgets
TEMPLATE = lib
CONFIG += plugin
TARGET = HelloCTK
CONFIG += c++11
EXAMPLE_FILES = qtplugin.json
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
qplugina.cpp
HEADERS += \
DeclareInterface.h \
QPluginA_global.h \
qplugina.h
# Default rules for deployment.
unix {
target.path = /usr/lib
}
!isEmpty(target.path): INSTALLS += target
2.2 qplugina.h
#ifndef QPLUGINA_H
#define QPLUGINA_H
#include "QPluginA_global.h"
#include <QObject>
#include <QtPlugin>
#include "DeclareInterface.h"
class QPluginA : public QObject, public DeclareInterface
{
Q_OBJECT
Q_INTERFACES(DeclareInterface)
Q_PLUGIN_METADATA(IID "qtplugin.json")
public:
QPluginA(QObject *parent = 0);
int add(int a, int b);
};
#endif // QPLUGINA_H
2.3 qplugina.cpp
#include "qplugina.h"
QPluginA::QPluginA(QObject *parent) : QObject(parent)
{
}
int QPluginA::add(int a, int b)
{
return a+b;
}
2.4 DeclareInterface.h
//declareinterface.h
#ifndef DECLAREINTERFACE_H
#define DECLAREINTERFACE_H
#include <QWidget>
//定义接口
class DeclareInterface
{
public:
virtual ~DeclareInterface() {}
virtual int add(int a,int b) = 0;
};
//一定是唯一的标识符
#define DeclareInterface_iid "Examples.Plugin.DeclareInterface"
QT_BEGIN_NAMESPACE
Q_DECLARE_INTERFACE(DeclareInterface, DeclareInterface_iid)
QT_END_NAMESPACE
#endif // DECLAREINTERFACE_H
3 effects
4 directory structure