http://doc.qt.io/qt-5/qtwidgets-gestures-imagegestures-example.html
来源:互联网 发布:矩阵制组织结构的特点 编辑:程序博客网 时间:2024/06/03 21:58
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){ qCDebug(lcExample) << "gestureEvent():" << 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 pinchTriggered()
function, which handles the pinch gesture when the user moves two fingers around on the display or input device:
void ImageWidget::pinchTriggered(QPinchGesture *gesture){ QPinchGesture::ChangeFlags changeFlags = gesture->changeFlags(); if (changeFlags & QPinchGesture::RotationAngleChanged) { 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; } if (gesture->state() == Qt::GestureFinished) { scaleFactor *= 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.
void ImageWidget::paintEvent(QPaintEvent*){ QPainter p(this); const qreal iw = currentImage.width(); const qreal ih = currentImage.height(); const qreal wh = height(); const qreal 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.
Files:
- gestures/imagegestures/imagewidget.cpp
- gestures/imagegestures/imagewidget.h
- gestures/imagegestures/mainwidget.cpp
- gestures/imagegestures/mainwidget.h
- gestures/imagegestures/main.cpp
- gestures/imagegestures/imagegestures.pro
- http://doc.qt.io/qt-5/qtwidgets-gestures-imagegestures-example.html
- QT的英文教程:http://doc.trolltech.com/qq/index.html
- Qt下载地址:http://download.qt.io/
- qt学习笔记(4):无法打开源文件QtWidgets/QApplication
- QT learn online doc
- qt 打印 doc文件
- NOKIA QT EXAMPLE
- Qt qwt的example
- QT (Application Example)
- Qt的时钟Example
- Qt示例[Mandelbrot Example]
- Qt-Image Viewer Example
- QT HTTP
- Qt在VS2012中引用QtWidgets时报GLES2/gl2.h无法打开错误的解决办法
- QT Screenshot Example 看看看
- Qt Pixelator Example 看看看~
- Qt example之随机joke
- Qt example之随机笑话
- String转int 非库函数 Java
- Java Jersey web service 总结
- vim:学习资料——把vim打造成 IDE
- ZooKeeper学习之zookeeper的ACL(AUTH)
- JDBC的使用
- http://doc.qt.io/qt-5/qtwidgets-gestures-imagegestures-example.html
- OpenGL视角LooAt及Perspective理解
- 【Leetcode】【python】Search Insert Position
- QGesture
- Leetcode--Two Sum
- 深度学习--Batch_Normlization
- day3代码
- “RPC好,还是RESTful好?”
- 牛客网-剑指offer-10-矩形覆盖