当前位置:   article > 正文

Qt 图片鼠标滚轮放缩、鼠标拖动平移、单击选点(附工程及源代码)_qt 鼠标缩放移动图片

qt 鼠标缩放移动图片

1.最终效果

Qt 图片交互——鼠标滚轮放缩、拖拽、选点_哔哩哔哩_bilibili

Qt 图片交互——鼠标滚轮放缩、拖拽、选点

2.核心代码说明

技术路线为:

基于QGraphicsView,使用scale函数执行放缩,使用centerOn(mapToScene(QPoint))执行平移,将QImage转化为QPixmap绘制在QGraphicsPixmapItem上。

头文件与源代码均是完整的,可以直接集成到工程中实用。

2.1.头文件

  1. #ifndef HI_GRAPHICSVIEW_H
  2. #define HI_GRAPHICSVIEW_H
  3. #include <QBoxLayout>
  4. #include <QGraphicsView>
  5. #include <qevent.h>
  6. class HI_ImageBox;
  7. class HI_GraphicsView:public QGraphicsView
  8. {
  9. Q_OBJECT
  10. public:
  11. HI_GraphicsView(QWidget *parent = 0);
  12. ~HI_GraphicsView();
  13. void Init(HI_ImageBox * imageBox){m_imageBox = imageBox;}
  14. void SetImage(const QImage & image);
  15. protected:
  16. virtual void wheelEvent(QWheelEvent *event) override;
  17. virtual void mouseMoveEvent(QMouseEvent *event) override;
  18. virtual void mousePressEvent(QMouseEvent *event) override;
  19. virtual void mouseReleaseEvent(QMouseEvent *event) override;
  20. virtual void mouseDoubleClickEvent(QMouseEvent *event) override;
  21. public slots:
  22. void ZoomIn();
  23. void ZoomOut();
  24. void Zoom(float scaleFactor);
  25. void Translate(QPointF delta);
  26. private:
  27. bool m_isTranslate;
  28. QPoint m_lastMousePos;
  29. HI_ImageBox * m_imageBox;
  30. QGraphicsScene * m_scene;
  31. QGraphicsPixmapItem * m_imageItem;
  32. };
  33. #endif // HI_GRAPHICSVIEW_H

2.2.源代码

  1. #include "hi_graphicsview.h"
  2. #include <QGraphicsPixmapItem>
  3. #include "hi_imagebox.h"
  4. HI_GraphicsView::HI_GraphicsView(QWidget *parent):
  5. QGraphicsView(parent),
  6. m_isTranslate(false),
  7. m_scene(new QGraphicsScene()),
  8. m_imageItem(new QGraphicsPixmapItem())
  9. {
  10. m_scene->addItem(m_imageItem);
  11. setScene(m_scene);
  12. setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  13. setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  14. setRenderHint(QPainter::Antialiasing);
  15. setSceneRect(INT_MIN/2, INT_MIN/2, INT_MAX, INT_MAX);
  16. setTransformationAnchor(QGraphicsView::AnchorViewCenter);
  17. centerOn(0, 0);
  18. }
  19. HI_GraphicsView::~HI_GraphicsView()
  20. {
  21. m_scene->deleteLater();
  22. delete m_imageItem;
  23. }
  24. void HI_GraphicsView::SetImage(const QImage &image)
  25. {
  26. m_imageItem->setPixmap(QPixmap::fromImage(image));
  27. QPoint newCenter(image.width() / 2 ,image.height()/2);
  28. //设置scene中心到图像中点
  29. centerOn(newCenter);
  30. show();
  31. }
  32. void HI_GraphicsView::wheelEvent(QWheelEvent *event)
  33. {
  34. // 滚轮的滚动量
  35. QPoint scrollAmount = event->angleDelta();
  36. // 正值表示滚轮远离使用者放大负值表示朝向使用者缩小
  37. scrollAmount.y() > 0 ? ZoomIn() : ZoomOut();
  38. }
  39. void HI_GraphicsView::mouseMoveEvent(QMouseEvent *event)
  40. {
  41. if(m_isTranslate)
  42. {
  43. //获取
  44. QPointF mouseDelta = event->pos()-m_lastMousePos;
  45. Translate(mouseDelta);
  46. }
  47. m_lastMousePos = event->pos();
  48. QGraphicsView::mouseMoveEvent(event);
  49. }
  50. void HI_GraphicsView::mousePressEvent(QMouseEvent *event)
  51. {
  52. if(event->button() == Qt::LeftButton)
  53. {
  54. m_isTranslate = true;
  55. m_lastMousePos = event->pos();
  56. }
  57. else if(event->button()==Qt::RightButton)
  58. {
  59. QPointF point = mapToScene(event->pos());
  60. //只有点击图片时才发送
  61. if (scene()->itemAt(point, transform()) != NULL)
  62. {
  63. emit m_imageBox->ImageClick(point.x(),point.y());
  64. }
  65. }
  66. QGraphicsView::mousePressEvent(event);
  67. }
  68. void HI_GraphicsView::mouseReleaseEvent(QMouseEvent *event)
  69. {
  70. if (event->button() == Qt::LeftButton)
  71. m_isTranslate = false;
  72. QGraphicsView::mouseReleaseEvent(event);
  73. }
  74. void HI_GraphicsView::mouseDoubleClickEvent(QMouseEvent *event)
  75. {
  76. centerOn(m_imageItem->pixmap().width()/2,m_imageItem->pixmap().height()/2);
  77. QGraphicsView::mouseDoubleClickEvent(event);
  78. }
  79. void HI_GraphicsView::ZoomIn()
  80. {
  81. Zoom(1.1);
  82. }
  83. void HI_GraphicsView::ZoomOut()
  84. {
  85. Zoom(0.9);
  86. }
  87. void HI_GraphicsView::Zoom(float scaleFactor)
  88. {
  89. // 防止过小或过大
  90. qreal factor = transform().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width();
  91. if (factor < 0.07 || factor > 100)
  92. return;
  93. scale(scaleFactor, scaleFactor);
  94. }
  95. void HI_GraphicsView::Translate(QPointF delta)
  96. {
  97. int w = viewport()->rect().width();
  98. int h = viewport()->rect().height();
  99. QPoint newCenter(w / 2. - delta.x()+0.5, h / 2. - delta.y()+0.5);
  100. centerOn(mapToScene(newCenter));
  101. }

其中HI_ImageBox类是做了一个接口,用于将类封装为一个工具库。可根据实际应用状况修改该部分内容。

接口类头文件

  1. #ifndef HI_IMAGEBOX_H
  2. #define HI_IMAGEBOX_H
  3. #include <QtCore/qglobal.h>
  4. #if defined(HI_IMAGEBOX_LIBRARY)
  5. # define HI_IMAGEBOX_EXPORT Q_DECL_EXPORT
  6. #else
  7. # define HI_IMAGEBOX_EXPORT Q_DECL_IMPORT
  8. #endif
  9. #include <QImage>
  10. #include <QBoxLayout>
  11. class HI_GraphicsView;
  12. /**
  13. * @brief The HI_ImageBox class
  14. * 图片展示控件,封装了图片加载,放缩,选点操作
  15. */
  16. class HI_IMAGEBOX_EXPORT HI_ImageBox:public QObject
  17. {
  18. Q_OBJECT
  19. public:
  20. HI_ImageBox();
  21. ~HI_ImageBox();
  22. /**
  23. * @brief 获取当前控件版本
  24. * @return
  25. */
  26. QString Version();
  27. /**
  28. * @brief 初始化
  29. * @param panel: 展示图片的容器
  30. */
  31. void Init(QBoxLayout * panel);
  32. /**
  33. * @brief 获取当前显示的图片
  34. * @return 当前显示的图片
  35. */
  36. const QImage & GetImage(){return m_image;}
  37. /**
  38. * @brief 设置图片
  39. * @param image: 要显示的图片
  40. */
  41. void SetImage(const QImage & image);
  42. /**
  43. * @brief 设置图片
  44. * @param image: 要展示图片的地址
  45. */
  46. void SetImage(const QString & image);
  47. //信号
  48. signals:
  49. /**
  50. * @brief 点击图片的信号
  51. * @param x: 点击的图片X坐标
  52. * @param y: 点击的图片Y坐标
  53. */
  54. void ImageClick(int x,int y);
  55. //变量
  56. private:
  57. HI_GraphicsView * m_widget; /**< 用于操作绘图的控件*/
  58. QImage m_image; /**< 当前显示图片*/
  59. friend class HI_GraphicsView;
  60. };
  61. #endif // HI_IMAGEBOX_H

接口类源代码

  1. #include "hi_imagebox.h"
  2. #include "hi_graphicsview.h"
  3. HI_ImageBox::HI_ImageBox():
  4. m_widget(nullptr)
  5. {
  6. }
  7. HI_ImageBox::~HI_ImageBox()
  8. {
  9. if(m_widget)
  10. m_widget->deleteLater();
  11. }
  12. QString HI_ImageBox::Version()
  13. {
  14. return "HI_ImageBox V1.0.21724";
  15. }
  16. void HI_ImageBox::Init(QBoxLayout *panel)
  17. {
  18. if(m_widget!=nullptr)
  19. return;
  20. m_widget = new HI_GraphicsView(nullptr);
  21. if(panel)
  22. panel->addWidget(m_widget);
  23. m_widget->Init(this);
  24. }
  25. void HI_ImageBox::SetImage(const QImage &image)
  26. {
  27. m_image = image;
  28. m_widget->SetImage(image);
  29. }
  30. void HI_ImageBox::SetImage(const QString &image)
  31. {
  32. QImage img = QImage(image);
  33. if(!img.isNull())
  34. {
  35. m_image = img;
  36. m_widget->SetImage(img);
  37. }
  38. }

3.工程源代码(包含demo)

不需要积分,直接下载

Qt图片交互——鼠标滚轮放缩、拖拽、选点-C++文档类资源-CSDN下载

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/91689
推荐阅读
相关标签
  

闽ICP备14008679号