CTK |
您所在的位置:网站首页 › 测试插件框架32位 › CTK |
一、项目介绍 1、主要为了在此小型测试项目中用到几乎所有ctk的常用知识: 插件间通信、服务工厂、服务追踪、事件监听、框架封装 2、项目共包含3个插件日志插件:用于模拟将接收的信息存储到日志中【这里只做简单的打印】,并且能够接收信息【主要来自界面插件】 打印插件:根据不同的插件返回提供不同打印服务 界面插件:封装一个界面插件,能够向框架发送信息【类方式和信号槽方式】 3、架构图
二、打印插件编写 1、工程结构
2、接口类 absprintserver.h #ifndef ABSPRINTSERVER_H #define ABSPRINTSERVER_H #include class AbsPrintServer { public: virtual ~AbsPrintServer(){} virtual void print(QString) = 0; }; Q_DECLARE_INTERFACE(AbsPrintServer,"judesmorning.zxy.AbsPrintServer") #endif // ABSPRINTSERVER_H3、实现类1,为日志插件提供服务 printserver.h #ifndef PRINTSERVER_H #define PRINTSERVER_H #include #include "absprintserver.h" class PrintServer : public QObject, public AbsPrintServer { Q_OBJECT Q_INTERFACES(AbsPrintServer) public: PrintServer(); void print(QString info) override; }; #endif // PRINTSERVER_Hprintserver.cpp #include "printserver.h" #include PrintServer::PrintServer() { } void PrintServer::print(QString info) { qDebug()open(); // AbsPrintServer* printService = static_cast(p_tracker->getService()); // printService->print("use tracker to get print plugin--------log plugin"); #endif #if 1 //监听ctk事件 context->connectFrameworkListener(this, SLOT(onFrameworkEvent(ctkPluginFrameworkEvent))); context->connectPluginListener(this, SLOT(onPluginEvent(ctkPluginEvent))); //QString filter = QString("(%1=%2)").arg(ctkPluginConstants::OBJECTCLASS).arg("org.commontk.eventadmin");// 过滤 ctkEventAdmin 服务 //context->connectServiceListener(this,SLOT(onServiceEvent(ctkServiceEvent))); //, filter); context->connectServiceListener(this,"onServiceEvent"); //, filter); #endif } void LogService::log(QString info) { qDebug()getServiceReference(); if (reference) { // 获取指定 ctkServiceReference 引用的服务对象 AbsPrintServer* service = qobject_cast(context->getService(reference)); if (service != Q_NULLPTR) { // 调用服务 service->print(info); } } #endif #if 1 //通过tracker向ctk索要服务 AbsPrintServer* service = static_cast(p_tracker->getService()); if (service != Q_NULLPTR) { service->print("log plugin with tracker:"+info); } else { qDebug()registerService(service, props); context->registerService(service); } void FirstPluginActivator::stop(ctkPluginContext *context) { Q_UNUSED(context) }5、插件间通信的结构体定义 includes.h #ifndef INCLUDES_H #define INCLUDES_H #include /*公共定义*/ //日志相关 enum _Log_Level{ LOG_LEVEL_DEBUG = 0, LOG_LEVEL_INFO, LOG_LEVEL_WARNING, LOG_LEVEL_ERROR, LOG_LEVEL_CRITICAL };//日志等级 typedef struct _LogInfo { _Log_Level level; //日志类型 QString pluginName; //插件名称 QString info; //日志信息 } LogInfo;//日志详情 #endif // INCLUDES_H
四、界面类 1、工程结构
2、接口类 absuiservice.h #ifndef ABSUISERVICE_H #define ABSUISERVICE_H #include class AbsUiService{ public: virtual ~AbsUiService(){} virtual void init() = 0; }; Q_DECLARE_INTERFACE(AbsUiService,"judesmorning.zxy.AbsUiService") #endif // ABSUISERVICE_H
3、实现类 uiservice.h #ifndef UISERVICE_H #define UISERVICE_H #include #include "absuiservice.h" #include "includes.h" #include "myui.h" class ctkPluginContext; class UiService : public QObject, public AbsUiService { Q_OBJECT Q_INTERFACES(AbsUiService) public: UiService(ctkPluginContext* context); void init() override; private slots: void publishLogEventSlot(LogInfo logInfo); private: ctkPluginContext* context;//ctk上下文对象 Myui myui;//界面对象 }; #endif // UISERVICE_Huiservice.cpp #include "uiservice.h" #include "ctkPluginContext.h" #include "service/event/ctkEventAdmin.h" #include #include UiService::UiService(ctkPluginContext* context) :context(context) { context->registerService(this); QObject::connect(&myui,SIGNAL(publishLogEventSignal(LogInfo)),this,SLOT(publishLogEventSlot(LogInfo))); //发送日志事件到ctk框架,signal方式 ctkServiceReference ref = context->getServiceReference(); if (ref) { ctkEventAdmin* eventAdmin = context->getService(ref); eventAdmin->publishSignal(&myui,SIGNAL(publishLogSignal(ctkDictionary)),"kdhy/yunwei/generalevent/log",Qt::QueuedConnection); } } void UiService::init() { myui.show(); } //发送日志事件到ctk框架,event方式 void UiService::publishLogEventSlot(LogInfo logInfo) { ctkServiceReference ref = context->getServiceReference(); if (ref) { ctkEventAdmin* eventAdmin = context->getService(ref); ctkDictionary props; props["level"] = logInfo.level; props["pluginName"] = logInfo.pluginName; props["info"] = logInfo.info; ctkEvent event("kdhy/yunwei/generalevent/log", props); qDebug() setupUi(this); } Myui::~Myui() { delete ui; } //发送event事件 void Myui::on_pushButton_clicked() { qDebug()textEdit->toPlainText(); emit publishLogEventSignal(logInfo); } //发送信号事件 void Myui::on_pushButton_2_clicked() { ctkDictionary props; props["level"] = LOG_LEVEL_INFO;; props["pluginName"] = "CreateCtkUiPlugin"; props["info"] = "Signal info:"+ui->textEdit->toPlainText(); emit publishLogSignal(props); }myui.ui
5、激活类 uiactivator.h #ifndef UIACTIVATOR_H #define UIACTIVATOR_H #include #include "ctkPluginActivator.h" #include "ctkPluginContext.h" #include "uiservice.h" class UiActivator: public QObject, public ctkPluginActivator { Q_OBJECT Q_INTERFACES(ctkPluginActivator) Q_PLUGIN_METADATA(IID "UiPlugin") public: UiActivator(); void start(ctkPluginContext *context); void stop(ctkPluginContext *context); private: QScopedPointer m_ui; }; #endif // UIACTIVATOR_Huiactivator.cpp #include "uiactivator.h" #include UiActivator::UiActivator() { } void UiActivator::start(ctkPluginContext *context) { // qDebug() usingEventAdmin = usingEventAdmin; if(usingEventAdmin) { QString path = QDir::currentPath() + "/third_libs/ctk/libs"; // 获取插件所在位置 ctkPluginFrameworkLauncher::addSearchPath(path); // 在插件的搜索路径列表中添加一条路径 ctkPluginFrameworkLauncher::start("org.commontk.eventadmin"); context = ctkPluginFrameworkLauncher::getPluginContext(); } else { QSharedPointer framework = frameworkFactory.getFramework(); // 初始化并启动插件框架 try { framework->init(); framework->start(); context = framework->getPluginContext(); } catch (const ctkPluginException &e) { this->print("CTK plugin framework init failed:"+QString(e.what())); } } } /*****************************************************************/ //作者:朱小勇 //函数名称:初始化服务 //函数参数:NULL //函数返回值:NULL //函数作用:NULL //备注:安装启动插件 /*****************************************************************/ void PullService::initServices() { RET_IF_EAQU(context,nullptr); QString prefixFilePath = QDir::currentPath()+"/third_libs/plugin/libs/"; QString path; foreach (QString oneFileName , pluginNames) { path = prefixFilePath+oneFileName; try { // 安装插件 print(QString("Ready to init plugin:%1").arg(oneFileName)); QSharedPointer plugin = context->installPlugin(QUrl::fromLocalFile(path)); print(QString("Plugin[%1_%2] installed...").arg(plugin->getSymbolicName()).arg(plugin->getVersion().toString())); // 启动插件 plugin->start(ctkPlugin::START_TRANSIENT); print(QString("Plugin[%1_%2] started...").arg(plugin->getSymbolicName()).arg(plugin->getVersion().toString())); } catch (const ctkPluginException &e) { print(QString("Failed install or start plugin:%1").arg(e.what())); } } } /*****************************************************************/ //作者:朱小勇 //函数名称:停止框架 //函数参数:NULL //函数返回值:NULL //函数作用:NULL //备注:NULL /*****************************************************************/ void PullService::stopFramework() { if(usingEventAdmin) { ctkPluginFrameworkLauncher::stop(); } else { QSharedPointer framework = frameworkFactory.getFramework(); framework->stop(); } } /*****************************************************************/ //作者:朱小勇 //函数名称:内部打印函数 //函数参数:NULL //函数返回值:NULL //函数作用:NULL //备注:NULL /*****************************************************************/ void PullService::print(QString info) { QString _i = QString("%1 %2 %3") .arg(__FILE__) .arg(QTime::currentTime().toString("HH:mm:ss")) .arg(info); qDebug()initCtkFramework(false); (void)PullService::getInstance()->initServices(); return a.exec(); }4、运行结果
长风破浪会有时,直挂云帆济沧海! 可通过下方链接找到博主 https://www.cnblogs.com/judes/p/10875138.html |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |