QCustomPlot实现实时动态曲线(包含手动设置XY轴显示的方法)

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,(参考:http://www.bubuko.com/infodetail-744744.html)部件名称改为customPlot
mainwindow.h代码如下:
 

  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3.  
  4. #include <QMainWindow>
  5. #include <QTimer>
  6. #include "qcustomplot.h"
  7.  
  8. namespace Ui {
  9.     class MainWindow;
  10. }
  11.  
  12. class MainWindow : public QMainWindow
  13. {
  14.     Q_OBJECT
  15.  
  16. public:
  17.     explicit MainWindow(QWidget *parent = 0);
  18.     ~MainWindow();
  19.     //设置qcustomplot画图属性,实时
  20.     void setupRealtimeDataDemo(QCustomPlot *customPlot);
  21. private slots:
  22.     //添加实时数据槽
  23.     void realtimeDataSlot();
  24.  
  25. private:
  26.     Ui::MainWindow *ui;
  27.     //定时器,周期调用realtimeDataSlot()槽,实现动态数据添加到曲线
  28.     QTimer dataTimer;
  29.  
  30.  
  31. };
  32.  
  33. #endif // MAINWINDOW_H

mainwindow.cpp代码如下:
 

  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include <QVector>
  4. #include <QTimer>
  5. #include <QTime>
  6.  
  7.  
  8.  
  9. MainWindow::MainWindow(QWidget *parent) :
  10.     QMainWindow(parent),
  11.     ui(new Ui::MainWindow)
  12. {
  13.     ui->setupUi(this);
  14.  
  15.     setupRealtimeDataDemo(ui->customPlot);
  16.     ui->customPlot->replot();
  17.  
  18.     ui->checkBox_temp->setChecked(true);
  19.     ui->checkBox_hui->setChecked(true);
  20. }
  21.  
  22. MainWindow::~MainWindow()
  23. {
  24.     delete ui;
  25. }
  26.  
  27. //画图初始化
  28. void MainWindow::setupRealtimeDataDemo(QCustomPlot *customPlot)
  29. {
  30. //#if QT_VERSION < QT_VERSION_CHECK(4, 7, 0)
  31.   //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");
  32. //#endif
  33.   //demoName = "Real Time Data Demo";
  34.  
  35.   // include this section to fully disable antialiasing for higher performance:
  36.   /*
  37.   customPlot->setNotAntialiasedElements(QCP::aeAll);
  38.   QFont font;
  39.   font.setStyleStrategy(QFont::NoAntialias);
  40.   customPlot->xAxis->setTickLabelFont(font);
  41.   customPlot->yAxis->setTickLabelFont(font);
  42.   customPlot->legend->setFont(font);
  43.   */
  44.   customPlot->addGraph(); // blue line
  45.   customPlot->graph(0)->setPen(QPen(Qt::blue));
  46.   customPlot->graph(0)->setName("temp");
  47.   //customPlot->graph(0)->setBrush(QBrush(QColor(240, 255, 200)));
  48.   //customPlot->graph(0)->setAntialiasedFill(false);
  49.   customPlot->addGraph(); // red line
  50.   customPlot->graph(1)->setPen(QPen(Qt::red));
  51.   customPlot->graph(1)->setName("hui");
  52.   //customPlot->graph(0)->setChannelFillGraph(customPlot->graph(1));
  53.  
  54.  
  55.   customPlot->xAxis->setTickLabelType(QCPAxis::ltDateTime);
  56.   customPlot->xAxis->setDateTimeFormat("hh:mm:ss");
  57.   customPlot->xAxis->setAutoTickStep(false);
  58.   customPlot->xAxis->setTickStep(2);
  59.   customPlot->axisRect()->setupFullAxesBox();
  60.  
  61.   // make left and bottom axes transfer their ranges to right and top axes:
  62.   //connect(customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->xAxis2, SLOT(setRange(QCPRange)));
  63.   //connect(customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->yAxis2, SLOT(setRange(QCPRange)));
  64.  
  65.   // setup a timer that repeatedly calls MainWindow::realtimeDataSlot:
  66.   connect(&dataTimer, SIGNAL(timeout()), this, SLOT(realtimeDataSlot()));
  67.   dataTimer.start(0); // Interval 0 means to refresh as fast as possible
  68.   customPlot->legend->setVisible(true);
  69.  
  70.  
  71.  
  72. }
  73.  
  74. void MainWindow::realtimeDataSlot()
  75. {
  76.     //key的单位是秒
  77.     double key = QDateTime::currentDateTime().toMSecsSinceEpoch()/1000.0;
  78.     qsrand(QTime::currentTime().msec() + QTime::currentTime().second() * 10000);
  79.     //使用随机数产生两条曲线
  80.     double value0 = qrand() % 100;
  81.     double value1 = qrand() % 80;
  82.     if (ui->checkBox_temp->isChecked())
  83.         ui->customPlot->graph(0)->addData(key, value0);//添加数据1到曲线1
  84.     if (ui->checkBox_hui->isChecked())
  85.         ui->customPlot->graph(1)->addData(key, value1);//添加数据2到曲线2
  86.     //删除8秒之前的数据。这里的8要和下面设置横坐标宽度的8配合起来
  87.     //才能起到想要的效果,可以调整这两个值,观察显示的效果。
  88.     ui->customPlot->graph(0)->removeDataBefore(key-8);
  89.     ui->customPlot->graph(1)->removeDataBefore(key-8);
  90.  

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

  1.     ui->customPlot->graph(0)->rescaleValueAxis();
  2.     ui->customPlot->graph(1)->rescaleValueAxis(true);   
  3.  
  4.     //这里的8,是指横坐标时间宽度为8秒,如果想要横坐标显示更多的时间
  5.     //就把8调整为比较大到值,比如要显示60秒,那就改成60。
  6.     //这时removeDataBefore(key-8)中的8也要改成60,否则曲线显示不完整。
  7.     ui->customPlot->xAxis->setRange(key+0.25, 8, Qt::AlignRight);//设定x轴的范围
  8.     ui->customPlot->replot();
  9. }
发布了27 篇原创文章 · 获赞 6 · 访问量 5066

猜你喜欢

转载自blog.csdn.net/qq_21449473/article/details/102987021