Qt Reference Documentation

Maemo 5 Gesture Example

Files:

The Maemo 5 Menu example shows how to create and use a custom gesture for a Maemo 5 device.

Current Maemo 5 devices consider every touch to be a mouse press. This is fine since none of the devices has multipoint-touch capabilites.

From Qt 4.7 and later, the Qt library will generate touch events for every mouse event which allows the use of gestures on Maemo 5 devices. The generated touch events will only have one touch point.

The example will show three rectangles (QGraphicsObject) that grab a custom gesture and react on it. The custom gesture will detect horizontal gestures and depending on the direction strike through the rectangles or remove the strike through mark.

The Custom Gesture

The custom gesture is called FingerSwipeGesture and it's implementation can be seen in fingerswipegesture.cpp. This gesture triggers on a near horizontal drawn line. It will be used to implement a "strike through" functionality.

The target widgets need to accept touch events or else the gesture won't see them afterwards. The call to

         widget->setAttribute(Qt::WA_AcceptTouchEvents);

for QWidgets or

             go->setAcceptTouchEvents(true);

for QGraphicsObjects will take care of this and is done in the FingerSwipeGestureRecognizer.

Note that only the QTapGesture and the QTapAndHoldGesture standard gestures are useful because they can be triggered with one touch point. Also you will need to QGraphicsItem::setAcceptTouchEvents() for those gestures by yourself.

The Graphics Items

The rectangles are of the type RectObject which is implemented in main.cpp. RectObject extends QGraphicsObject.

We will need to register the new custom gesture and then grab the gesture for the graphics items. This is done in the constructor of RectObject

         if (!fingerSwipeGestureType)
             fingerSwipeGestureType = QGestureRecognizer::registerRecognizer( new FingerSwipeGestureRecognizer() );

         grabGesture(fingerSwipeGestureType);

In order to receive touch updates and touch end events we will need to accept the touch begin events. This is done in a re-implementation of sceneEvent().

         if (e->type() == QEvent::TouchBegin) {
             e->accept();
             return true;
         }

Also in sceneEvent we can handle the QGestureEvent.

         if (e->type() == QEvent::Gesture) {
              QGestureEvent *ge = static_cast<QGestureEvent*>(e);

              if (QGesture *gesture = ge->gesture(fingerSwipeGestureType)) {
                  FingerSwipeGesture *swipe = static_cast<FingerSwipeGesture*>(gesture);
                  if (swipe->state() == Qt::GestureFinished) {
                      m_striken = swipe->isLeftToRight();
                      update();
                  }
                  ge->setAccepted(gesture, true);
                  return true;
              }
         }

The only thing left to do is to create the graphics items and show them inside a QGraphicsView.

         RectObject *rect1 = new RectObject( 10, 10, 200, 100, QBrush( Qt::gray ) );
         RectObject *rect2 = new RectObject( 100, 200, 200, 200, QBrush( Qt::green ) );
         RectObject *rect3 = new RectObject( 400, 300, 300, 80, QBrush( Qt::yellow ) );

         scene->addItem(rect1);
         scene->addItem(rect2);
         scene->addItem(rect3);
X

Thank you for giving your feedback.

Make sure it is related to this specific page. For more general bugs and requests, please use the Qt Bug Tracker.

[0]; s.parentNode.insertBefore(ga, s); })();