zoukankan      html  css  js  c++  java
  • 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




    
    




    
    


  • 相关阅读:
    vue -resource 文件提交提示process,或者拦截处理
    利用vue写一个复选框的组件
    webpack处理媒体文件(图片/视频和音频)
    函数——惰性函数
    函数——函数的节流与防抖
    函数——箭头函数&自执行函数(二)
    js重点——作用域——作用域分类及变量提升
    js重点——作用域——内部原理
    js之数据类型(对象类型——单体内置对象——JSON)
    js之数据类型(对象类型——单体内置对象——Math)
  • 原文地址:https://www.cnblogs.com/bruce1992/p/14314710.html
Copyright © 2011-2022 走看看