×
Namespaces

Variants
Actions
Revision as of 04:17, 11 October 2012 by hamishwillee (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Handling Gestures in Qt

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


Contents

Overview

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)

GesturesQt.png

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)
{
grabGesture(Qt::PanGesture);
grabGesture(Qt::PinchGesture);
grabGesture(Qt::SwipeGesture);
}

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 File:GesturesQt.zip

Related Articles


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

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