Qt 程序自定义插件
Qt 程序自定义插件
1,定义接口文件
/******************************************************************************************************
* Copyright (C) 2014, All right reserved.
* file Basic_Module_Interface.h
* version 1.0
* author NingJian (freegodly@gmail.com)
* brief
* detail
平台插件接口文件
* TODO
* history 2014-9-17 created by NingJian
*
* note
******************************************************************************************************/
#ifndef BASIC_MODULE_INTERFACE_H
#define BASIC_MODULE_INTERFACE_H
#include <iostream>
#include <map>
#include <tr1/memory>
#include <tr1/functional>
#include <QScriptEngine>
#include <QScriptValue>
#include <QtCore/QtPlugin>
#include <QString>
/* ############################################################################################################# */
///
/// > 方便获取软件编译时间
///
#ifndef STT_BUILD_TIME
#define STT_BUILD_TIME std::string("Build Time: ")+std::string(__TIME__)+std::string(" ")+std::string(__DATE__)
#endif
/* ############################################################################################################# */
///
/// > 定义测试结构信息
///
#ifndef STT_TEST_INFO
#define STT_TEST_INFO
///
/// \brief The TEST_INFO struct
///
struct TEST_INFO
{
///
/// \brief id [ID信息]
///
int id;
std::string name;
///
/// \brief variable_map [测试结构的自定义数据存储]
///
std::map<std::string,std::string> variable_map;
};
///
/// \brief The TEST_FUN_INFO struct
///
struct TEST_FUN_INFO
{
std::string modle_name;
std::string fun_name;
std::string fun_describe;
};
#endif
/* ############################################################################################################# */
///
/// > 定义模块函数指针类型
///
#ifndef STT_FUN_REG
#define STT_FUN_REG
///
/// > 实现运行指令的函数类型 定义
///
typedef bool (*RUN_FUN)(QString fun_name, int test_id,QString arg1 ,QString arg2,QString arg3,QString arg4, QString arg5,QString arg6,QString arg7,QString arg8,QString arg9);
///
/// > 实现运行模块配置函数类型 定义
///
typedef bool (*UI_FUN)();
///
/// > 实现注册模块配置函数 定义
///
typedef void (*REG_UI_FUN) (QString image_path,QString config_name ,UI_FUN f );
#endif
/* ############################################################################################################# */
class IBasicModule
{
public:
virtual ~IBasicModule(){}
///
/// \brief initiation
/// 加载初始化资源等
///
virtual bool init(std::map<int,TEST_INFO> &test_info,std::map<std::string,TEST_FUN_INFO> &test_fun_info,std::map<std::string,std::string> &moudles_config,RUN_FUN run_fun)const =0;
///
/// \brief initiation
/// 加载释放资源等
///
virtual bool release()const =0;
///
/// \brief initiation
/// 测试前的初始化资源等
///
virtual bool initiation(int test_id)const =0;
///
/// \brief initiation
/// 测试后的释放资源等
///
virtual bool finish(int test_id)const =0;
///
/// \brief reg_fun
/// 注册命令的函数 需要实现要注册到平台的指令
/// 保存STT平台传来的函数和结构信息供该类以后调用
///
/// \param rf
/// 注册指令的平台回调函数指针
/// 如果模块有自定义数据导入,需要向test_info中添加数据
///
virtual void reg_fun(int test_id,QScriptEngine *eng ) const = 0;
virtual void reg_ui_fun(REG_UI_FUN reg_ui_f)const = 0;
///
/// \brief get_moudle_version
/// 获取模块的版本信息
/// \return
///
virtual std::string get_moudle_version() const =0;
///
/// \brief get_moudle_name
/// \return
///
virtual std::string get_moudle_name() const =0;
///
/// \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());
}
}
Ubuntu 环境下Gtk与QT编译环境安装与配置
Linux系统下QT环境搭建
Ubuntu下QT控制台程序无法运行的解决方案以及XTerm的配置方法
Ubuntu 10.04下QT4.7.4移植详解
Ubuntu 14.04下安装部署Qt5开发环境
Qt 的详细介绍:请点这里
Qt 的下载地址:请点这里
本文永久更新链接地址:
评论暂时关闭