C
Handling touch input
This topic explains how to integrate system inputs with Qt Quick Ultralite.
Overview
Qt Quick Ultralite supports touch events and key events by default. These events are handled by the following functions defined in the platforminterface/platforminterface.h header file.
- Qul::PlatformInterface::handleTouchEvent() - Sends touch events to the Qt Quick Ultralite engine.
- Qul::PlatformInterface::handleTouchCancelEvent() - Cancles a touch event.
- Qul::PlatformInterface::handleKeyEvent() - Sends key events to the the Qt Quick Ultralite engine.
Note: Calling any of the above functions directly, from ISR for example, may corrupt the Qt Quick Ultralite application state. Instead, Qul::EventQueue should be used. You can also use your own queue implementation, but in that case the call to any of the above functions must be in Qt Quick Ultralite context, meaning that it should probably be called from PlatformContext::exec.
For single touch systems, use the Qul::Platform::SinglePointTouchEventDispatcher helper class. It offers a more convinient way to send touch events to the Qt Quick Ultralite engine, than Qul::PlatformInterface::handleTouchEvent(). With the latter one, you need to handle the multi-touch aspects yourself.
Besides the default touch and key events, it is also possible to send custom events using the Qul::EventQueue class. This requires a custom C++ to QML interface in the application to receive custom events.
Implementing input handling
Typically, the system receives touch and key events via interrupts. To integrate system inputs with Qt Quick Ultralite, set up ISRs to serve the interrupts received from input devices of the system. The ISR then interprets input data, converts it to appropriate format, and sends it to Qt Quick Ultralite engine for further processing.
Sending touch events using dispatcher
Here is an example for single touch system using the Qul::Platform::SinglePointTouchEventDispatcher helper class mentioned above. For clarity, the platform-specific interrupt configuration and functions to get touch event data are not shown here.
First, create a class with Qul::EventQueue for single point touch events and Qul::Platform::SinglePointTouchEventDispatcher as private member. Events are dispatched from reimplemented Qul::EventQueue::onEvent(). Also, a static instance of the event queue is created.
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;
Implement the touchISR()
function to handle interrupts from the touch controller and post events to the queue.
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); }
Note: Use PlatformContext::currentTimestamp to ensure that input event timestamps are from the same clock as the rest of the Qt Quick Ultralite engine.
After building and flashing to the device, the background color should change when touching the screen.
You have now completed the basic phases of the porting guide and can commit your state. From here go back to the introduction page and continue by implementing the recommended hardware accelerations if your device supports them.
Available under certain Qt licenses.
Find out more.