Qt之QCustomPlot范例

Qt之QCustomPlot范例

效果

Demo下载地址

    http://download.csdn.net/download/qq21497936/9964235

背景

   本文使用QCustomPlot做一个QCustomPlot范例,实现一些基本的操作。

QCustomPlot介绍

   QCustomPlot是一个小型的qt画图标类,效果可以,易用,只需要在项目中加入头文件qcustomplot.h和qcustomplot.cpp文件,然后使一个widget提升为QCustomPlot类,即可使用。

   QCustomPlot官网:

      http://www.qcustomplot.com/

   QCustomPlot下载地址:

      http://www.qcustomplot.com/index.php/download

QCustomPlot帮助文件

   QCustomPlot源码包中,带有qt帮助文件,将其添加进qt帮助文件,添加过程如下图:

 

 

   添加完后,即可在qt帮助文件中查看QCustomPlot类相关信息。

 

本人对于QCustomPlot理解

   QCustomPlot是一个二维图表,我们姑且认为其有坐标轴图层和若干其他图层。

坐标轴-图层

   坐标轴图形是横轴和纵轴的图层,可设置各种属性,下表是本Demo使用到的属性设置

 

 
  1. //下标的格式:数字或者时钟

  2. ui->customPlot->xAxis->setTickLabelType(QCPAxis::ltNumber);

  3. //设置时钟下标格式

  4. // ui->customPlot->xAxis->setDateTimeFormat("hh:mm:ss");

  5. ui->customPlot->xAxis->setRange(0,8);

  6. //是否允许自动下标

  7. ui->customPlot->xAxis->setAutoTickStep(false);

  8. //手动设置下标,要先禁止自动下标,隔多少间距显示下标

  9. ui->customPlot->xAxis->setTickStep(1);

  10. //设置steps之间的间隔杠

  11. ui->customPlot->xAxis->setAutoSubTicks(false);

  12. ui->customPlot->xAxis->setSubTickCount(9);//9+1

  13. // ui->customPlot->axisRect()->setupFullAxesBox();

  14. //设置横轴标签

  15. ui->customPlot->xAxis->setLabel("时间(单位s)");

  16. //设置纵轴范围

  17. ui->customPlot->yAxis->setRange(-1,1);

  18. //设置纵轴标签仅使用1个字会报换行符错误后面加个空格

  19. ui->customPlot->yAxis->setLabel("大小");

  20. ui->customPlot->yAxis->setAutoTickStep(false);

  21. ui->customPlot->yAxis->setTickStep(0.2);

  22. ui->customPlot->yAxis->setAutoSubTicks(false);

  23. ui->customPlot->yAxis->setSubTickCount(1);//1+1

若干其他-图层

   使用其他图层之前,必须使用函数addGraph()先添加图层,添加的图层从序列号0开始计层数,使用函数graph()获取指定图层的指针,获取的图层类似于一张图画;

   使用图层指针可以设置画笔setPen()-决定线条的颜色,设置画刷setBrush()-决定其点连成的线到X轴的颜色,实现两条线之间局域用画刷填充,我们需要设置主从图层,从主图层的点画向从图层的点,此时从图层的画刷设置为透明(缺省为透明,若未修改可不设置),然后设置主图层的画刷颜色为我们需要填充的颜色,并使用函数setChannelFillGraph()从使用主图层的画刷画向从图层,从而填充两者点之间的区域。

   在图层上画点,使用addData()函数,图层会将每相邻点之间自动用线调连接起来,当点的数据超出显示范围之后,最好使用removeDataBefore()删除范围外的数据,不然内存将一直增加,QCustomPlot不会自己删除。

关键画点效果实现

 
  1. voidMainWindow::realtimeDataSlot()

  2. {

  3. //每次刷新点的坐标,并且刷新qt图标的界面

  4. doublekey=(double)(_elapsedTimer.elapsed()+_begin)/1000;

  5. //10ms更新一个点

  6. if(key-_lastPointKey>0.01)

  7. {

  8. //sin(key*1.6+cos(key*1.7)*2)*10+sin(key*1.2+0.56)*20+26;

  9. doublevalue0=qSin(key);

  10. //sin(key*1.3+cos(key*1.2)*1.2)*7+sin(key*0.9+0.26)*24+26;

  11. doublevalue1=qCos(key);

  12. if(_start&&!_pause)

  13. {

  14. if(ui->customPlot->graph(0)->visible()&&ui->customPlot->graph(1)->visible())

  15. ui->customPlot->graph(0)->setBrush(QBrush(Qt::yellow));

  16. else

  17. ui->customPlot->graph(0)->setBrush(QBrush(Qt::transparent));

  18. //画线

  19. ui->customPlot->graph(0)->addData(key,value0);

  20. ui->customPlot->graph(1)->addData(key,value1);

  21. //画点

  22. ui->customPlot->graph(2)->clearData();

  23. ui->customPlot->graph(2)->addData(key,value0);

  24. //蓝色图层是否可见

  25. if(ui->customPlot->graph(0)->visible())

  26. {

  27. //统一是x的格式,末尾为0也显示

  28. QStringzzz=QString("%1").arg(key);

  29. for(intindex=zzz.size()-zzz.lastIndexOf(".");index<4;index++)

  30. {

  31. zzz+="0";

  32. }

  33. ui->label_maleX->setText(QString("%1").arg(zzz));

  34. //在msvc2010编译上会出现错误:9个QString::arg匹配和 mid右侧不是联合

  35. // QStringstrValue0=QString("%1").arg(value0,4,10,QChar('0')).mid(0,6);

  36. QStringstrValue0=QString("%1").arg(value0).mid(0,6);

  37. strValue0=

  38. strValue0.startsWith("-")?strValue0.mid(0,6):strValue0.mid(0,5);

  39. ui->label_maleY->setText(strValue0);

  40. }else

  41. {

  42. ui->label_maleX->setText("");

  43. ui->label_maleY->setText("");

  44. }

  45. ui->customPlot->graph(3)->clearData();

  46. ui->customPlot->graph(3)->addData(key,value1);

  47. //红色图层是否可见

  48. if(ui->customPlot->graph(1)->visible())

  49. {

  50. //统一是x的格式,末尾为0也显示

  51. QStringzzz=QString("%1").arg(key);

  52. for(intindex=zzz.size()-zzz.lastIndexOf(".");index<4;index++)

  53. {

  54. zzz+="0";

  55. }

  56. ui->label_femaleX->setText(QString("%1").arg(zzz));

  57. //在msvc2010编译上会出现错误:9个QString::arg匹配和 mid右侧不是联合

  58. // QStringstrValue1=QString("%1").arg(value1,4,10,'0').mid(0,6);

  59. QStringstrValue1=QString("%1").arg(value1).mid(0,6);

  60. strValue1=strValue1.startsWith("-")?strValue1.mid(0,6):strValue1.mid(0,5);

  61. ui->label_femaleY->setText(strValue1);

  62. }else

  63. {

  64. ui->label_femaleX->setText("");

  65. ui->label_femaleY->setText("");

  66. }

  67. //移除图标已逝去的点,避免其占用内存

  68. ui->customPlot->graph(0)->removeDataBefore(key-8);

  69. ui->customPlot->graph(1)->removeDataBefore(key-8);

  70. //rescalevalue(vertical)axistofitthecurrentdata:

  71. //根据该图像最高点和最低点,缩放Y周

  72. // ui->customPlot->graph(0)->rescaleValueAxis();

  73. //根据该图像最高点和最低点,缩放Y周以最后一个为主,所以这里只加入边界,true

  74. // ui->customPlot->graph(1)->rescaleValueAxis(true);

  75. _lastPointKey=key;

  76. }

  77. }

  78. //当显示的X轴超过8的时候,X坐标范围开始移动

  79. if(key>8.0)

  80. {

  81. ui->customPlot->xAxis->setRange(key+0.01,8,Qt::AlignRight);

  82. }

  83. ui->customPlot->replot();

  84.  
  85. //计算每秒的帧数

  86. staticdoublelastFpsKey;

  87. staticintframeCount;

  88. ++frameCount;

  89. //取2秒的平衡均值

  90. if(key-lastFpsKey>2)

  91. {

  92. //设置状态栏显示FPS

  93. ui->statusBar->showMessage(

  94. QString("%1FPS")

  95. .arg(frameCount/(key-lastFpsKey),0,'f',0));

  96. lastFpsKey=key;

  97. frameCount=0;

  98. }

  99. }


 

QCustomPlot官方范例

   QCustomPlot提供了4个例程,这里初入手的特别注意范例plot-examples,其包含了多种效果,修改代码的数字实现不同的demo,如何修改如下图:

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值