• qt 定义插件



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




    
    




    
    


  • 相关阅读:
    new 做了什么
    create-react-app+react-app-rewired引入antd实践
    实战build-react(二)-------引入Ant Design(增加)
    package.json文件
    读书多些会怎样
    关于学历
    QDialog弹出一个窗口,改变窗口大小
    #include <thread>
    #include <memory>
    #include <map>
  • 原文地址:https://www.cnblogs.com/bruce1992/p/14314710.html
Copyright © 2020-2023  润新知