QT Plugin 插件开发

        插件是一种(遵循一定规范的应用程序接口编写出来的)程序,定位于开发实现应用软件平台不具备的功能的程序。
        插件必须依赖于应用程序才能发挥自身功能,仅靠插件是无法正常运行的;相反地,应用程序并不需要依赖插件就可以运行,这样一来,插件就可以加载到应用程序上并且动态更新而不会对应用诚信度造成任何改变(热更新)。

        插件就行硬件插卡一样,可以被随时删除、插入和修改,所以结构很灵活,容易修改,方便软件的升级和维护。
   QT Plugin 插件开发_第1张图片

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

QT 插件开发目录

1 新建QT项目

1.1 Pro文件

1.2 .h文件

1.3 .cpp文件

1.4 DeclareInterface.h虚函数文件

2 新建Plugin插件

 2.1 QPluginA.pro

2.2 qplugina.h

2.3 qplugina.cpp

2.4 DeclareInterface.h

3 效果

4 目录结构


1 新建QT项目

        新建QMainPlugin

QT Plugin 插件开发_第2张图片

 

1.1 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

1.2 .h文件

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 

#include 
#include 
#include 
#include 

#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文件

#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(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虚函数文件

//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 新建Plugin插件

        新建QPluginA项目

QT Plugin 插件开发_第3张图片

 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 
#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

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 

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

QT Plugin 插件开发_第4张图片

4 目录结构

QT Plugin 插件开发_第5张图片

 

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