From 9d1d8aafc19fc68c4a33b8bbfa09d76e3b12d599 Mon Sep 17 00:00:00 2001 From: liuminghang Date: Thu, 24 Jun 2021 11:25:02 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E4=BA=8C=E6=8C=87?= =?UTF-8?q?=E8=A7=A6=E5=B1=8F=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Description: 增加二指触屏功能 Log: 增加二指触屏功能 Change-Id: Ib96e7082ebf651708992357bcbe064048b830ce2 --- src/view/imageview.cpp | 43 ++++++++++++++++++++++++++++++++++++++++++ src/view/imageview.h | 10 +++++++--- 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/src/view/imageview.cpp b/src/view/imageview.cpp index 33b8b3f..b062d55 100644 --- a/src/view/imageview.cpp +++ b/src/view/imageview.cpp @@ -10,6 +10,8 @@ #include #include #include +#include +#include const qreal MAX_SCALE_FACTOR = 20.0; const qreal MIN_SCALE_FACTOR = 0.029; @@ -23,6 +25,8 @@ ImageView::ImageView(QWidget *parent): setScene(scene); setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + this->grabGesture(Qt::PinchGesture); + setAttribute(Qt::WA_AcceptTouchEvents); } void ImageView::openImage(const QString &path) @@ -192,6 +196,23 @@ void ImageView::mouseMoveEvent(QMouseEvent *event) return QGraphicsView::mouseMoveEvent(event); } +bool ImageView::event(QEvent *event) +{ + if (event->type() == QEvent::Gesture) { + return gestureEvent(static_cast(event)); + } + return QGraphicsView::event(event); +} + +bool ImageView::gestureEvent(QGestureEvent *event) +{ +// if (QGesture *swipe = event->gesture(Qt::SwipeGesture)) //滑动逻辑 +// swipeTriggered(static_cast(swipe)); + if (QGesture *pinch = event->gesture(Qt::PinchGesture))//双指捏合 + pinchTriggered(static_cast(pinch)); + return true; +} + const QImage ImageView::image() { @@ -215,6 +236,27 @@ void ImageView::wheelEvent(QWheelEvent *event) event->accept(); } + +void ImageView::handleGestureEvent(QGestureEvent *gesture) +{ + qDebug() << "------" << __FUNCTION__ << ""; + if (QGesture *pinch = gesture->gesture(Qt::PinchGesture)) + pinchTriggered(static_cast(pinch)); +} + +void ImageView::pinchTriggered(QPinchGesture *gesture) +{ + qDebug() << "------" << __FUNCTION__ << ""; + QPinchGesture::ChangeFlags changeFlags = gesture->changeFlags(); + //缩放手势 + if (changeFlags & QPinchGesture::ScaleFactorChanged) { + QPoint pos = mapFromGlobal(gesture->centerPoint().toPoint()); + if (abs(gesture->scaleFactor() - 1) > 0.006) { + qDebug() << "scaleFactor" << gesture->scaleFactor(); + scaleAtPoint(pos, gesture->scaleFactor()); + } + } +} void ImageView::scaleAtPoint(QPoint pos, qreal factor) { // Remember zoom anchor point. @@ -264,3 +306,4 @@ void ImageView::setScaleValue(qreal v) } + diff --git a/src/view/imageview.h b/src/view/imageview.h index 74c3fc7..252e01c 100644 --- a/src/view/imageview.h +++ b/src/view/imageview.h @@ -7,8 +7,8 @@ #include class QGraphicsPixmapItem; - - +class QGestureEvent; +class QPinchGesture; class ImageView : public QGraphicsView { @@ -27,6 +27,8 @@ public: void autoFit(); //鼠标移动事件 void mouseMoveEvent(QMouseEvent *event) override; + bool event(QEvent *event)override; + bool gestureEvent(QGestureEvent *event); //返回当前图片img const QImage image(); @@ -50,7 +52,9 @@ public slots: //鼠标滚轮事件 void wheelEvent(QWheelEvent *event) override; protected: - + //二指捏合功能的触屏事件 + void handleGestureEvent(QGestureEvent *gesture); + void pinchTriggered(QPinchGesture *gesture); private: QString m_currentPath;//当前图片路径