JFrame(组件的加载与退出【TestPlugin】)
作者:互联网
组件位置
组件暂时都被编译进 bin 目录,之后会进行区分!
组件配置文件
该配置文件指示了哪些组件将要被加载以及加载顺序
加载组件代码:
1 bool JCore::loadPlugins(const QString &pluginsPath) 2 { 3 QFileInfoList fileList; 4 QDir dir(pluginsPath); 5 6 dir.setFilter(QDir::Files); 7 fileList = dir.entryInfoList(); 8 9 foreach (QString pluginPath, m_config.getListAllPlugins()) 10 { 11 if(_pluginExist(pluginPath, fileList)) 12 { 13 qDebug() << "加载插件 [ " << pluginPath << " ] ... "; 14 if(!loadPlugin(pluginPath)) 15 { 16 qDebug() << "加载插件 [ " << pluginPath << " ] 失败!"; 17 break; 18 } 19 else 20 { 21 qDebug() << "加载插件 [ " << pluginPath << " ] 成功!"; 22 } 23 } 24 else 25 { 26 qDebug() << "插件 [ " << pluginPath << " ] 不存在!"; 27 } 28 } 29 return true; 30 }
1 bool JCore::loadPlugin(const QString &pluginPath) 2 { 3 IPluginInterface *p_plugin = nullptr; 4 QPluginLoader* p_pluginLoader = nullptr; 5 QObject *p_instance = nullptr; 6 7 try 8 { 9 p_pluginLoader = new QPluginLoader(pluginPath); 10 p_instance = p_pluginLoader->instance(); 11 12 } 13 catch(...) 14 { 15 return false; 16 } 17 18 p_plugin = qobject_cast<IPluginInterface*>(p_instance); 19 p_plugin->initialize(); 20 21 std::shared_ptr<PluginInfo> _pluginInfo(new PluginInfo); 22 _pluginInfo->m_name = p_plugin->getPluginNmae(); 23 _pluginInfo->m_fullPath = pluginPath; 24 _pluginInfo->m_pInstance = std::shared_ptr<IPluginInterface>(p_plugin); 25 26 m_listPluginInfo.append(_pluginInfo); 27 28 return true; 29 }
插件接口文件
1 class IPluginInterface 2 { 3 public: 4 virtual ~IPluginInterface(){} 5 6 // 插件初始化 7 virtual void initialize() = 0; 8 9 // 获取插件的名字 10 virtual QString getPluginNmae() = 0; 11 12 // 插件退出时候调用的函数 13 virtual int exit() = 0; 14 };
JCore启动入口函数
1 JCore::StartUPStatus JCore::startUP(const QString &appPath) 2 { 3 if(!m_config.readAllPlugins(appPath)) 4 { 5 qDebug() << "读取插件路径失败!"; 6 return StartUPStatus::CONFIG_READ_ERROR; 7 } 8 9 if(!loadPlugins(appPath)) 10 { 11 qDebug() << "实例化插件失败!"; 12 return StartUPStatus::PLUGIN_LOAD_ERROR; 13 } 14 15 return StartUPStatus::OK; 16 }
1 MainWindow::MainWindow(QWidget *parent) : 2 QMainWindow(parent), 3 ui(new Ui::MainWindow) 4 { 5 ui->setupUi(this); 6 m_coreInstance = JCore::getInstance(); 7 if(JCore::StartUPStatus::OK != m_coreInstance->startUP(QCoreApplication::applicationDirPath() + "/")) 8 { 9 qDebug() << "框架启动失败!"; 10 } 11 }
TestPlugin 插件
1 #ifndef TESTPLUGIN_H 2 #define TESTPLUGIN_H 3 4 #include "testplugin_global.h" 5 #include "IPluginInterface.h" 6 7 class TESTPLUGINSHARED_EXPORT TestPlugin : public QObject, public IPluginInterface 8 { 9 Q_OBJECT 10 Q_PLUGIN_METADATA(IID Plugin_interface_iid FILE "QPlugin.json") 11 Q_INTERFACES(IPluginInterface) 12 public: 13 TestPlugin(QObject *parent = nullptr); 14 15 virtual ~TestPlugin() override; 16 17 // 插件初始化 18 virtual void initialize() override; 19 20 // 获取插件的名字 21 virtual QString getPluginNmae() override; 22 23 virtual int exit() override; 24 }; 25 26 #endif // TESTPLUGIN_H
1 #include "testplugin.h" 2 #include <QDebug> 3 4 TestPlugin::TestPlugin(QObject *parent) 5 : QObject(parent) 6 { 7 } 8 9 TestPlugin::~TestPlugin() 10 { 11 qDebug() << "~TestPlugin ..."; 12 } 13 14 void TestPlugin::initialize() 15 { 16 qDebug() << "TestPlugin begin initialize ..."; 17 } 18 19 QString TestPlugin::getPluginNmae() 20 { 21 return "TestPlugin"; 22 } 23 24 int TestPlugin::exit() 25 { 26 qDebug() << "TestPlugin exit ..."; 27 return 0; 28 }
测试
标签:IPluginInterface,插件,JFrame,TestPlugin,JCore,QString,virtual,加载 来源: https://www.cnblogs.com/JackZheng/p/16147229.html