QT 程序自定义插件

1,定义接口文件

  1 /******************************************************************************************************
  2 * Copyright (C) 2014, All right reserved.
  3 
  4 * file   Basic_Module_Interface.h
  5 * version  1.0
  6 * author   NingJian (freegodly@gmail.com)
  7 * brief
  8 
  9 * detail
 10       平台插件接口文件
 11 * TODO
 12 * history  2014-9-17 created by NingJian
 13 *
 14 * note
 15 ******************************************************************************************************/
 16 
 17 #ifndef BASIC_MODULE_INTERFACE_H
 18 #define BASIC_MODULE_INTERFACE_H
 19 
 20 
 21 #include <iostream>
 22 #include <map>
 23 #include <tr1/memory>
 24 #include <tr1/functional>
 25 #include <QScriptEngine>
 26 #include <QScriptValue>
 27 #include <QtCore/QtPlugin>
 28 #include <QString>
 29 
 30 
 31 /* ############################################################################################################# */
 32 
 33 ///
 34 ///  > 方便获取软件编译时间
 35 ///
 36 #ifndef  STT_BUILD_TIME
 37 #define STT_BUILD_TIME std::string("Build Time: ")+std::string(__TIME__)+std::string(" ")+std::string(__DATE__)
 38 #endif
 39 
 40 /* ############################################################################################################# */
 41 ///
 42 ///  > 定义测试结构信息
 43 ///
 44 #ifndef STT_TEST_INFO
 45 #define STT_TEST_INFO
 46 
 47 ///
 48 /// rief The TEST_INFO struct
 49 ///
 50 struct TEST_INFO
 51 {
 52     ///
 53     /// rief id [ID信息]
 54     ///
 55     int id;
 56 
 57     std::string name;
 58     ///
 59     /// rief variable_map  [测试结构的自定义数据存储]
 60     ///
 61     std::map<std::string,std::string> variable_map;
 62 
 63 };
 64 
 65 ///
 66 /// rief The TEST_FUN_INFO struct
 67 ///
 68 struct TEST_FUN_INFO
 69 {
 70     std::string modle_name;
 71     std::string fun_name;
 72     std::string fun_describe;
 73 };
 74 
 75 
 76 #endif
 77 
 78 /* ############################################################################################################# */
 79 
 80 ///
 81 ///  > 定义模块函数指针类型
 82 ///
 83 #ifndef STT_FUN_REG
 84 #define STT_FUN_REG
 85 
 86 
 87 ///
 88 ///   > 实现运行指令的函数类型 定义
 89 ///
 90 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);
 91 
 92 ///
 93 ///   > 实现运行模块配置函数类型 定义
 94 ///
 95 typedef bool (*UI_FUN)();
 96 
 97 ///
 98 ///   > 实现注册模块配置函数 定义
 99 ///
100 typedef  void (*REG_UI_FUN) (QString image_path,QString config_name ,UI_FUN f );
101 
102 
103 #endif
104 
105 /* ############################################################################################################# */
106 
107 class IBasicModule 
108 {
109 public:
110     virtual ~IBasicModule(){}
111     ///
112     /// rief initiation
113     /// 加载初始化资源等
114     ///
115         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;
116 
117     ///
118     /// rief initiation
119     /// 加载释放资源等
120     ///
121     virtual bool release()const =0;
122     
123     
124     ///
125     /// rief initiation
126     /// 测试前的初始化资源等
127     ///
128     virtual bool initiation(int test_id)const =0;
129 
130     ///
131     /// rief initiation
132     /// 测试后的释放资源等
133     ///
134     virtual bool finish(int test_id)const =0;
135     
136     ///
137     /// rief reg_fun
138     ///  注册命令的函数 需要实现要注册到平台的指令
139     ///  保存STT平台传来的函数和结构信息供该类以后调用
140     ///
141     /// param rf
142     /// 注册指令的平台回调函数指针
143     /// 如果模块有自定义数据导入,需要向test_info中添加数据
144     ///
145     virtual void reg_fun(int test_id,QScriptEngine *eng ) const = 0;
146 
147 
148     virtual void reg_ui_fun(REG_UI_FUN reg_ui_f)const = 0;
149 
150     ///
151     /// rief get_moudle_version
152     /// 获取模块的版本信息
153     /// 
eturn
154     ///
155     virtual  std::string get_moudle_version() const =0;
156 
157     ///
158     /// rief get_moudle_name
159     /// 
eturn
160     ///
161     virtual  std::string get_moudle_name() const =0;
162 
163     ///
164     /// rief get_moudle_describe
165     /// 获取模块的描述信息
166     /// 
eturn
167     ///
168     virtual  std::string get_moudle_describe() const =0;
169 
170     
171 };
172 
173 
174 QT_BEGIN_NAMESPACE
175 
176 #define IBasicModule_iid "com.twsz.tc.ningjian.IBasicModule/1.0"
177 
178 Q_DECLARE_INTERFACE(IBasicModule, IBasicModule_iid)
179 
180 QT_END_NAMESPACE
181 
182 
183 //Q_DECLARE_INTERFACE(IBasicModule,"com.twsz.tc.ningjian.IBasicModule/1.0");
184 
185 #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
 
 1 /******************************************************************************************************
 2 * Copyright (C) 2014, All right reserved.
 3 
 4 * file
 5 * version  1.0
 6 * author   NingJian (freegodly@gmail.com)
 7 * brief
 8 
 9 * detail
10 
11 * TODO
12 * history  2014-9-17 created by NingJian
13 *
14 * note
15 ******************************************************************************************************/
16 #ifndef STT_BASIC_MOUDLE_H
17 #define STT_BASIC_MOUDLE_H
18 
19 
20 #include <Basic_Module_Interface.h>
21 #include <iostream>
22 #include <QScriptEngine>
23 #include <QScriptValue>
24 #include <QtCore/QtPlugin>
25 
26 #include <iostream>
27 using namespace std;
28 
29 class  STT_Basic_Moudle:public QObject,public IBasicModule
30 {
31     Q_OBJECT
32     Q_PLUGIN_METADATA(IID "com.twsz.tc.ningjian.IBasicModule/1.0" )
33     Q_INTERFACES(IBasicModule)
34 
35 
36 public:
37     STT_Basic_Moudle();
38     // IBasicModule interface
39 public:
40     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;
41     bool release() const;
42     bool initiation(int test_id) const;
43     bool finish(int test_id) const;
44 
45     std::string get_moudle_version() const;
46     std::string get_moudle_describe() const;
47     void reg_fun(int test_id, QScriptEngine *eng) const;
48     void reg_ui_fun(REG_UI_FUN reg_ui_f) const;
49     std::string get_moudle_name() const;
50 
51 
52 
53 public:
54     ///
55     /// rief G_Test_Info
56     ///
57     static std::map<int,TEST_INFO> *STT_G_Test_Info;
58 
59     ///
60     /// rief G_Test_Fun_Info
61     ///
62     static std::map<std::string,TEST_FUN_INFO>  *STT_G_Test_Fun_Info;
63 
64 
65     ///
66     /// rief G_Test_Run_Fun
67     ///
68     static RUN_FUN STT_G_Test_Run_Fun;
69 
70     ///
71     /// rief STT_G_Moudles_Config
72     ///
73     static std::map<std::string,std::string>  *STT_G_Moudles_Config;
74 
75 
76 };
77 
78 
79 std::string get_stt_variable(int test_id,std::string key);
80 void set_stt_variable(int test_id,std::string key,std::string value);
81 void add_fun(const char * moudle_name,const char * fun_name ,const char * fun_describe);
82 
83 
84 #endif // STT_BASIC_MOUDLE_H
View Code
3、使用插件

下面是遍历加载plugins目录下所有实现 IBasicModule 接口的插件 获取相应的实例就可以调用了
头文件记得添加
#include <QPluginLoader>
 1     //注册模块指令
 2     QDir plugindir = QDir(QDir::currentPath()+"/plugins");
 3     int i = 0;
 4     foreach(QString filename,plugindir.entryList(QDir::Files)){
 5         QPluginLoader loader(plugindir.absoluteFilePath(filename));
 6         if (IBasicModule * base_moudle = qobject_cast<IBasicModule *>(loader.instance()))
 7         {
 8             qDebug()<<base_moudle->get_moudle_name().c_str();
 9 
10             STT_Global::basicModule_map.insert(std::pair<std::string,IBasicModule*>(base_moudle->get_moudle_name(),base_moudle));
11             STT_Global::PlugsList.push_back( base_moudle->get_moudle_name());
12             base_moudle->init(G_Test_Info,G_Test_Fun_Info,G_Moudles_Config,G_STT_Run_Fun);
13             base_moudle->reg_fun(-1,G_STT_Interpreter[-1]);
14             base_moudle->reg_ui_fun(G_Reg_UI_FUN);
15             i++;
16             emit STT_Global::fl->signal_process(20+ 80 * i / plugindir.entryList(QDir::Files).size());
17         }
18     }




原文地址:https://www.cnblogs.com/freegodly/p/4250775.html