Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Handling Gestures in Qt

From Wiki
Jump to: navigation, search
Article Metadata
Code ExampleTested with
Devices(s): N8
Platform(s): Symbian
Keywords: QGesture
Created: somnathbanik (16 May 2015)
Last edited: hamishwillee (11 Oct 2012)



This article demonstrates how to handle Gestures in Qt. Qt provides a gesture framework that handles user panning, pinching, and swiping. It allows the user to extend the gesture recognizer and to interact and handle application gestures of there own. The framework uses the QGesture class.To indicate the widget about the incoming gestures, it must invoke grabGesture, passing the gesture ID of the gesture it can handle.

Basic Idea

We will create an example that shows how to use the Pinch Gesture (and other gestures)


Note.pngNote: The screen shot is not that much explanatory, please see the source code for better understanding

Class Implementation

GesturesWidget::GesturesWidget(QWidget *parent)
: QWidget(parent)

QWidget doesn’t define an explicit event handler for gestures, so we need to catch these gestures in QWidget’s event method:

bool GesturesWidget::event(QEvent *event)
if (event->type() == QEvent::Gesture)
return gestureEvent(static_cast<QGestureEvent*>(event));
return QWidget::event(event);

The gestureEvent method is not an in-build method of QWidget class, we implemented this method to handle gestures recognition and actions.

bool GesturesWidget::gestureEvent(QGestureEvent *event)
if (QGesture *pan = event->gesture(Qt::PanGesture))
panTriggered(static_cast<QPanGesture *>(pan));
if (QGesture *pinch = event->gesture(Qt::PinchGesture))
pinchTriggered(static_cast<QPinchGesture *>(pinch));
return true;

Each of the individual gesture handlers called form gestureEvent do the actual gesture handling and then we perform the widget-specific processing, such as panning, rotating etc. For example in pinching the user may adjust the distance between the two touched points repeatedly to see the same content at different zoom levels. For more information on writing a gesture recognizer see Gestures Programming

Source Code

The full source code presented in this article is available here

Related Articles

--somnathbanik 16:24, 16 May 2011 (EEST)

This page was last modified on 11 October 2012, at 01:17.
90 page views in the last 30 days.