C
处理触摸输入
本主题介绍如何将系统输入与Qt Quick Ultralite集成。
概述
Qt Quick Ultralite 默认支持触摸事件和按键事件。这些事件由platforminterface/platforminterface.h 头文件中定义的以下函数进行处理。
- Qul::PlatformInterface::handleTouchEvent() - 将触摸事件发送至Qt Quick Ultralite引擎。
- Qul::PlatformInterface::handleTouchCancelEvent() - 取消触摸事件。
- Qul::PlatformInterface::handleKeyEvent() - 将按键事件发送至Qt Quick Ultralite 引擎。
注意: 直接调用 上述任何函数(例如在 ISR中调用 )可能会破坏Qt Quick Ultralite应用程序的状态。因此,应使用Qul::EventQueue 。您也可以使用自定义的队列实现,但在这种情况下,对上述任何函数的调用都必须在Qt Quick Ultralite上下文中进行,这意味着该调用最好从PlatformContext::exec 中发起。
对于单点触控系统,请使用Qul::Platform::SinglePointTouchEventDispatcher 辅助类。与Qul::PlatformInterface::handleTouchEvent() 相比,它提供了一种更便捷的方式将触摸事件发送至Qt Quick Ultralite 引擎。使用后者时,您需要自行处理多点触控相关事宜。
除了默认的触摸和键盘事件外,还可以使用Qul::EventQueue 类发送自定义事件。这需要在应用程序中实现一个自定义的C++到QML接口来接收自定义事件。
实现输入处理
通常,系统通过中断接收触摸和按键事件。要将系统输入与Qt Quick Ultralite集成,需设置中断服务程序(ISR)来处理从系统输入设备接收的中断。随后,ISR会解析输入数据,将其转换为适当格式,并发送至Qt Quick Ultralite引擎进行进一步处理。
使用调度器发送触摸事件
以下是一个使用上述Qul::Platform::SinglePointTouchEventDispatcher 辅助类的单点触摸系统示例。为简明起见,此处未展示平台特定的中断配置以及获取触摸事件数据的函数。
首先,创建一个类,其中包含用于单点触摸事件的Qul::EventQueue ,并将Qul::Platform::SinglePointTouchEventDispatcher 作为私有成员。事件由重写的Qul::EventQueue::onEvent()方法分发。此外,还创建了一个事件队列的静态实例。
class SinglePointTouchEventQueue : public Qul::EventQueue<Qul::Platform::SinglePointTouchEvent> { public: void onEvent(const Qul::Platform::SinglePointTouchEvent &event) QUL_DECL_OVERRIDE { touchEventDispatcher.dispatch(event); } void onQueueOverrun() QUL_DECL_OVERRIDE { clearOverrun(); PlatformInterface::log("Touch event discarded/overwritten. Consider increasing the queue size.\n"); } private: Qul::Platform::SinglePointTouchEventDispatcher touchEventDispatcher; }; static SinglePointTouchEventQueue touchEventQueue;
实现touchISR() 函数,用于处理来自触摸控制器的中断并将事件发布到队列中。
void touchISR() { static Qul::Platform::SinglePointTouchEvent event; // Here would be platform specific code to fetch touch data and store // it into the x, y and pressed members of the the event. event.x = 0; event.y = 0; event.pressed = false; event.timestamp = Qul::Platform::getPlatformInstance()->currentTimestamp(); touchEventQueue.postEventFromInterrupt(event); }
注意:请使用 PlatformContext::currentTimestamp 确保输入事件的时间戳与Qt Quick Ultralite 引擎其余部分使用同一时钟。
构建并烧录到设备后,触摸屏幕时背景颜色应发生变化。
至此,您已完成移植指南的基本阶段,可以提交您的状态。接下来,请返回简介页面,如果您的设备支持,请继续实现推荐的硬件加速功能。
在某些 Qt 许可证下可用。
了解更多信息。