Qt绘图

 

2018年06月22日 18:04:34 逸璞丷昊-承接本科毕设、价格私聊 阅读数:7336更多

所属专栏: Qt的学习之旅

版权声明:- - 内容若有错误,请您务必指出,感谢让我提高并给予我建议的你! - - 转载请注明出处 https://blog.csdn.net/yishuicanhong/article/details/80738361

       本来是想写一篇上位机通过串口接收数据并进行简单实时动态画图的,但是网上关于Qt串口接收数据的博客已经非常多了,且也会导致内容太多,显得复杂,如果需要串口收发数据例程,网上随便百度就能收到了,也可以下载我写的北斗/GPS双模定位上位机,这里主要介绍一下收到数据后的画图实现。
       使用Qt绘制图形,首先需要了解Qt的窗口与视口的概念,理解Qt的坐标变换,然后学会掌握Qpainter这个类的使用就能实现基本的绘图功能了。下面一个实时绘制曲线图形的例子(坐标用应该直接使用一个Qpoint模板的列表表示比较好)。这里,实时数据只是由使用creatData函数产生,在实际工程中将数据改为读取实时数据即可。

头文件

 
  1. #ifndef WIDGET_H

  2. #define WIDGET_H

  3.  
  4. #include <QWidget>

  5. #include <QTimer>

  6. #include <QList>

  7.  
  8. class Widget : public QWidget

  9. {

  10. Q_OBJECT

  11.  
  12. protected:

  13. void paintEvent(QPaintEvent *);

  14. public:

  15. int m_count;

  16. float x;

  17. QTimer* m_Timer;

  18. QList<float> xList;

  19. QList<float> yList;

  20.  
  21. Widget(QWidget *parent = 0);

  22. ~Widget();

  23.  
  24. public slots:

  25. void creatData();

  26.  
  27. };

  28.  
  29. #endif // WIDGET_H

源文件

 
  1. #include "Widget.h"

  2. #include <QPainter>

  3. #include <QPointF>

  4. #include <QPen>

  5. #include <qmath.h>

  6. Widget::Widget(QWidget *parent) : QWidget(parent)

  7. {

  8. x = -10;

  9. m_count = 0;

  10. m_Timer = new QTimer(this);

  11. m_Timer->start(10);

  12. connect(m_Timer,SIGNAL(timeout()),this,SLOT(creatData()));

  13. }

  14. void Widget::creatData()

  15. {

  16. x += 0.2;

  17. m_count += 1;

  18. if(m_count%2)

  19. {

  20. xList.append(x);

  21. yList.append(qSin(x));

  22. }

  23. else

  24. {

  25. xList.append(x);

  26. yList.append(qSin(x));

  27. update();

  28. }

  29. if(m_count == 100)

  30. {

  31. xList.clear();

  32. yList.clear();

  33. m_count = 0;

  34. x = -10;

  35. }

  36. }

  37. void Widget::paintEvent(QPaintEvent *)

  38. {

  39. QPainter painter(this);

  40. QPen pen;

  41. pen.setColor(Qt::green);

  42. pen.setStyle(Qt::SolidLine);

  43. pen.setWidthF(0.05);

  44. painter.setPen(pen);

  45. painter.setViewport(50, 50, width()-100, height()-100);

  46. painter.setWindow(-10, 2, 20, -4); // (-10, 2) (10, -2)

  47. painter.fillRect(-10, 2, 20, -4, Qt::white);

  48. painter.drawLine(QPointF(-10, 0), QPointF(10, 0)); // x

  49. painter.drawLine(QPointF(0, 2), QPointF(0, -2)); // y

  50. for(int i = 0; i < yList.count(); i++)

  51. {

  52. if(i == 0)

  53. painter.drawPoint(QPointF(xList[i], yList[i]));

  54. else

  55. painter.drawLine(QPointF(xList[i-1], yList[i-1]), QPointF(xList[i], yList[i]));

  56. }

  57. }

  58. Widget::~Widget()

  59. {

  60.  
  61. }

主函数

 
  1. #include <QApplication>

  2. #include "Widget.h"

  3. int main(int argc, char *argv[])

  4. {

  5. QApplication a(argc, argv);

  6. Widget w;

  7. w.show();

  8.  
  9. return a.exec();

  10.  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值