Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1308124
  • 博文数量: 124
  • 博客积分: 5772
  • 博客等级: 大校
  • 技术积分: 1647
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-27 10:39
文章分类

全部博文(124)

文章存档

2020年(1)

2019年(1)

2018年(5)

2017年(2)

2016年(17)

2015年(3)

2014年(7)

2013年(11)

2012年(13)

2011年(30)

2010年(34)

分类: LINUX

2016-08-23 11:35:28

Qt4中,可以使用QCustompPlot来绘制曲线,QCustompPlot是一个第三方工具,可以到官网下载:http://www.qcustomplot.com/index.php/download
这里实现一个实时动态曲线图,用随机数作为实时数据,程序运行结果如下:

主机环境:fedora9,Qt4.7,Qtcreator 2.0.1
使用Qtcreator 2.0.1新建一个工程,基类模板选择QMainWindow。
将解压得到的QCustompPlot文件夹里面的头文件qcustomplot.h和源文件qcustomplot.cpp复制粘贴到工程文件夹下。Qtcreator中,对着工程名右键,添加已有文件,将头文件qcustomplot.h和源文件qcustomplot.cpp都添加到工程中来。
在界面上拖拽一个widget部件,然后升级成Qcustomplot,(参考:)部件名称改为customPlot
mainwindow.h代码如下:

  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H

  3. #include <QMainWindow>
  4. #include <QTimer>
  5. #include "qcustomplot.h"

  6. namespace Ui {
  7.     class MainWindow;
  8. }

  9. class MainWindow : public QMainWindow
  10. {
  11.     Q_OBJECT

  12. public:
  13.     explicit MainWindow(QWidget *parent = 0);
  14.     ~MainWindow();
  15.     //设置qcustomplot画图属性,实时
  16.     void setupRealtimeDataDemo(QCustomPlot *customPlot);
  17. private slots:
  18.     //添加实时数据槽
  19.     void realtimeDataSlot();

  20. private:
  21.     Ui::MainWindow *ui;
  22.     //定时器,周期调用realtimeDataSlot()槽,实现动态数据添加到曲线
  23.     QTimer dataTimer;


  24. };

  25. #endif // MAINWINDOW_H
mainwindow.cpp代码如下:

  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include <QVector>
  4. #include <QTimer>
  5. #include <QTime>



  6. MainWindow::MainWindow(QWidget *parent) :
  7.     QMainWindow(parent),
  8.     ui(new Ui::MainWindow)
  9. {
  10.     ui->setupUi(this);

  11.     setupRealtimeDataDemo(ui->customPlot);
  12.     ui->customPlot->replot();

  13.     ui->checkBox_temp->setChecked(true);
  14.     ui->checkBox_hui->setChecked(true);
  15. }

  16. MainWindow::~MainWindow()
  17. {
  18.     delete ui;
  19. }

  20. //画图初始化
  21. void MainWindow::setupRealtimeDataDemo(QCustomPlot *customPlot)
  22. {
  23. //#if QT_VERSION < QT_VERSION_CHECK(4, 7, 0)
  24.   //QMessageBox::critical(this, "", "You're using Qt < 4.7, the realtime data demo needs functions that are available with Qt 4.7 to work properly");
  25. //#endif
  26.   //demoName = "Real Time Data Demo";

  27.   // include this section to fully disable antialiasing for higher performance:
  28.   /*
  29.   customPlot->setNotAntialiasedElements(QCP::aeAll);
  30.   QFont font;
  31.   font.setStyleStrategy(QFont::NoAntialias);
  32.   customPlot->xAxis->setTickLabelFont(font);
  33.   customPlot->yAxis->setTickLabelFont(font);
  34.   customPlot->legend->setFont(font);
  35.   */
  36.   customPlot->addGraph(); // blue line
  37.   customPlot->graph(0)->setPen(QPen(Qt::blue));
  38.   customPlot->graph(0)->setName("temp");
  39.   //customPlot->graph(0)->setBrush(QBrush(QColor(240, 255, 200)));
  40.   //customPlot->graph(0)->setAntialiasedFill(false);
  41.   customPlot->addGraph(); // red line
  42.   customPlot->graph(1)->setPen(QPen(Qt::red));
  43.   customPlot->graph(1)->setName("hui");
  44.   //customPlot->graph(0)->setChannelFillGraph(customPlot->graph(1));


  45.   customPlot->xAxis->setTickLabelType(QCPAxis::ltDateTime);
  46.   customPlot->xAxis->setDateTimeFormat("hh:mm:ss");
  47.   customPlot->xAxis->setAutoTickStep(false);
  48.   customPlot->xAxis->setTickStep(2);
  49.   customPlot->axisRect()->setupFullAxesBox();

  50.   // make left and bottom axes transfer their ranges to right and top axes:
  51.   //connect(customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->xAxis2, SLOT(setRange(QCPRange)));
  52.   //connect(customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->yAxis2, SLOT(setRange(QCPRange)));

  53.   // setup a timer that repeatedly calls MainWindow::realtimeDataSlot:
  54.   connect(&dataTimer, SIGNAL(timeout()), this, SLOT(realtimeDataSlot()));
  55.   dataTimer.start(0); // Interval 0 means to refresh as fast as possible
  56.   customPlot->legend->setVisible(true);



  57. }

  58. void MainWindow::realtimeDataSlot()
  59. {
  60.     //key的单位是秒
  61.     double key = QDateTime::currentDateTime().toMSecsSinceEpoch()/1000.0;
  62.     qsrand(QTime::currentTime().msec() + QTime::currentTime().second() * 10000);
  63.     //使用随机数产生两条曲线
  64.     double value0 = qrand() % 100;
  65.     double value1 = qrand() % 80;
  66.     if (ui->checkBox_temp->isChecked())
  67.         ui->customPlot->graph(0)->addData(key, value0);//添加数据1到曲线1
  68.     if (ui->checkBox_hui->isChecked())
  69.         ui->customPlot->graph(1)->addData(key, value1);//添加数据2到曲线2
  70.     //删除8秒之前的数据。这里的8要和下面设置横坐标宽度的8配合起来
  71.     //才能起到想要的效果,可以调整这两个值,观察显示的效果。
  72.     ui->customPlot->graph(0)->removeDataBefore(key-8);
  73.     ui->customPlot->graph(1)->removeDataBefore(key-8);

      //自动设定graph(1)曲线y轴的范围,如果不设定,有可能看不到图像
//也可以用ui->customPlot->yAxis->setRange(up,low)手动设定y轴范围
  1.     ui->customPlot->graph(0)->rescaleValueAxis();
  2.     ui->customPlot->graph(1)->rescaleValueAxis(true);   

  3.     //这里的8,是指横坐标时间宽度为8秒,如果想要横坐标显示更多的时间
  4.     //就把8调整为比较大到值,比如要显示60秒,那就改成60。
  5.     //这时removeDataBefore(key-8)中的8也要改成60,否则曲线显示不完整。
  6.     ui->customPlot->xAxis->setRange(key+0.25, 8, Qt::AlignRight);//设定x轴的范围
  7.     ui->customPlot->replot();
  8. }



阅读(53580) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

mythas2018-09-11 16:42:25

博主, double key = QDateTime::currentDateTime().toMSecsSinceEpoch()/1000.0;这句获得的是现在的时间到epoch time的时间间隔,显示的时候怎么只显示当天的时分秒呢