文章目录
- 一、前言
- 二、框架事件
- 三、插件事件
- 四、服务事件
- 五、添加事件监听
一、前言
CTK一共有三种事件可以监听:
- 框架事件
- 插件事件
- 服务事件
但是这些事件只有在变化时才能监听到,如果已经变化过后,进入一个稳定的状态,这时才去监听,那么是无法监听到的。
二、框架事件
针对整个框架的,相当于只有一个,因为框架只有一个,但是这里有个问题,就是监听这个事件是在框架初始化之后的,所以根本没法监听到框架事件的初始化,只能监听到结束的事件。类型有:
事件 | 说明 |
---|---|
FRAMEWORK_STARTED | |
PLUGIN_ERROR | |
PLUGIN_WARNING | |
PLUGIN_INFO | |
FRAMEWORK_STOPPED | |
FRAMEWORK_STOPPED_UPDATE | |
FRAMEWORK_WAIT_TIMEDOUT |
三、插件事件
在安装、启动插件的过程中呈现的,主要就是插件的一个状态的变化。类型有:
事件 | 说明 |
---|---|
INSTALLED | |
RESOLVED | |
LAZY_ACTIVATION | |
STARTING | |
STARTED | |
STOPPING | |
STOPPED | |
UPDATED | |
UNRESOLVED | |
UNINSTALLED |
四、服务事件
在创建、回收插件时的事情,主要体现在服务的注册和注销。类型有:
事件 | 说明 |
---|---|
REGISTERED | |
MODIFIED | |
MODIFIED_ENDMATCH | |
UNREGISTERING |
五、添加事件监听
子项目【Application】中新增类EventListener
#ifndef EVENT_LISTENER_H
#define EVENT_LISTENER_H
#include <QObject>
#include <ctkPluginFrameworkEvent.h>
#include <ctkPluginEvent.h>
#include <ctkServiceEvent.h>
class EventListener : public QObject
{
Q_OBJECT
public:
explicit EventListener(QObject *parent = Q_NULLPTR);
~EventListener();
public slots:
// 监听框架事件
void onFrameworkEvent(const ctkPluginFrameworkEvent& event);
// 监听插件事件
void onPluginEvent(const ctkPluginEvent& event);
// 监听服务事件
void onServiceEvent(const ctkServiceEvent& event);
};
#endif // EVENT_LISTENER_H
#include "event_listener.h"
EventListener::EventListener(QObject *parent)
: QObject(parent)
{
}
EventListener::~EventListener()
{
}
// 监听框架事件
void EventListener::onFrameworkEvent(const ctkPluginFrameworkEvent& event)
{
if (!event.isNull()) {
QSharedPointer<ctkPlugin> plugin = event.getPlugin();
qDebug() << "FrameworkEvent: [" << plugin->getSymbolicName() << "]" << event.getType() << event.getErrorString();
} else {
qDebug() << "The framework event is null";
}
}
// 监听插件事件
void EventListener::onPluginEvent(const ctkPluginEvent& event)
{
if (!event.isNull()) {
QSharedPointer<ctkPlugin> plugin = event.getPlugin();
qDebug() << "PluginEvent: [" << plugin->getSymbolicName() << "]" << event.getType();
} else {
qDebug() << "The plugin event is null";
}
}
// 监听服务事件
void EventListener::onServiceEvent(const ctkServiceEvent &event)
{
if (!event.isNull()) {
ctkServiceReference ref = event.getServiceReference();
QSharedPointer<ctkPlugin> plugin = ref.getPlugin();
qDebug() << "ServiceEvent: [" << event.getType() << "]" << plugin->getSymbolicName() << ref.getUsingPlugins();
} else {
qDebug() << "The service event is null";
}
}
【main.cpp】中调用事件监听类
#include "mainwindow.h"
#include <QApplication>
#include "ctkPluginFrameworkFactory.h"
#include "ctkPluginFramework.h"
#include "ctkPluginException.h"
#include "ctkPluginContext.h"
#include "ctkPluginFrameworkLauncher.h"
#include <QDebug>
#include "../HelloCTK/HelloService.h"
#include "event_listener.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setApplicationName("CTK_PluginFramework");//给框架创建名称,Linux下没有会报错
// 获取插件所在位置
// 在插件的搜索路径列表中添加一条路径
ctkPluginFrameworkLauncher::addSearchPath("../CTK/lib/ctk-0.1/plugins");
ctkPluginFrameworkLauncher::start("org.commontk.eventadmin");
ctkPluginFrameworkFactory frameworkFactory;
QSharedPointer<ctkPluginFramework> framework = frameworkFactory.getFramework();
// 初始化并启动插件框架
try {
framework->init();
framework->start();
qDebug() << "======================================";
qDebug() << "CTK plugin framework start...";
qDebug() << "======================================";
} catch (const ctkPluginException &e) {
qDebug() << "CTK plugin framework init err: " << e.what();
return -1;
}
// 获取插件服务的contex
ctkPluginContext* pluginContext = framework->getPluginContext();
// 事件监听
EventListener listener;
pluginContext->connectFrameworkListener(&listener, SLOT(onFrameworkEvent(ctkPluginFrameworkEvent)));
pluginContext->connectPluginListener(&listener, SLOT(onPluginEvent(ctkPluginEvent)));
// 过滤 ctkEventAdmin 服务
// QString filter = QString("(%1=%2)").arg(ctkPluginConstants::OBJECTCLASS).arg("org.commontk.eventadmin");
pluginContext->connectServiceListener(&listener, "onServiceEvent"); //, filter);
//---------------------------------------------------------------------------------------------------------------------------------------
//注册事件调用
try {
// 安装插件
QString HelloCTK_dir = "C:/Qt_Pro/build-CTK_PluginFramework-CMake-Debug/HelloCTK/bin/plugins/HelloCTK.dll";
QSharedPointer<ctkPlugin> plugin = pluginContext->installPlugin(QUrl::fromLocalFile(HelloCTK_dir));
qDebug() << QString("Plugin[%1_%2] installed...").arg(plugin->getSymbolicName()).arg(plugin->getVersion().toString());
// 启动插件
plugin->start(ctkPlugin::START_TRANSIENT);
qDebug() << QString("Plugin[%1_%2] started").arg(plugin->getSymbolicName()).arg(plugin->getVersion().toString());
//获取元数据
QHash<QString, QString> headers = plugin->getHeaders();
ctkVersion version = ctkVersion::parseVersion(headers.value(ctkPluginConstants::PLUGIN_VERSION));
QString name = headers.value(ctkPluginConstants::PLUGIN_SYMBOLICNAME);
QString number = headers.value("Plugin-Number");
qDebug()<<"version: "<<version;
qDebug()<<"name: "<<name;
qDebug()<<"number: "<<number;
} catch (const ctkPluginException &e) {
qDebug() << QString("Failed install or run plugin: ") << e.what();
return -2;
}
// 获取服务引用
ctkServiceReference reference = pluginContext->getServiceReference<HelloService>();
if (reference) {
// 获取指定 ctkServiceReference 引用的服务对象
//HelloService* service = qobject_cast<HelloService *>(pluginContext->getService(reference));
HelloService* service = pluginContext->getService<HelloService>(reference);
if (service != Q_NULLPTR) {
// 调用服务
service->sayHello();
}
}
//---------------------------------------------------------------------------------------------------------------------------------------
//类通信
try {
// 安装插件
QString BlogEventHandler_Path = "C:/Qt_Pro/build-CTK_PluginFramework-CMake-Debug/BlogEventHandler/bin/plugins/BlogEventHandler.dll";
QSharedPointer<ctkPlugin> plugin = pluginContext->installPlugin(QUrl::fromLocalFile(BlogEventHandler_Path));
qDebug() << QString("Plugin[%1_%2] installed...").arg(plugin->getSymbolicName()).arg(plugin->getVersion().toString());
// 启动插件
plugin->start(ctkPlugin::START_TRANSIENT);
qDebug() << QString("Plugin[%1_%2] started").arg(plugin->getSymbolicName()).arg(plugin->getVersion().toString());
} catch (const ctkPluginException &e) {
qDebug() << QString("Failed install or run plugin: ") << e.what();
return -2;
}
try {
// 安装插件
QString BlogManager_Path = "C:/Qt_Pro/build-CTK_PluginFramework-CMake-Debug/BlogManager/bin/plugins/BlogManager.dll";
QSharedPointer<ctkPlugin> plugin = pluginContext->installPlugin(QUrl::fromLocalFile(BlogManager_Path));
qDebug() << QString("Plugin[%1_%2] installed...").arg(plugin->getSymbolicName()).arg(plugin->getVersion().toString());
// 启动插件
plugin->start(ctkPlugin::START_TRANSIENT);
qDebug() << QString("Plugin[%1_%2] started").arg(plugin->getSymbolicName()).arg(plugin->getVersion().toString());
} catch (const ctkPluginException &e) {
qDebug() << QString("Failed install or run plugin: ") << e.what();
return -2;
}
//---------------------------------------------------------------------------------------------------------------------------------------
//信号槽通信-测试插件
try {
// 安装插件
QString BlogEventHandlerUsingSlot_Path = "C:/Qt_Pro/build-CTK_PluginFramework-CMake-Debug/BlogEventHandlerUsingSlot/bin/plugins/BlogEventHandlerUsingSlot.dll";
QSharedPointer<ctkPlugin> plugin = pluginContext->installPlugin(QUrl::fromLocalFile(BlogEventHandlerUsingSlot_Path));
qDebug() << QString("Plugin[%1_%2] installed...").arg(plugin->getSymbolicName()).arg(plugin->getVersion().toString());
// 启动插件
plugin->start(ctkPlugin::START_TRANSIENT);
qDebug() << QString("Plugin[%1_%2] started").arg(plugin->getSymbolicName()).arg(plugin->getVersion().toString());
} catch (const ctkPluginException &e) {
qDebug() << QString("Failed install or run plugin: ") << e.what();
return -2;
}
try {
// 安装插件
QString BlogManagerUsingSignal_Path = "C:/Qt_Pro/build-CTK_PluginFramework-CMake-Debug/BlogManagerUsingSignal/bin/plugins/BlogManagerUsingSignal.dll";
QSharedPointer<ctkPlugin> plugin = pluginContext->installPlugin(QUrl::fromLocalFile(BlogManagerUsingSignal_Path));
qDebug() << QString("Plugin[%1_%2] installed...").arg(plugin->getSymbolicName()).arg(plugin->getVersion().toString());
// 启动插件
plugin->start(ctkPlugin::START_TRANSIENT);
qDebug() << QString("Plugin[%1_%2] started").arg(plugin->getSymbolicName()).arg(plugin->getVersion().toString());
} catch (const ctkPluginException &e) {
qDebug() << QString("Failed install or run plugin: ") << e.what();
return -2;
}
//---------------------------------------------------------------------------------------------------------------------------------------
//CTK服务工厂
// try {
// // 安装插件
// QString Hello_Path = "C:/Qt_Pro/build-CTK_PluginFramework-CMake-Debug/Hello/bin/plugins/Hello.dll";
// QSharedPointer<ctkPlugin> plugin = pluginContext->installPlugin(QUrl::fromLocalFile(Hello_Path));
// qDebug() << QString("Plugin[%1_%2] installed...").arg(plugin->getSymbolicName()).arg(plugin->getVersion().toString());
// // 启动插件
// plugin->start(ctkPlugin::START_TRANSIENT);
// qDebug() << QString("Plugin[%1_%2] started").arg(plugin->getSymbolicName()).arg(plugin->getVersion().toString());
// } catch (const ctkPluginException &e) {
// qDebug() << QString("Failed install or run plugin: ") << e.what();
// return -2;
// }
// // 获取服务引用
// ctkServiceReference reference = pluginContext->getServiceReference<HelloService>();
// if (reference) {
// // 获取指定 ctkServiceReference 引用的服务对象
// //HelloService* service = qobject_cast<HelloService *>(pluginContext->getService(reference));
// HelloService* service = pluginContext->getService<HelloService>(reference);
// if (service != Q_NULLPTR) {
// // 调用服务
// service->sayHello();
// }
// }
// 停止插件
//ctkPluginFrameworkLauncher::stop();
//---------------------------------------------------------------------------------------------------------------------------------------
MainWindow w;
w.show();
return a.exec();
}