#include #include #include #include #include #include &l_qt雷达图">
当前位置:   article > 正文

QT 绘制雷达图_qt雷达图

qt雷达图

RadarWidget.h

  1. #pragma once
  2. #include <QWidget>
  3. #include "ui_RadarWidget.h"
  4. #include <QPainter>
  5. #include <QPaintEvent>
  6. #include <QTimerEvent>
  7. #include <QRect>
  8. #include <QMouseEvent>
  9. #include <iostream>
  10. #include <QKeyEvent>
  11. #include <math.h>
  12. class RadarWidget : public QWidget
  13. {
  14. Q_OBJECT
  15. public:
  16. RadarWidget(QWidget *parent = Q_NULLPTR);
  17. virtual void paintEvent(QPaintEvent* event);
  18. virtual void timerEvent(QTimerEvent* event);
  19. virtual void wheelEvent(QWheelEvent* event);
  20. virtual void mouseMoveEvent(QMouseEvent *event);
  21. virtual void keyPressEvent(QKeyEvent* event);
  22. virtual void resizeEvent(QResizeEvent* event);
  23. //获取角度对应的圆弧点
  24. QPointF trans(int angle);
  25. //获取中心点
  26. QPointF GetCentrePT();
  27. //获取矩形
  28. QRect GetCenterRect(QPointF point);
  29. QPointF CustomRotate(QPointF point, qreal from_angle, qreal rotate);
  30. private:
  31. //缩放系数
  32. float m_zoom;
  33. //widget的尺寸
  34. QRect _rect;
  35. //记录绘图中心点
  36. QPoint m_point;
  37. //扇区渐变角度
  38. int m_Angle;
  39. //扇区每次移动角度
  40. int Speed;
  41. int m_nRadius;
  42. int m_iRet;
  43. Ui::RadarWidgetClass ui;
  44. };

RadarWidget.cpp

  1. #include "RadarWidget.h"
  2. #define PI 3.1415926
  3. RadarWidget::RadarWidget(QWidget *parent)
  4. : QWidget(parent)
  5. {
  6. ui.setupUi(this);
  7. m_zoom = 1.0;
  8. Speed = 1;
  9. _rect = this->rect();
  10. m_point.setX(_rect.width() / 2);
  11. m_point.setY(_rect.height() / 2);
  12. m_Angle = 0;
  13. //开启鼠标追踪
  14. setMouseTracking(true);
  15. this->startTimer(100);
  16. }
  17. void RadarWidget::paintEvent(QPaintEvent* event)
  18. {
  19. QPainter painter(this);
  20. painter.setPen(QColor(Qt::white));
  21. //开启反锯齿
  22. painter.setRenderHint(QPainter::Antialiasing, true);
  23. //将中心位置设置为坐标原点
  24. painter.translate(m_point.x(),m_point.y());
  25. //最小半径
  26. int iRet = _rect.height()/2/3*m_zoom;
  27. m_iRet = iRet;
  28. //画刻度
  29. QPoint line1;
  30. QPoint line2;
  31. line1.setX(0);
  32. line1.setY(0);
  33. line2.setX(0);
  34. line2.setY(-iRet * 3);
  35. //画线
  36. QLineF n_line(line1, line2);
  37. for (int i = 0; i < 12; i++)
  38. {
  39. n_line.setAngle(i * 30.0);
  40. painter.drawLine(n_line);
  41. }
  42. //画角度
  43. for (int i = 0; i < 12; i++)
  44. {
  45. int drawangel = i * 30;
  46. //角度转弧度
  47. QPointF pt = trans(drawangel);
  48. // painter.drawLine(GetCentrePT(), pt);
  49. QString strText = QString::number(drawangel); //设置当前字体
  50. painter.save();
  51. //1】偏移坐标原点到需要绘制刻度的位置
  52. painter.translate(pt);
  53. //2】旋转指定的角度
  54. painter.rotate(drawangel);
  55. //3】 画刻度
  56. painter.drawText(GetCentrePT().x(), GetCentrePT().y(),strText);
  57. painter.restore();
  58. }
  59. //画刻度 360°
  60. for (int i = 0; i < 360; i++)
  61. {
  62. QPointF optintold;
  63. optintold.setX(cos(PI * 2 / 360 * i) * m_iRet * 3);
  64. optintold.setY(sin(PI * 2 / 360 * i) * m_iRet * 3);
  65. QPointF optintnew;
  66. int nes = m_iRet * 3 -10;
  67. optintnew.setX(cos(PI * 2 / 360 * i) * nes);
  68. optintnew.setY(sin(PI * 2 / 360 * i) * nes);
  69. painter.drawLine(optintnew, optintold);
  70. }
  71. //画同心圆 及 量程
  72. for (int i = 0; i < 4; i++)
  73. {
  74. QPoint point;
  75. point.setX(0);
  76. point.setY(0);
  77. painter.drawEllipse(point,
  78. iRet*i, iRet * i);
  79. painter.drawText(GetCentrePT().x(), GetCentrePT().y()-iRet * i, QString::number(i*50));
  80. }
  81. //圆锥渐变
  82. // QConicalGradient ConicaGrad(0,0,0);
  83. // QColor i_color = QColor(14,255,11);
  84. // i_color.setAlpha(100);
  85. // ConicaGrad.setColorAt(0, i_color);
  86. // i_color.setAlpha(50);
  87. // ConicaGrad.setColorAt(0.1, i_color);
  88. // i_color.setAlpha(0);
  89. // ConicaGrad.setColorAt(1, i_color);
  90. // painter.setBrush(QBrush(ConicaGrad));
  91. QRect ReFar(-_rect.height() / 2 * m_zoom, -_rect.height() / 2 * m_zoom, _rect.height() * m_zoom, _rect.height() * m_zoom);
  92. painter.setBrush(QColor(84, 135, 29,90));
  93. painter.drawPie(ReFar,m_Angle * 16, 70*16);
  94. }
  95. void RadarWidget::timerEvent(QTimerEvent* event)
  96. {
  97. m_Angle += Speed;
  98. if (m_Angle > 360)
  99. m_Angle = 0;
  100. update();
  101. }
  102. void RadarWidget::wheelEvent(QWheelEvent* event)
  103. {
  104. if (event->delta() > 0)
  105. {
  106. m_zoom += 0.1;
  107. }
  108. else
  109. {
  110. m_zoom -= 0.1;
  111. }
  112. update();
  113. }
  114. void RadarWidget::mouseMoveEvent(QMouseEvent* event)
  115. {
  116. //判断是否为左键按下
  117. if (Qt::LeftButton & event->buttons())
  118. {
  119. m_point.setX(event->pos().x());
  120. m_point.setY(event->pos().y());
  121. }
  122. }
  123. void RadarWidget::keyPressEvent(QKeyEvent* event)
  124. {
  125. //TODO:控制雷达旋转速度 <- 速度+ -> 速度-
  126. if (Qt::Key_Left == event->key())
  127. {
  128. if (Speed >= 10)
  129. Speed = 10;
  130. Speed++;
  131. }
  132. else if (Qt::Key_Right == event->key())
  133. {
  134. if (Speed < 1)
  135. Speed = 1;
  136. Speed--;
  137. }
  138. }
  139. void RadarWidget::resizeEvent(QResizeEvent* event)
  140. {
  141. m_point.setX(event->size().width() / 2);
  142. m_point.setY(event->size().height() / 2);
  143. m_nRadius = (event->size().height() - 50) / 2;
  144. }
  145. QPointF RadarWidget::trans(int angle)
  146. {
  147. QPointF ptResult(GetCentrePT());
  148. //[角度转弧度]
  149. //float rad = qDegreesToRadians(static_cast<float>(angle));
  150. float rad = (float)angle / 180 * PI;
  151. ptResult.setX(GetCentrePT().x() + m_iRet * 3 *sin(static_cast<qreal>(rad)));
  152. ptResult.setY(GetCentrePT().y() - m_iRet * 3 *cos(static_cast<qreal>(rad)));
  153. return ptResult;
  154. }
  155. QPointF RadarWidget::GetCentrePT()
  156. {
  157. QPointF pointf;
  158. pointf.setX(0);
  159. pointf.setY(0);
  160. return pointf;
  161. }
  162. QRect RadarWidget::GetCenterRect(QPointF point)
  163. {
  164. QRect rect;
  165. rect.setX(point.x());
  166. rect.setY(point.y());
  167. rect.setWidth(50);
  168. rect.setHeight(50);
  169. return rect;
  170. }
  171. QPointF RadarWidget::CustomRotate(QPointF point, qreal from_angle, qreal rotate)
  172. {
  173. QPointF Tmp;
  174. qreal arc = (rotate - from_angle) / 180 * PI;
  175. qreal Length = sqrt(point.x() * point.x() + point.y() * point.y());
  176. Tmp.setX(Length * cos(arc));
  177. Tmp.setY(Length * sin(arc));
  178. return Tmp.toPoint();
  179. }

 

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