Image Gestures Example

Demonstrates the use of simple gestures in a widget.

This example shows how to enable gestures for a widget and use gesture input to perform actions.

../_images/imagegestures-example.jpg

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.

ImageWidget Class Definition

The ImageWidget class is a simple QWidget subclass that reimplements the general event() handler function in addition to several more specific event handlers:

class ImageWidget(QWidget):

    Q_OBJECT
# public
    ImageWidget(QWidget parent = None)
    def openDirectory(path):
    def grabGestures(gestures):
protected:
    event = bool(QEvent event)
    def paintEvent(event):
    def resizeEvent(event):
    def mouseDoubleClickEvent(event):
# private
    gestureEvent = bool(QGestureEvent event)
    def panTriggered(QPanGesture*):
    def pinchTriggered(QPinchGesture*):
    def 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().

ImageWidget Class Implementation

In the widget’s constructor, we begin by setting up various parameters that will be used to control the way images are displayed.

def __init__(self, parent):
    QWidget.__init__(self, parent)
    self.position = 0
    self.horizontalOffset = 0
    self.verticalOffset = 0
    , rotationAngle(0), scaleFactor(1), currentStepScaleFactor(1)

    setMinimumSize(QSize(100, 100))

We enable three of the standard gestures for the widget by calling 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 event() to receive gesture events.

def event(self, QEvent event):

    if (event.type() == QEvent.Gesture)
        def gestureEvent(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 gesture() function:

def gestureEvent(self, QGestureEvent event):

    qCDebug(lcExample) << "gestureEvent():" << event
    if (QGesture swipe = event.gesture(Qt.SwipeGesture))
        swipeTriggered(QSwipeGesture (swipe))
    elif QGesture pan = event.gesture(Qt.PanGesture):
        panTriggered(QPanGesture (pan))
    if (QGesture pinch = event.gesture(Qt.PinchGesture))
        pinchTriggered(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 pinchTriggered() function, which handles the pinch gesture when the user moves two fingers around on the display or input device:

def pinchTriggered(self, gesture):

    QPinchGesture.ChangeFlags changeFlags = gesture.changeFlags()
    if (changeFlags  QPinchGesture.RotationAngleChanged) {
        rotationDelta = gesture.rotationAngle() - gesture.lastRotationAngle()
        rotationAngle += rotationDelta
        qCDebug(lcExample) << "pinchTriggered(): rotate by" <<
            rotationDelta << "." << rotationAngle

    if (changeFlags  QPinchGesture.ScaleFactorChanged) {
        currentStepScaleFactor = gesture.totalScaleFactor()
        qCDebug(lcExample) << "pinchTriggered(): zoom by" <<
            gesture.scaleFactor() << "." << currentStepScaleFactor

    if (gesture.state() == Qt.GestureFinished) {
         = currentStepScaleFactor
        currentStepScaleFactor = 1

    update()

The QPinchGesture class provides properties to interpret the changing distance between the two touch points as a zoom factor, and the angle delta as a rotation to be applied to the image. The center point between the touch points could be used to drag the image, but in this example we use the pan gesture for that purpose.

The scaleFactor() is a relative value representing how much the zoom should change from one event to the next, whereas totalScaleFactor() provides the amount of zoom that has been expressed since the gesture began. When the touch points are released and another gesture begins, totalScaleFactor() will begin again at 1.0. In this case we store totalScaleFactor() into the currentStepScaleFactor variable so that it can be used in paintEvent() to scale the image. Alternatively it would be possible to simply multiply the stored total scale factor by scaleFactor() here in the pinch handler.

In contrast, rotationAngle() represents the amount of rotation since the pinch gesture began, while lastRotationAngle() provides the previous value. So it is necessary to subtract in order to get an incremental delta. When the user begins a new pinch gesture, rotationAngle() will start from zero, and we want the image to begin to rotate from its current angle. This is achieved by adding the delta to the stored rotationAngle (which will be applied in paintEvent()). If we simply assigned totalRotationAngle() to the stored rotationAngle, a new gesture would cause the image to reset to a right-side-up orientation before beginning to rotate again. But it would be possible to store the rotation angle since the gesture began and add it to rotationAngle in paintEvent(), just as we store the amount of zoom since the gesture began.

The pan and swipe gestures in this example are also handled in separate functions, and use the values of properties from the QGesture objects passed to them.

def paintEvent(self, QPaintEvent*):

    p = QPainter(self)
    iw = currentImage.width()
    ih = currentImage.height()
    wh = height()
    ww = width()
    p.translate(ww / 2, wh / 2)
    p.translate(horizontalOffset, verticalOffset)
    p.rotate(rotationAngle)
    p.scale(currentStepScaleFactor * scaleFactor, currentStepScaleFactor * scaleFactor)
    p.translate(-iw / 2, -ih / 2)
    p.drawImage(0, 0, currentImage)

In paintEvent(), scaleFactor represents the zoom level before the pinch gesture began, while currentStepScaleFactor represents the additional zoom factor while a pinch gesture is in progress. But for rotation, only the current rotationAngle is stored. The horizontal and vertical offsets represent the distance that the image has been dragged by the pan gesture.

Example project @ code.qt.io