当前位置:   article > 正文

Qt编写自定义控件:图片显示控件_qt 图片控件

qt 图片控件
  1. #ifndef PICTUREVIEWINGCONTROL_H
  2. #define PICTUREVIEWINGCONTROL_H
  3. #include <QWidget>
  4. class PictureViewingControl : public QWidget
  5. {
  6. Q_OBJECT
  7. public:
  8. PictureViewingControl(QWidget * parent = nullptr);
  9. ~PictureViewingControl();
  10. void setPixmap(const QString &imgFilePath);
  11. private:
  12. void paintEvent(QPaintEvent *event)override;
  13. bool event(QEvent * event)override;
  14. public:
  15. enum class ActionType
  16. {
  17. Amplification,//放大
  18. Shrink,//缩小
  19. Move,//移动
  20. None//不进行任何操作
  21. };
  22. private:
  23. QPoint Alloffset;//总偏移
  24. QPoint offset;
  25. QRect paintRect;
  26. QRect drawRect;
  27. QPixmap img_src;//源图
  28. ActionType action;//操作
  29. double ratio;//缩放比例
  30. };
  31. #endif // PICTUREVIEWINGCONTROL_H
  1. #include "pictureviewingcontrol.h"
  2. #include <QWheelEvent>
  3. #include <QPainter>
  4. PictureViewingControl::PictureViewingControl(QWidget *parent):
  5. QWidget(parent)
  6. {
  7. action = ActionType::None;//初始动作:什么都不做
  8. ratio = 1.0;
  9. Alloffset = QPoint(0,0);
  10. }
  11. PictureViewingControl::~PictureViewingControl()
  12. {
  13. }
  14. bool PictureViewingControl::event(QEvent * event)
  15. {
  16. static bool press = false;//是否按下了左键
  17. static QPoint PreDot;
  18. auto type = event->type();
  19. if(type == QEvent::MouseButtonPress )//鼠标按下事件
  20. {
  21. if(QMouseEvent *mouse = static_cast<QMouseEvent* >(event))
  22. {
  23. if(mouse->button() == Qt::LeftButton && drawRect.contains(mouse->pos()))
  24. {
  25. press = true;
  26. PreDot = mouse->pos();
  27. setCursor(QCursor(Qt::ClosedHandCursor));
  28. }
  29. }
  30. }
  31. else if(type == QEvent::MouseButtonRelease)//鼠标松开事件
  32. {
  33. if(QMouseEvent *mouse = static_cast<QMouseEvent* >(event))
  34. {
  35. if(mouse->button() == Qt::LeftButton && press)
  36. {
  37. press = false;
  38. }
  39. setCursor(QCursor(Qt::OpenHandCursor));
  40. }
  41. }
  42. else if(type == QEvent::MouseMove)//移动鼠标
  43. {
  44. if(press)
  45. {
  46. if(QMouseEvent *mouse = static_cast<QMouseEvent* >(event))
  47. {
  48. offset.setX(mouse->x() - PreDot.x());
  49. offset.setY(mouse->y() - PreDot.y());
  50. PreDot = mouse->pos();
  51. action = ActionType::Move;
  52. this->repaint();
  53. }
  54. }
  55. }
  56. else if(type == QEvent::Wheel)
  57. {
  58. if(QWheelEvent *mouse = static_cast<QWheelEvent* >(event))
  59. {
  60. if(mouse->angleDelta().y() < 0)
  61. {
  62. //下滑,缩小
  63. action = ActionType::Shrink;
  64. this->repaint();
  65. action = ActionType::None;
  66. }
  67. else
  68. {
  69. //上滑,放大
  70. action = ActionType::Amplification;
  71. this->repaint();
  72. action = ActionType::None;
  73. }
  74. }
  75. }
  76. return QWidget::event(event);
  77. }
  78. void PictureViewingControl::setPixmap(const QString & imgFilePath)
  79. {
  80. img_src.load(imgFilePath);
  81. ratio = 0.2;
  82. Alloffset = QPoint(0,0);
  83. offset = QPoint(0,0);
  84. repaint();
  85. }
  86. void PictureViewingControl::paintEvent(QPaintEvent *event)
  87. {
  88. if(img_src.isNull())
  89. {
  90. return;
  91. }
  92. auto rect = event->rect();
  93. paintRect = QRect(1, 1, rect.width() + 1, rect.height() + 1);
  94. QPainter painter(this);
  95. painter.setRenderHint(QPainter::Antialiasing);//开启抗锯齿
  96. int NowW = ratio * img_src.width();//缩放后的图片尺寸
  97. int NowH = ratio * img_src.height();
  98. if(action == ActionType::Shrink)//缩小
  99. {
  100. ratio -= 0.1 * ratio;
  101. if(ratio < 0.2)
  102. {
  103. ratio = 0.2;
  104. }
  105. }
  106. if(action == ActionType::Amplification)//放大
  107. {
  108. ratio += 0.1 * ratio;
  109. if(ratio > 2)
  110. {
  111. ratio = 2;
  112. }
  113. }
  114. auto temp_img = img_src.copy().scaled(NowW, NowH,Qt::KeepAspectRatio);
  115. if(action == ActionType::Move)//移动
  116. {
  117. Alloffset.setX(Alloffset.x() + offset.x());
  118. Alloffset.setY(Alloffset.y() + offset.y());
  119. }
  120. //限制偏移量 效果是图像超出边框太多则弹回来
  121. if(abs(Alloffset.x()) >= (paintRect.width()/2 + NowW/2 - 10)) //限制X偏移值
  122. {
  123. if(Alloffset.x() > 0)
  124. {
  125. Alloffset.setX(paintRect.width()/2 + NowW/2 - 100);//右
  126. }
  127. else
  128. {
  129. Alloffset.setX(-paintRect.width()/2 + -NowW/2 + 100);//左
  130. }
  131. }
  132. if(abs(Alloffset.y()) >= (paintRect.height()/2 + NowH/2 - 10)) //限制Y偏移值
  133. {
  134. if(Alloffset.y() > 0)
  135. {
  136. Alloffset.setY(paintRect.height()/2 + NowH/2 - 100);//下
  137. }
  138. else
  139. {
  140. Alloffset.setY(-paintRect.height()/2 + -NowH/2 + 100);//上
  141. }
  142. }
  143. int x = paintRect.width()/2 + Alloffset.x() - NowW/2;
  144. if(x < 0)
  145. {
  146. x = 0;
  147. }
  148. int y = paintRect.height()/2 + Alloffset.y() - NowH/2;
  149. if(y < 0)
  150. {
  151. y = 0;
  152. }
  153. int sx = NowW/2 - paintRect.width()/2 - Alloffset.x();
  154. if(sx < 0)
  155. {
  156. sx = 0;
  157. }
  158. int sy = NowH/2 - paintRect.height()/2 - Alloffset.y();
  159. if(sy < 0)
  160. {
  161. sy = 0;
  162. }
  163. int w = (NowW - sx) > paintRect.width() ? paintRect.width(): (NowW - sx);
  164. if(w > (paintRect.width() - x))
  165. {
  166. w = paintRect.width() - x;
  167. }
  168. int h = (NowH - sy) > paintRect.height() ? paintRect.height() : (NowH - sy);
  169. if(h > (paintRect.height() - y))
  170. {
  171. h = paintRect.height() - y;
  172. }
  173. drawRect = QRect(x + paintRect.x(), y + paintRect.y(), w, h);
  174. painter.drawTiledPixmap(drawRect,temp_img,QPoint(sx, sy)); //绘画图片
  175. }

效果:

  1. #include "pictureviewingcontrol.h"
  2. #include <QApplication>
  3. int main(int argc, char *argv[])
  4. {
  5. QApplication a(argc, argv);
  6. PictureViewingControl w;
  7. w.setPixmap(":/backgroundImage1.jpg");
  8. w.show();
  9. return a.exec();
  10. }

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

闽ICP备14008679号