path: root/examples
diff options
authorShawn Rutledge <>2015-02-25 10:53:31 +0100
committerShawn Rutledge <>2015-03-03 13:30:25 +0000
commita9515cd02fabcb90691093c388be9849282a9e22 (patch)
tree8501b78ef0179a599541b1fefbff7e6a33d537bc /examples
parent2b5982aac8ad103443e33379fe5654f5bd419c87 (diff)
doc: imagegestures example: document the pinch handler
The swipe handler is simple, and also doesn't currently work on every platform. But the pinch handler is the one that needs explanation, because the difference between incremental and absolute values of the rotation and scale properties is tricky. Change-Id: Ie3c7f4941d4a17734c9a920a8dd978f86fb03c4b Reviewed-by: Friedemann Kleint <> Reviewed-by: Topi Reiniƶ <>
Diffstat (limited to 'examples')
2 files changed, 54 insertions, 15 deletions
diff --git a/examples/widgets/gestures/imagegestures/doc/src/imagegestures.qdoc b/examples/widgets/gestures/imagegestures/doc/src/imagegestures.qdoc
index 1b0406037c..07acef6b4d 100644
--- a/examples/widgets/gestures/imagegestures/doc/src/imagegestures.qdoc
+++ b/examples/widgets/gestures/imagegestures/doc/src/imagegestures.qdoc
@@ -83,18 +83,50 @@
QGesture subclass.
To illustrate how a standard gesture can be interpreted by an application, we
- show the implementation of the \c swipeTriggered() function, which handles the
- gesture associated with a brushing or swiping motion on the user's display or
+ show the implementation of the \c pinchTriggered() function, which handles the
+ pinch gesture when the user moves two fingers around on the display or
input device:
- \snippet gestures/imagegestures/imagewidget.cpp swipe function
- 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.
+ \snippet gestures/imagegestures/imagewidget.cpp pinch function
+ 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 \c scaleFactor() is a relative value representing how much the zoom
+ should change from one event to the next, whereas \c totalScaleFactor()
+ provides the amount of zoom that has been expressed since the gesture
+ began. When the touch points are released and another gesture begins,
+ \c totalScaleFactor() will begin again at 1.0. In this case we store
+ \c totalScaleFactor() into the \c currentStepScaleFactor variable so that
+ it can be used in \c paintEvent() to scale the image. Alternatively it would
+ be possible to simply multiply the stored total scale factor by
+ \c scaleFactor() here in the pinch handler.
+ In contrast, \c rotationAngle() represents the amount of rotation since the
+ pinch gesture began, while \c 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, \c 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 \c rotationAngle (which
+ will be applied in \c paintEvent()). If we simply assigned
+ \c totalRotationAngle() to the stored \c 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 \c rotationAngle in \c 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.
+ \snippet gestures/imagegestures/imagewidget.cpp paint method
+ In \c 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.
diff --git a/examples/widgets/gestures/imagegestures/imagewidget.cpp b/examples/widgets/gestures/imagegestures/imagewidget.cpp
index 38319a9dbe..3d0d7e7a93 100644
--- a/examples/widgets/gestures/imagegestures/imagewidget.cpp
+++ b/examples/widgets/gestures/imagegestures/imagewidget.cpp
@@ -77,6 +77,7 @@ bool ImageWidget::event(QEvent *event)
//! [event handler]
+//! [paint method]
void ImageWidget::paintEvent(QPaintEvent*)
QPainter p(this);
@@ -93,6 +94,7 @@ void ImageWidget::paintEvent(QPaintEvent*)
p.translate(-iw/2, -ih/2);
p.drawImage(0, 0, currentImage);
+//! [paint method]
void ImageWidget::mouseDoubleClickEvent(QMouseEvent *)
@@ -138,16 +140,20 @@ void ImageWidget::panTriggered(QPanGesture *gesture)
+//! [pinch function]
void ImageWidget::pinchTriggered(QPinchGesture *gesture)
QPinchGesture::ChangeFlags changeFlags = gesture->changeFlags();
if (changeFlags & QPinchGesture::RotationAngleChanged) {
- rotationAngle += gesture->rotationAngle() - gesture->lastRotationAngle();
- qCDebug(lcExample) << "pinchTriggered(): rotate to" << rotationAngle;
+ qreal 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;
+ qCDebug(lcExample) << "pinchTriggered(): zoom by" <<
+ gesture->scaleFactor() << "->" << currentStepScaleFactor;
if (gesture->state() == Qt::GestureFinished) {
scaleFactor *= currentStepScaleFactor;
@@ -155,6 +161,7 @@ void ImageWidget::pinchTriggered(QPinchGesture *gesture)
+//! [pinch function]
//! [swipe function]
void ImageWidget::swipeTriggered(QSwipeGesture *gesture)