///
/// \brief get_moudle_describe
/// 获取模块的描述信息
/// \return
///
virtual std::string get_moudle_describe() const =0;
};
QT_BEGIN_NAMESPACE
#define IBasicModule_iid "com.twsz.tc.ningjian.IBasicModule/1.0"
Q_DECLARE_INTERFACE(IBasicModule, IBasicModule_iid)
QT_END_NAMESPACE
//Q_DECLARE_INTERFACE(IBasicModule,"com.twsz.tc.ningjian.IBasicModule/1.0");
#endif // BASIC_MODULE_INTERFACE_H
基本是纯虚类要在末尾添加
Q_DECLARE_INTERFACE(IBasicModule, IBasicModule_iid) 即可2、实现插件
头文件 如下: cpp正常就好了,注意要继承QObject 和 接口类就可以了,还要在在头文件中添加 Q_OBJECT Q_PLUGIN_METADATA(IID "com.twsz.tc.ningjian.IBasicModule/1.0" ) Q_INTERFACES(IBasicModule)
在项目属性中要添加
TEMPLATE = lib CONFIG += plugin
/******************************************************************************************************
* Copyright (C) 2014, All right reserved.
* file
* version 1.0
* author NingJian (freegodly@gmail.com)
* brief
* detail
* TODO
* history 2014-9-17 created by NingJian
*
* note
******************************************************************************************************/
#ifndef STT_BASIC_MOUDLE_H
#define STT_BASIC_MOUDLE_H
#include <Basic_Module_Interface.h>
#include <iostream>
#include <QScriptEngine>
#include <QScriptValue>
#include <QtCore/QtPlugin>
#include <iostream>
using namespace std;
class STT_Basic_Moudle:public QObject,public IBasicModule
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "com.twsz.tc.ningjian.IBasicModule/1.0" )
Q_INTERFACES(IBasicModule)
public:
STT_Basic_Moudle();
// IBasicModule interface
public:
bool init(std::map<int, TEST_INFO> &test_info, std::map<string, TEST_FUN_INFO> &test_fun_info, std::map<string, string> &moudles_config, RUN_FUN run_fun) const;
bool release() const;
bool initiation(int test_id) const;
bool finish(int test_id) const;
std::string get_moudle_version() const;
std::string get_moudle_describe() const;
void reg_fun(int test_id, QScriptEngine *eng) const;
void reg_ui_fun(REG_UI_FUN reg_ui_f) const;
std::string get_moudle_name() const;
public:
///
/// \brief G_Test_Info
///
static std::map<int,TEST_INFO> *STT_G_Test_Info;
///
/// \brief G_Test_Fun_Info
///
static std::map<std::string,TEST_FUN_INFO> *STT_G_Test_Fun_Info;
///
/// \brief G_Test_Run_Fun
///
static RUN_FUN STT_G_Test_Run_Fun;
///
/// \brief STT_G_Moudles_Config
///
static std::map<std::string,std::string> *STT_G_Moudles_Config;
};
std::string get_stt_variable(int test_id,std::string key);
void set_stt_variable(int test_id,std::string key,std::string value);
void add_fun(const char * moudle_name,const char * fun_name ,const char * fun_describe);
#endif // STT_BASIC_MOUDLE_H
3、使用插件
下面是遍历加载plugins目录下所有实现 IBasicModule 接口的插件 获取相应的实例就可以调用了
头文件记得添加
#include <QPluginLoader>
//注册模块指令
QDir plugindir = QDir(QDir::currentPath()+"/plugins");
int i = 0;
foreach(QString filename,plugindir.entryList(QDir::Files)){
QPluginLoader loader(plugindir.absoluteFilePath(filename));
if (IBasicModule * base_moudle = qobject_cast<IBasicModule *>(loader.instance()))
{
qDebug()<<base_moudle->get_moudle_name().c_str();
STT_Global::basicModule_map.insert(std::pair<std::string,IBasicModule*>(base_moudle->get_moudle_name(),base_moudle));
STT_Global::PlugsList.push_back( base_moudle->get_moudle_name());
base_moudle->init(G_Test_Info,G_Test_Fun_Info,G_Moudles_Config,G_STT_Run_Fun);
base_moudle->reg_fun(-1,G_STT_Interpreter[-1]);
base_moudle->reg_ui_fun(G_Reg_UI_FUN);
i++;
emit STT_Global::fl->signal_process(20+ 80 * i / plugindir.entryList(QDir::Files).size());
}
}