feat: 增加二指触屏功能

Description: 增加二指触屏功能

Log: 增加二指触屏功能
Change-Id: Ib96e7082ebf651708992357bcbe064048b830ce2
This commit is contained in:
liuminghang 2021-06-24 11:25:02 +08:00
parent d02ef1d18a
commit 9d1d8aafc1
2 changed files with 50 additions and 3 deletions

View File

@ -10,6 +10,8 @@
#include <QThreadPool> #include <QThreadPool>
#include <qmath.h> #include <qmath.h>
#include <QObject> #include <QObject>
#include <QGestureEvent>
#include <QPinchGesture>
const qreal MAX_SCALE_FACTOR = 20.0; const qreal MAX_SCALE_FACTOR = 20.0;
const qreal MIN_SCALE_FACTOR = 0.029; const qreal MIN_SCALE_FACTOR = 0.029;
@ -23,6 +25,8 @@ ImageView::ImageView(QWidget *parent):
setScene(scene); setScene(scene);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
this->grabGesture(Qt::PinchGesture);
setAttribute(Qt::WA_AcceptTouchEvents);
} }
void ImageView::openImage(const QString &path) void ImageView::openImage(const QString &path)
@ -192,6 +196,23 @@ void ImageView::mouseMoveEvent(QMouseEvent *event)
return QGraphicsView::mouseMoveEvent(event); return QGraphicsView::mouseMoveEvent(event);
} }
bool ImageView::event(QEvent *event)
{
if (event->type() == QEvent::Gesture) {
return gestureEvent(static_cast<QGestureEvent *>(event));
}
return QGraphicsView::event(event);
}
bool ImageView::gestureEvent(QGestureEvent *event)
{
// if (QGesture *swipe = event->gesture(Qt::SwipeGesture)) //滑动逻辑
// swipeTriggered(static_cast<QSwipeGesture *>(swipe));
if (QGesture *pinch = event->gesture(Qt::PinchGesture))//双指捏合
pinchTriggered(static_cast<QPinchGesture *>(pinch));
return true;
}
const QImage ImageView::image() const QImage ImageView::image()
{ {
@ -215,6 +236,27 @@ void ImageView::wheelEvent(QWheelEvent *event)
event->accept(); event->accept();
} }
void ImageView::handleGestureEvent(QGestureEvent *gesture)
{
qDebug() << "------" << __FUNCTION__ << "";
if (QGesture *pinch = gesture->gesture(Qt::PinchGesture))
pinchTriggered(static_cast<QPinchGesture *>(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) void ImageView::scaleAtPoint(QPoint pos, qreal factor)
{ {
// Remember zoom anchor point. // Remember zoom anchor point.
@ -264,3 +306,4 @@ void ImageView::setScaleValue(qreal v)
} }

View File

@ -7,8 +7,8 @@
#include <QGraphicsView> #include <QGraphicsView>
class QGraphicsPixmapItem; class QGraphicsPixmapItem;
class QGestureEvent;
class QPinchGesture;
class ImageView : public QGraphicsView class ImageView : public QGraphicsView
{ {
@ -27,6 +27,8 @@ public:
void autoFit(); void autoFit();
//鼠标移动事件 //鼠标移动事件
void mouseMoveEvent(QMouseEvent *event) override; void mouseMoveEvent(QMouseEvent *event) override;
bool event(QEvent *event)override;
bool gestureEvent(QGestureEvent *event);
//返回当前图片img //返回当前图片img
const QImage image(); const QImage image();
@ -50,7 +52,9 @@ public slots:
//鼠标滚轮事件 //鼠标滚轮事件
void wheelEvent(QWheelEvent *event) override; void wheelEvent(QWheelEvent *event) override;
protected: protected:
//二指捏合功能的触屏事件
void handleGestureEvent(QGestureEvent *gesture);
void pinchTriggered(QPinchGesture *gesture);
private: private:
QString m_currentPath;//当前图片路径 QString m_currentPath;//当前图片路径