事件系统
客户端插件通过 xplugin::xPluginEvent() 使用统一事件总线(XPluginEventSystem),与 InterfaceManager::sigNetworkConnect 等信号互补:适合跨模块、自定义类型、过滤与一次性订阅。
订阅示例
- 订阅方法
QString subId = xplugin::xPluginEvent().subscribe(
xplugin::XPluginEventType::RCPowerStatusChange,
[](const xplugin::XPluginEvent& e) {
// e.data(), e.source()
},
PLUGIN_NAME,
this // context 销毁时自动取消
);
- 常用内置类型(见 xpluginevent.h):
| 类型 | 值 | 含义 |
|---|---|---|
| NetworkConnected | 0x2001 | 网络已连接 |
| NetworkDisconnected | 0x2002 | 网络断开 |
| XServiceDataChange | 0x3001 | xservice 节点变化 |
| RCPowerStatusChange | 0x4001 | 上电状态变化 |
| RCRunModelChange | 0x4011 | 手/自动切换 |
| RCRunStart / RCRunStop | 0x4021 / 0x4022 | 程序运行启停 |
| RCBeforeRunStart / RCBeforeRunStop | 0x4023 / 0x4024 | 运行前/停止前钩子 |
- 用户自定义事件从 UserEventStart(0x8000) 起,可用registerCustomEventType("MyEvent") 分配类型。
发布
xplugin::xPluginEvent().publish(
xplugin::XPluginEventType::UserEventStart + 1,
QVariant("payload"),
PLUGIN_NAME);
取消订阅
| 方法 | 用途 |
|---|---|
| unsubscribe(subscriptionId) | 按 ID |
| unsubscribeByPlugin(PLUGIN_NAME) | 插件全部 |
| unsubscribeByType(type) | 某类型全部 |
| unsubscribeByTypeAndPlugin(PLUGIN_NAME, type) | 插件 + 类型 |
- 插件 detach() 时建议取消本插件全部订阅,避免悬空回调。