Qt 4.6: Gestures Programming

|  | Home · All Classes · Overviews |

# Gestures Programming

The QGesture class provides the ability to form gestures from a series of events independent of the input method. A gesture could be a particular movement of a mouse, a touch screen action, or a series of events from some other source. The nature of the input, the interpretation of the gesture and the action taken are the choice of the implementing developer.

- Creating Your Own Gesture Recognizer
- An Example, ImageViewer

### Creating Your Own Gesture Recognizer

QGesture is a base class for a user defined gesture recognizer class. In order to implement the recognizer you will need to subclass the QGesture class and implement the pure virtual function filterEvent(). Once you have implemented the filterEvent() function to make your own recognizer you can process events. A sequence of events may, according to your own rules, represent a gesture. The events can be singly passed to the recognizer via the filterEvent() function or as a stream of events by specifying a parent source of events. The events can be from any source and could result in any action as defined by the user. The source and action need not be graphical though that would be the most likely scenario. To find how to connect a source of events to automatically feed into the recognizer see QGesture.

Recognizers based on QGesture can emit any of the following signals:

     Q_SIGNALS:
         void started();
         void triggered();
         void finished();
         void cancelled();

These signals are emitted when the state changes with the call to updateState(), more than one signal may be emitted when a change of state occurs. There are four GestureStates

New State | Description | QGesture Actions on Entering this State
--- | --- | ---
Qt::NoGesture | Initial value | emit cancelled()
Qt::GestureStarted | A continuous gesture has started | emit started() and emit triggered()
Qt::GestureUpdated | A gesture continues | emit triggered()
Qt::GestureFinished | A gesture has finished. | emit finished()

Note: started() can be emitted if entering any state greater than NoGesture if NoGesture was the previous state. This means that your state machine does not need to explicitly use the Qt::GestureStarted state, you can simply proceed from NoGesture to Qt::GestureUpdated to emit a started() signal and a triggered() signal.

You may use some or all of these states when implementing the pure virtual function filterEvent(). filterEvent() will usually implement a state machine using the GestureState enums, but the details of which states are used is up to the developer.

You may also need to reimplement the virtual function reset() if internal data or objects need to be re-initialized. The function must conclude with a call to updateState() to change the current state to Qt::NoGesture.

### An Example, ImageViewer

To illustrate how to use QGesture we will look at the ImageViewer example. This example uses QPanGesture, standard gesture, and an implementation of TapAndHoldGesture. Note that TapAndHoldGesture is platform dependent.

     void TapAndHoldGesture::reset()
     {
         timer.stop();
         iteration = 0;
         position = startPosition = QPoint();
         updateState(Qt::NoGesture);
     }

In ImageViewer we see that the ImageWidget class uses two gestures: QPanGesture and TapAndHoldGesture. The QPanGesture is a standard gesture which is part of Qt. TapAndHoldGesture is defined and implemented as part of the example. The ImageWidget listens for signals from the gestures, but is not interested in the started() signal.

     private slots:
         void gestureTriggered();
         void gestureFinished();
         void gestureCancelled();

TapAndHoldGesture uses QTouchEvent events and mouse events to detect start, update and end events that can be mapped onto the GestureState changes. The implementation in this case uses a timer as well. If the timeout event occurs a given number of times after the start of the gesture then the gesture is considered to have finished whether or not the appropriate touch or mouse event has occurred. Also if a large jump in the position of the event occurs, as calculated by the manhattanLength() call, then the gesture is cancelled by calling reset() which tidies up and uses updateState() to change state to NoGesture which will result in the cancelled() signal being emitted by the recognizer.

ImageWidget handles the signals by connecting the slots to the signals, although `cancelled()` is not connected here.

         panGesture = new QPanGesture(this);
         connect(panGesture, SIGNAL(triggered()), this, SLOT(gestureTriggered()));

         tapAndHoldGesture = new TapAndHoldGesture(this);
         connect(tapAndHoldGesture, SIGNAL(triggered()), this, SLOT(gestureTriggered()));
         connect(tapAndHoldGesture, SIGNAL(finished()), this, SLOT(gestureFinished()));

These functions in turn will have to be aware of which gesture object was the source of the signal since we have more than one source per slot. This is easily done by using the QObject::sender() function as shown here

     void ImageWidget::gestureTriggered()
     {
         if (sender() == panGesture) {

As triggered() signals are handled by gestureTriggered() there may be position updates invoking calls to, for example, goNextImage(), this will cause a change in the image handling logic of ImageWidget and a call to updateImage() to display the changed state.

Following the logic of how the QEvent is processed we can summmarize it as follows:

- filterEvent() becomes the event filter of the parent ImageWidget object for a QPanGesture object and a TapAndHoldGesture object.
- filterEvent() then calls updateState() to change states
- updateState() emits the appropriate signal(s) for the state change.
- The signals are caught by the defined slots in ImageWidget
- The widget logic changes and an update() results in a paint event.