目录
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 内容如下:
#ifndef INTERFACE_H
#define INTERFACE_H
#include <QGenericPlugin>
#include <QtPlugin>
//定义接口
class Interface : public QObject
{
Q_OBJECT
public:
virtual ~Interface() {}
virtual int add(int a,int b) = 0;
};
QT_BEGIN_NAMESPACE
Q_DECLARE_INTERFACE(Interface, "org.qt-project.Qt.QGenericPluginFactoryInterface")
QT_END_NAMESPACE
#endif // INTERFACE_H
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "interface.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
void LoadPlugin();
Interface* m_pInterface = nullptr;
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
void MainWindow::LoadPlugin()
{
QString filename = qApp->applicationDirPath() + "/Plugin.dll";
qDebug()<<"LoadPlugin filename:"<<filename;
QPluginLoader pluginLoader(filename);
QGenericPlugin *plugin = qobject_cast<QGenericPlugin *>(pluginLoader.instance());
if (plugin) {
// 使用 QGenericPlugin 的 create 函数来创建插件对象
QObject *pluginInstance = plugin->create("pluginA", "");
if (pluginInstance){
// 尝试转换插件对象为MyPlugin类型
Interface *myPlugin = qobject_cast<Interface *>(pluginInstance);
qDebug()<<"LoadPlugin success! "<<myPlugin->add(112,1);
// 获取插件的元数据信息(如果有的话)
QJsonObject metaData = pluginLoader.metaData();
QJsonObject jsonObject = metaData.value("MetaData").toObject();
QJsonObject obj = jsonObject.value("MetaData").toObject();
QString version = obj.value("Version").toString();
QString Name = obj.value("Name").toString();
QString Description = obj.value("Description").toString();
QString Class = obj.value("Class").toString();
QString Author = obj.value("Author").toString();
QString License = obj.value("License").toString();
qDebug()<<version;
qDebug()<<Name;
qDebug()<<Description;
qDebug()<<Class;
qDebug()<<Author;
qDebug()<<License;
return ;
}
else
qDebug()<<"qobject_cast fail!";
}
else{
QMessageBox::warning(0,"warning","LoadPlugin fail!");
qDebug()<<"LoadPlugin fail!";
}
}
3 插件程序
1 右击主项目 新建子项目项
(也可以新建另一个工程 )
选择C++ Library ---->QT Plugin
将 Interface.h拷贝到该目录下 与Plugin.pro同级目录
genericplugin.h
#ifndef GENERICPLUGIN_H
#define GENERICPLUGIN_H
#include <QGenericPlugin>
#include "Interface.h"
class GenericPlugin : public QGenericPlugin
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QGenericPluginFactoryInterface/1.0" FILE "Plugin.json")
public:
explicit GenericPlugin(QObject *parent = nullptr);
private:
QObject *create(const QString &name, const QString &spec) override;
};
#endif // GENERICPLUGIN_H
genericplugin.cpp
#include "genericplugin.h"
#include "plugina.h"
#include "pluginb.h"
GenericPlugin::GenericPlugin(QObject *parent)
: QGenericPlugin(parent)
{
}
QObject *GenericPlugin::create(const QString &name, const QString &spec)
{
static_assert(true, "You need to implement this function");
if(name == "pluginA")
return new pluginA();
if(name == "pluginB")
return new pluginB();
return nullptr;
}
plugina.h
#ifndef PLUGINA_H
#define PLUGINA_H
#include <QObject>
#include "Interface.h"
class pluginA : public Interface
{
Q_OBJECT
Q_INTERFACES(Interface)
public:
pluginA();
int add(int a,int b) override;
};
#endif // PLUGINA_H
plugina.cpp
#include "plugina.h"
pluginA::pluginA()
{
}
int pluginA::add(int a, int b)
{
return 11;
}
pluginb.h
#ifndef PLUGINB_H
#define PLUGINB_H
#include <QObject>
#include "Interface.h"
class pluginB : public Interface
{
Q_OBJECT
Q_INTERFACES(Interface)
public:
pluginB();
int add(int a,int b) override;
};
#endif // PLUGINB_H
pluginb.cpp
#include "pluginb.h"
pluginB::pluginB()
{
}
int pluginB::add(int a, int b)
{
return 12;
}
Plugin.json
{
"MetaData": {
"Version": "1.0.0",
"Name": "MyCustomPlugin",
"Description": "A custom plugin for my application",
"Class": "MyCustomPluginClass",
"Author": "Your Name",
"License": "Some License",
"Dependencies": {
"OtherPlugin": ">=1.2.0"
}
}
}