QT Plugin 热拔插插件

        QT中 热加载插件 和动态加载DLL有什么区别 ?热加载插件是在程序运行时动态加载插件,而动态加载DLL是在程序编译时就将DLL文件链接到程序中。热加载插件可以在不停止程序的情况下更新插件,而动态加载DLL需要重新编译程序才能更新DLL文件。

        QT 中 热加载插件如何在不停止程序的情况下更新插件?使用 QPluginLoader 类来实现热加载插件。在程序运行时,使用 QPluginLoader 加载插件,并使用 QObject::connect() 函数连接插件的信号和槽。当需要更新插件时,可以使用 QPluginLoader::unload() 函数卸载旧插件,然后再次使用 QPluginLoader 加载新插件即可。这样就可以在不停止程序的情况下更新插件。

        个人理解在实际项目中使用DLL动态库的时候,如果需要更新或者进行替换的时候需要关闭程序替换DLL在重启程序,一般可能没什么但是有些特殊场景需要24小时工作或者有通信、一直有任务在运行不适合关闭主程序,这种情况则非常需要 QT Plugin 热拔插插件 功能,无需重启程序在设置界面加个设置按钮和连接一个信号槽就可以实现动态不停止程序的情况下更新插件。

QT Plugin 热拔插插件_第1张图片

 本文作者原创,转载请附上文章出处与本文链接。

QT Plugin 热拔插插件目录

1 QPluginA

1.1 QPluginA.pro

1.2 DeclareInterface.h

1.3 qplugina.h

1.4 qplugina.cpp

2 QPluginB

2.1 QPluginB.pro

2.2 DeclareInterface.h

2.3 qpluginb.h

2.4 qpluginb.cpp

3 QMainPlugin

3.1 QMainPlugin.pro

3.2 DeclareInterface.h

3.3 mainwindow.h

3.4 mainwindow.cpp


1 QPluginA

        插件A源代码

1.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

1.2 DeclareInterface.h

//declareinterface.h
#ifndef DECLAREINTERFACE_H
#define DECLAREINTERFACE_H


#include 

//定义接口
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

1.3 qplugina.h

#ifndef QPLUGINA_H
#define QPLUGINA_H

#include "QPluginA_global.h"
#include 
#include 
#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

1.4 qplugina.cpp

#include "qplugina.h"

QPluginA::QPluginA(QObject *parent) : QObject(parent)
{
}

int QPluginA::add(int a, int b)
{
    return a+b;
}

2 QPluginB

        插件B源代码

2.1 QPluginB.pro

QT += core
QT += gui
QT             += widgets


TEMPLATE = lib
CONFIG += plugin
TARGET = HelloCTKS


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 += \
    qpluginb.cpp

HEADERS += \
    DeclareInterface.h \
    QPluginB_global.h \
    qpluginb.h

# Default rules for deployment.
unix {
    target.path = /usr/lib
}
!isEmpty(target.path): INSTALLS += target

2.2 DeclareInterface.h

//declareinterface.h
#ifndef DECLAREINTERFACE_H
#define DECLAREINTERFACE_H


#include 

//定义接口
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.3 qpluginb.h

#ifndef QPLUGINB_H
#define QPLUGINB_H

#include "QPluginB_global.h"
#include 
#include 
#include "DeclareInterface.h"

class  QPluginB : public QObject, public DeclareInterface
{
public:
    Q_OBJECT
    Q_INTERFACES(DeclareInterface)
    Q_PLUGIN_METADATA(IID  "qtplugin.json")


public:

    QPluginB(QObject *parent = 0);
    int add(int a, int b);
};

#endif // QPLUGINB_H

2.4 qpluginb.cpp

#include "qpluginb.h"

QPluginB::QPluginB(QObject *parent) : QObject(parent)
{
}

int QPluginB::add(int a, int b)
{
    return a-b;
}

3 QMainPlugin

        项目源代码

3.1 QMainPlugin.pro

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

3.2 DeclareInterface.h

//declareinterface.h
#ifndef DECLAREINTERFACE_H
#define DECLAREINTERFACE_H


#include 

//定义接口
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.3 mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 

#include 
#include 
#include 
#include 

#include "DeclareInterface.h"

#pragma execution_character_set("utf-8")
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();

    void on_pushButton_2_clicked();

    void on_pushButton_3_clicked();

private:
    Ui::MainWindow *ui;

    QPluginLoader pluginLoader;

    bool loadPlugin(QString strPlugin);   //加载插件
    DeclareInterface* m_pInterface = nullptr;  //获取插件类型
};
#endif // MAINWINDOW_H

3.4 mainwindow.cpp

#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(QString strPlugin){

    pluginLoader.setFileName(strPlugin);
    QObject *plugin = pluginLoader.instance();
    qDebug() << __FUNCTION__ << pluginLoader.errorString();
    if (plugin) {
        m_pInterface = qobject_cast(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));

}

void MainWindow::on_pushButton_2_clicked()
{
    pluginLoader.unload();
    if(!loadPlugin("HelloCTK.dll")){
        QMessageBox::warning(this, "Error", "Could not load the plugin");
    }

}

void MainWindow::on_pushButton_3_clicked()
{
    pluginLoader.unload();
    if(!loadPlugin("HelloCTKS.dll")){
        QMessageBox::warning(this, "Error", "Could not load the plugin");
    }
}

PS: 程序效果图  PluginA 实现了减法 PluginB实现了加法

你可能感兴趣的:(#,QT开发之路,第二章,qt,开发语言)