日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Qt QWidget实现手势缩放和平移(一)

發布時間:2025/1/21 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Qt QWidget实现手势缩放和平移(一) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

由于項目要求,需要在QWidget中實現一個手勢操作的功能,對圖片進行放大/縮小/平移功能,并且還需要支持通過鼠標和鍵盤來實現該功能。其實這種功能在QGraphicsView中實現比較簡單, 不過在QWidget中也能實現,本次通過QGestureEvent來捕捉手勢操作,然后對圖片進行縮放或者移動。

廢話不多說,直接上代碼


首先來看頭文件:

class QGestureEvent; class QPanGesture; class QPinchGesture; class QSwipeGesture; class CProjectionPicture;class CProjectionPicture : public QWidget {Q_OBJECT public:CProjectionPicture(QWidget *parent = 0);void setPicture(QImage & image);protected:// 放大/縮小void wheelEvent(QWheelEvent *event) Q_DECL_OVERRIDE;void mouseDoubleClickEvent(QMouseEvent *event) Q_DECL_OVERRIDE;void keyPressEvent(QKeyEvent *event) Q_DECL_OVERRIDE;// 平移void mouseMoveEvent(QMouseEvent *event) Q_DECL_OVERRIDE;void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE;bool event(QEvent *event) Q_DECL_OVERRIDE;void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;void resizeEvent(QResizeEvent *e) Q_DECL_OVERRIDE;public Q_SLOTS:void zoomIn(); // 放大void zoomOut(); // 縮小void zoom(float scale); // 縮放 - scaleFactor:縮放的比例因子void translate(QPointF delta); // 平移private:bool gestureEvent(QGestureEvent *event);void panTriggered(QPanGesture*);void pinchTriggered(QPinchGesture*);QImage loadImage(const QString &fileName);QImage currentImage;qreal horizontalOffset;qreal verticalOffset;qreal scaleFactor;qreal currentStepScaleFactor;Qt::MouseButton m_translateButton; // 平移按鈕bool m_bMouseTranslate;qreal m_zoomDelta; // 縮放的增量QPoint m_lastMousePos; // 鼠標最后按下的位置};
源文件:

CProjectionPicture::CProjectionPicture(QWidget *parent): QWidget(parent),horizontalOffset(0),verticalOffset(0),scaleFactor(1),currentStepScaleFactor(1),m_translateButton(Qt::LeftButton),m_bMouseTranslate(false),m_zoomDelta(0.2), {this->setFocusPolicy(Qt::ClickFocus);grabGesture(Qt::PanGesture);grabGesture(Qt::PinchGesture);grabGesture(Qt::SwipeGesture); } void CProjectionPicture::setPicture(QImage &image) {currentImage = image.convertToFormat(QImage::Format_RGB888);update(); }bool CProjectionPicture::event(QEvent *event) {if (event->type() == QEvent::Gesture)return gestureEvent(static_cast<QGestureEvent*>(event));return QWidget::event(event); }void CProjectionPicture::paintEvent(QPaintEvent*) {QPainter painter(this);QImage image = currentImage;if(!image.isNull()){image = image.scaled(this->width()*currentStepScaleFactor * scaleFactor,this->height()*currentStepScaleFactor * scaleFactor,Qt::KeepAspectRatio,Qt::SmoothTransformation);}const qreal iw = image.width();const qreal ih = image.height();const qreal wh = height();const qreal ww = width();painter.translate(ww/2, wh/2);painter.translate(horizontalOffset, verticalOffset);//painter.scale(currentStepScaleFactor * scaleFactor, currentStepScaleFactor * scaleFactor);painter.translate(-iw/2, -ih/2);painter.drawImage(0,0,image);}void CProjectionPicture::mouseDoubleClickEvent(QMouseEvent *) {scaleFactor = 1;currentStepScaleFactor = 1;verticalOffset = 0;horizontalOffset = 0;update(); }bool CProjectionPicture::gestureEvent(QGestureEvent *event) {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; }void CProjectionPicture::panTriggered(QPanGesture *gesture) { #ifndef QT_NO_CURSORswitch (gesture->state()) {case Qt::GestureStarted:case Qt::GestureUpdated:setCursor(Qt::SizeAllCursor);break;default:setCursor(Qt::ArrowCursor);} #endifQPointF delta = gesture->delta();horizontalOffset += delta.x();verticalOffset += delta.y();update(); }void CProjectionPicture::pinchTriggered(QPinchGesture *gesture) {QPinchGesture::ChangeFlags changeFlags = gesture->changeFlags();if (changeFlags & QPinchGesture::ScaleFactorChanged) {currentStepScaleFactor = gesture->totalScaleFactor();}if (gesture->state() == Qt::GestureFinished) {scaleFactor *= currentStepScaleFactor;currentStepScaleFactor = 1;}update(); }void CProjectionPicture::resizeEvent(QResizeEvent*e) {update();QWidget::resizeEvent(e); }// 上/下/左/右鍵向各個方向移動、加/減鍵進行縮放 void CProjectionPicture::keyPressEvent(QKeyEvent *event) {switch (event->key()) {qDebug() << event->key();case Qt::Key_Up:translate(QPointF(0, -5)); // 上移break;case Qt::Key_Down:translate(QPointF(0, 5)); // 下移break;case Qt::Key_Left:translate(QPointF(-5, 0)); // 左移break;case Qt::Key_Right:translate(QPointF(5, 0)); // 右移break;case Qt::Key_Plus: // 放大zoomIn();break;case Qt::Key_Minus: // 縮小zoomOut();break;default:QWidget::keyPressEvent(event);}QWidget::keyPressEvent(event); }// 平移 void CProjectionPicture::mouseMoveEvent(QMouseEvent *event) {if (m_bMouseTranslate){QPointF mouseDelta = event->pos() - m_lastMousePos;translate(mouseDelta);}m_lastMousePos = event->pos();QWidget::mouseMoveEvent(event); }void CProjectionPicture::mousePressEvent(QMouseEvent *event) {qDebug() << "CProjectionPicture::mousePressEvent";if (event->button() == m_translateButton) {m_bMouseTranslate = true;m_lastMousePos = event->pos();setCursor(Qt::OpenHandCursor);}QWidget::mousePressEvent(event); }void CProjectionPicture::mouseReleaseEvent(QMouseEvent *event) {if (event->button() == m_translateButton){m_bMouseTranslate = false;setCursor(Qt::ArrowCursor);}QWidget::mouseReleaseEvent(event); }// 放大/縮小 void CProjectionPicture::wheelEvent(QWheelEvent *event) {qDebug() << "CProjectionPicture::wheelEvent"; // QPoint numPixels = event->pixelDelta();QPoint scrallAmount = event->angleDelta();if(scrallAmount.y() > 0){zoomIn();}else if(scrallAmount.y() < 0){zoomOut();}QWidget::wheelEvent(event); }// 放大 void CProjectionPicture::zoomIn() {zoom(1 + m_zoomDelta); }// 縮小 void CProjectionPicture::zoomOut() {zoom(1 - m_zoomDelta); }// 縮放 - scaleFactor:縮放的比例因子 void CProjectionPicture::zoom(float scale) {scaleFactor *= scale;update(); }// 平移 void CProjectionPicture::translate(QPointF delta) {horizontalOffset += delta.x();verticalOffset += delta.y();update(); }
本文參考:http://blog.csdn.net/liang19890820/article/details/53543017

總結

以上是生活随笔為你收集整理的Qt QWidget实现手势缩放和平移(一)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。