1,定義接口文件
/******************************************************************************************************
* Copyright (C) 2014, All right reserved.
* file Basic_Module_Interface.h
* version 1.0
* author NingJian ([email protected])
* 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 ([email protected])
* 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編譯環境安裝與配置 http://www.linuxidc.com/Linux/2013-08/88539.htm
Linux系統下QT環境搭建 http://www.linuxidc.com/Linux/2013-07/87576.htm
Ubuntu下QT控制台程序無法運行的解決方案以及XTerm的配置方法 http://www.linuxidc.com/Linux/2013-06/86244.htm
Ubuntu 10.04下QT4.7.4移植詳解 http://www.linuxidc.com/Linux/2013-01/77930.htm
Ubuntu 14.04下安裝部署Qt5開發環境 http://www.linuxidc.com/Linux/2014-05/101774.htm
Qt 的詳細介紹:請點這裡
Qt 的下載地址:請點這裡