Files:
This example shows how to enable gestures for a widget and use gesture input to perform actions.
We use two classes to create the user interface for the application: MainWidget and ImageWidget. The MainWidget class is simply used as a container for the ImageWidget class, which we will configure to accept gesture input. Since we are interested in the way gestures are used, we will concentrate on the implementation of the ImageWidget class.
The ImageWidget class is a simple QWidget subclass that reimplements the general QWidget::event() handler function in addition to several more specific event handlers:
class ImageWidget : public QWidget { Q_OBJECT public: ImageWidget(QWidget *parent = 0); void openDirectory(const QString &path); protected: bool event(QEvent *event); void paintEvent(QPaintEvent *event); void resizeEvent(QResizeEvent *event); void mouseDoubleClickEvent(QMouseEvent *event); private: bool gestureEvent(QGestureEvent *event); void panTriggered(QPanGesture*); void pinchTriggered(QPinchGesture*); void swipeTriggered(QSwipeGesture*); ... };
We also implement a private helper function, gestureEvent(), to help manage gesture events delivered to the widget, and three functions to perform actions based on gestures: panTriggered(), pinchTriggered() and swipeTriggered().
In the widget's constructor, we begin by setting up various parameters that will be used to control the way images are displayed.
ImageWidget::ImageWidget(QWidget *parent) : QWidget(parent), position(0), horizontalOffset(0), verticalOffset(0), rotationAngle(0), scaleFactor(1), currentStepScaleFactor(1) { setMinimumSize(QSize(100,100)); grabGesture(Qt::PanGesture); grabGesture(Qt::PinchGesture); grabGesture(Qt::SwipeGesture); }
We enable three of the standard gestures for the widget by calling QWidget::grabGesture() with the types of gesture we need. These will be recognized by the application's default gesture recognizer, and events will be delivered to our widget.
Since QWidget does not define a specific event handler for gestures, the widget needs to reimplement the general QWidget::event() to receive gesture events.
bool ImageWidget::event(QEvent *event) { if (event->type() == QEvent::Gesture) return gestureEvent(static_cast<QGestureEvent*>(event)); return QWidget::event(event); }
We implement the event handler to delegate gesture events to a private function specifically written for the task, and pass all other events to QWidget's implementation.
The gestureHandler() function examines the gestures supplied by the newly-delivered QGestureEvent. Since only one gesture of a given type can be used on a widget at any particular time, we can check for each gesture type using the QGestureEvent::gesture() function:
bool ImageWidget::gestureEvent(QGestureEvent *event) { if (QGesture *swipe = event->gesture(Qt::SwipeGesture)) swipeTriggered(static_cast<QSwipeGesture *>(swipe)); else 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; }
If a QGesture object is supplied for a certain type of gesture, we call a special purpose function to deal with it, casting the gesture object to the appropriate QGesture subclass.
To illustrate how a standard gesture can be interpreted by an application, we show the implementation of the swipeTriggered() function, which handles the gesture associated with a brushing or swiping motion on the user's display or input device:
void ImageWidget::swipeTriggered(QSwipeGesture *gesture) { if (gesture->state() == Qt::GestureFinished) { if (gesture->horizontalDirection() == QSwipeGesture::Left || gesture->verticalDirection() == QSwipeGesture::Up) goPrevImage(); else goNextImage(); update(); } }
The QSwipeGesture class provides specialized functions and defines a enum to make it more convenient for developers to discover which direction, if any, the user swiped the display. Here, we simply navigate to the previous image in the collection if the user swiped upwards or to the left; otherwise we navigate to the next image in the collection.
The other gestures are also handled by special purpose functions, but use the values of properties held by the QGesture object passed to them.
© 2008-2011 Nokia Corporation and/or its subsidiaries. Nokia, Qt and their respective logos are trademarks of Nokia Corporation in Finland and/or other countries worldwide.
All other trademarks are property of their respective owners. Privacy Policy
Licensees holding valid Qt Commercial licenses may use this document in accordance with the Qt Commercial License Agreement provided with the Software or, alternatively, in accordance with the terms contained in a written agreement between you and Nokia.
Alternatively, this document may be used under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation.