系列文章目录
第一章 Qt插件创建并调用插件
第二章 Qt创建插件管理器统一管理插件
第三章 Qt插件之间相互通信
第四章 Qt创建并加载一个窗口插件
第五章 Qt插件工程作为子工程
前言
一、创建一个窗口插件
1.重写继承于插件基类的纯虚函数
#include "PluginWidget01_global.h"
#include<QWidget>
#include"PluginInterface.h"
#include<QDebug>
QT_BEGIN_NAMESPACE
namespace Ui { class PluginWidget; }
QT_END_NAMESPACE
class PLUGINWIDGET01_EXPORT PluginWidget : public QWidget ,public PluginInterface
{
Q_OBJECT
Q_INTERFACES(PluginInterface)
Q_PLUGIN_METADATA(IID "pluginWidget01")
public:
explicit PluginWidget(QWidget *parent = nullptr);
~PluginWidget();
protected:
virtual QString getName() const override
{
return "PluginWidget";
}
virtual QString showText() const override
{
return "this is PluginWidget";
}
virtual void recMsgFromManager(PluginMetaData metaData) override
{
qDebug() << "插件PluginWidget接收到消息:" << metaData.msg;
if("show" == metaData.msg)
this->show();
}
signals:
virtual void sendMsgManager(PluginMetaData) override;
private:
Ui::PluginWidget *ui;
};
二、加载创建的窗口插件
1.通过插件管理器加载窗口插件
class PluginManger : public QObject
{
Q_OBJECT
public:
static PluginManger *instance()
{
if(nullptr == m_instance)
{
m_instance = new PluginManger;
}
return m_instance;
}
void loadAllPlugins();
void unloadAllPlugins();
void loadPlugin(const QString &filePath);
void unloadPlugin(const QString &filePath);
QPluginLoader *getPlugin(const QString &name);
QVariant getPluginName(QPluginLoader *loader);
public slots:
void recMsgFromManager(PluginMetaData metadata);
private:
explicit PluginManger(QObject *parent = nullptr);
~PluginManger();
QHash<QString,QPluginLoader *> loaders;
QHash<QString,QString> names;
static PluginManger *m_instance;
class GarbageCollector
{
~GarbageCollector()
{
if(PluginManger::instance())
{
delete PluginManger::instance();
PluginManger::m_instance = nullptr;
}
}
};
static GarbageCollector gc;
};
void PluginManger::loadAllPlugins()
{
QDir pluginsdir(QDir::currentPath());
qDebug() << QDir::currentPath();
pluginsdir.cd("..\\Lib");
QFileInfoList pluginsInfo = pluginsdir.entryInfoList(\
QDir::Files | QDir::NoDotAndDotDot);
for(QFileInfo fileInfo : pluginsInfo)
loadPlugin(fileInfo.absoluteFilePath());
}
void PluginManger::unloadAllPlugins()
{
for(QString filePath : loaders.keys())
unloadPlugin(filePath);
}
void PluginManger::loadPlugin(const QString &filePath)
{
if(!QLibrary::isLibrary(filePath))
return;
QPluginLoader *loader = new QPluginLoader(filePath);
QString pluginName;
if(loader->load())
{
PluginInterface *plugin = qobject_cast<PluginInterface *>(loader->instance());
if(plugin)
{
pluginName = plugin->getName();
loaders.insert(filePath,loader);
names.insert(filePath,pluginName);
qDebug() << "插件名称:" << plugin->getName() << "插件信息" << plugin->showText();
connect(loader->instance(),SIGNAL(sendMsgManager(PluginMetaData)),this,SLOT(recMsgFromManager(PluginMetaData)));
}else {
delete loader;
loader = nullptr;
}
}else {
qDebug() << "loadPlugin:" << filePath << loader->errorString();
}
}
void PluginManger::unloadPlugin(const QString &filePath)
{
QPluginLoader *loader = loaders.value(filePath);
if(loader->unload())
{
loaders.remove(filePath);
delete loader;
loader = nullptr;
}
}
QPluginLoader *PluginManger::getPlugin(const QString &name)
{
return loaders.value(names.key(name));
}
QVariant PluginManger::getPluginName(QPluginLoader *loader)
{
if(loader)
return names.value(loaders.key(loader));
else {
return "";
}
}
void PluginManger::recMsgFromManager(PluginMetaData metadata)
{
auto loader = getPlugin(metadata.dest);
if(loader)
{
auto interface = qobject_cast<PluginInterface *>(loader->instance());
if(interface)
{
interface->recMsgFromManager(metadata);
}
}
}
2.通过插件管理器之间的通信调用插件
PluginManger *pm = PluginManger::instance();
auto loader = pm->getPlugin("PluginWidget");
if(loader)
{
PluginInterface *plugin = qobject_cast<PluginInterface *>(loader->instance());
PluginMetaData m;
m.dest = "PluginWidget";
m.src = "Plugin01";
m.msg = "show";
emit plugin->sendMsgManager(m);
}else {
qDebug() << "PluginWidget插件不存在";
}