当前位置:   article > 正文

QT插件简单使用2

QT插件简单使用2

目录

1 总的目录结构

2 主程序

3 插件程序

4 运行结果


相比原来的QT插件简单使用-CSDN博客增加了

         QObject *create(const QString &name, const QString &spec) override;

函数的使用和Plugin.json的使用

1 总的目录结构

        编译器mingw-64

2 主程序

1 新建一个其他项目的子文件目录后,创建一个普通的 QMainWindow 项目 

主程序中添加头文件  Interface.h 内容如下:

  1. #ifndef INTERFACE_H
  2. #define INTERFACE_H
  3. #include <QGenericPlugin>
  4. #include <QtPlugin>
  5. //定义接口
  6. class Interface : public QObject
  7. {
  8. Q_OBJECT
  9. public:
  10. virtual ~Interface() {}
  11. virtual int add(int a,int b) = 0;
  12. };
  13. QT_BEGIN_NAMESPACE
  14. Q_DECLARE_INTERFACE(Interface, "org.qt-project.Qt.QGenericPluginFactoryInterface")
  15. QT_END_NAMESPACE
  16. #endif // INTERFACE_H

mainwindow.h

  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3. #include <QMainWindow>
  4. #include "interface.h"
  5. QT_BEGIN_NAMESPACE
  6. namespace Ui { class MainWindow; }
  7. QT_END_NAMESPACE
  8. class MainWindow : public QMainWindow
  9. {
  10. Q_OBJECT
  11. public:
  12. MainWindow(QWidget *parent = nullptr);
  13. ~MainWindow();
  14. void LoadPlugin();
  15. Interface* m_pInterface = nullptr;
  16. private slots:
  17. void on_pushButton_clicked();
  18. private:
  19. Ui::MainWindow *ui;
  20. };
  21. #endif // MAINWINDOW_H
mainwindow.cpp
  1. void MainWindow::LoadPlugin()
  2. {
  3. QString filename = qApp->applicationDirPath() + "/Plugin.dll";
  4. qDebug()<<"LoadPlugin filename:"<<filename;
  5. QPluginLoader pluginLoader(filename);
  6. QGenericPlugin *plugin = qobject_cast<QGenericPlugin *>(pluginLoader.instance());
  7. if (plugin) {
  8. // 使用 QGenericPlugin 的 create 函数来创建插件对象
  9. QObject *pluginInstance = plugin->create("pluginA", "");
  10. if (pluginInstance){
  11. // 尝试转换插件对象为MyPlugin类型
  12. Interface *myPlugin = qobject_cast<Interface *>(pluginInstance);
  13. qDebug()<<"LoadPlugin success! "<<myPlugin->add(112,1);
  14. // 获取插件的元数据信息(如果有的话)
  15. QJsonObject metaData = pluginLoader.metaData();
  16. QJsonObject jsonObject = metaData.value("MetaData").toObject();
  17. QJsonObject obj = jsonObject.value("MetaData").toObject();
  18. QString version = obj.value("Version").toString();
  19. QString Name = obj.value("Name").toString();
  20. QString Description = obj.value("Description").toString();
  21. QString Class = obj.value("Class").toString();
  22. QString Author = obj.value("Author").toString();
  23. QString License = obj.value("License").toString();
  24. qDebug()<<version;
  25. qDebug()<<Name;
  26. qDebug()<<Description;
  27. qDebug()<<Class;
  28. qDebug()<<Author;
  29. qDebug()<<License;
  30. return ;
  31. }
  32. else
  33. qDebug()<<"qobject_cast fail!";
  34. }
  35. else{
  36. QMessageBox::warning(0,"warning","LoadPlugin fail!");
  37. qDebug()<<"LoadPlugin fail!";
  38. }
  39. }
3 插件程序

1 右击主项目 新建子项目项

(也可以新建另一个工程 ) 

选择C++ Library ---->QT Plugin

将 Interface.h拷贝到该目录下 与Plugin.pro同级目录

genericplugin.h
  1. #ifndef GENERICPLUGIN_H
  2. #define GENERICPLUGIN_H
  3. #include <QGenericPlugin>
  4. #include "Interface.h"
  5. class GenericPlugin : public QGenericPlugin
  6. {
  7. Q_OBJECT
  8. Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QGenericPluginFactoryInterface/1.0" FILE "Plugin.json")
  9. public:
  10. explicit GenericPlugin(QObject *parent = nullptr);
  11. private:
  12. QObject *create(const QString &name, const QString &spec) override;
  13. };
  14. #endif // GENERICPLUGIN_H
genericplugin.cpp
  1. #include "genericplugin.h"
  2. #include "plugina.h"
  3. #include "pluginb.h"
  4. GenericPlugin::GenericPlugin(QObject *parent)
  5. : QGenericPlugin(parent)
  6. {
  7. }
  8. QObject *GenericPlugin::create(const QString &name, const QString &spec)
  9. {
  10. static_assert(true, "You need to implement this function");
  11. if(name == "pluginA")
  12. return new pluginA();
  13. if(name == "pluginB")
  14. return new pluginB();
  15. return nullptr;
  16. }

plugina.h

  1. #ifndef PLUGINA_H
  2. #define PLUGINA_H
  3. #include <QObject>
  4. #include "Interface.h"
  5. class pluginA : public Interface
  6. {
  7. Q_OBJECT
  8. Q_INTERFACES(Interface)
  9. public:
  10. pluginA();
  11. int add(int a,int b) override;
  12. };
  13. #endif // PLUGINA_H

plugina.cpp

  1. #include "plugina.h"
  2. pluginA::pluginA()
  3. {
  4. }
  5. int pluginA::add(int a, int b)
  6. {
  7. return 11;
  8. }

pluginb.h

  1. #ifndef PLUGINB_H
  2. #define PLUGINB_H
  3. #include <QObject>
  4. #include "Interface.h"
  5. class pluginB : public Interface
  6. {
  7. Q_OBJECT
  8. Q_INTERFACES(Interface)
  9. public:
  10. pluginB();
  11. int add(int a,int b) override;
  12. };
  13. #endif // PLUGINB_H

pluginb.cpp

  1. #include "pluginb.h"
  2. pluginB::pluginB()
  3. {
  4. }
  5. int pluginB::add(int a, int b)
  6. {
  7. return 12;
  8. }

Plugin.json

  1. {
  2. "MetaData": {
  3. "Version": "1.0.0",
  4. "Name": "MyCustomPlugin",
  5. "Description": "A custom plugin for my application",
  6. "Class": "MyCustomPluginClass",
  7. "Author": "Your Name",
  8. "License": "Some License",
  9. "Dependencies": {
  10. "OtherPlugin": ">=1.2.0"
  11. }
  12. }
  13. }
4 运行结果

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/305128
推荐阅读
相关标签
  

闽ICP备14008679号