CTK
是一个成熟的插件化框架,在使用前需要链接 CTK
的两个库 Core 和 PluginFramwork
,本文将针对 CTK
框架的通信机制进行介绍。
//事件管理服务
ctkServiceReference eventAdminRef = context->getServiceReference<ctkEventAdmin>();
m_eventAdmin = qobject_cast<ctkEventAdmin*>(context->getService(eventAdminRef));
if(!m_eventAdmin){
qDebug() << "事件管理服务获取失败!";
}
//消息订阅
ctkDictionary props;
props.insert(ctkEventConstants::EVENT_TOPIC, "cn/qtio/eventAdmin/subscriber/handleEvent");
context->registerService<ctkEventHandler>(this, props);
ctkServiceReference ref = context->getServiceReference<ctkEventAdmin>();
ctkEventAdmin *eventAdmin = qobject_cast<ctkEventAdmin*>(context->getService(ref));
ctkProperties props;
props.insert("type", "消息类型");
ctkEvent event("cn/qtio/eventAdmin/subscriber/handleEvent", props);
if (eventAdmin) {
//sendEvent:同步通信
eventAdmin->sendEvent(event);
}
在框架内部,提供一个专门用于事件驱动的模块,各个插件可以通过发送事件的方式将内部数据发送出去。
//插件管理器
ctkServiceReference pluginAdminRef = context->getServiceReference<PluginAdmin>();
m_pluginAdmin = qobject_cast<PluginAdmin *>(context->getService(pluginAdminRef));
if(!m_pluginAdmin){
qDebug() << "获取插件管理器失败!";
}
//消息订阅
ctkDictionary props;
props.insert(ctkEventConstants::EVENT_TOPIC, "cn/qtio/eventAdmin/MainWindow/handleEvent");
context->registerService<ctkEventHandler>(this, props);
//注册异步信号
m_eventAdmin->publishSignal(this, SIGNAL(queuedPublished(ctkDictionary)), "cn/qtio/eventAdmin/subscriber/handleEvent", Qt::QueuedConnection);
//注册同步信号
m_eventAdmin->publishSignal(this, SIGNAL(directPublished(ctkDictionary)), "cn/qtio/eventAdmin/subscriber/handleEvent", Qt::DirectConnection);