QCustomPlot实现实时动态曲线

10 篇文章 3 订阅
1 篇文章 0 订阅


原文地址:http://blog.chinaunix.net/uid-11829250-id-5750296.html

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代码如下:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTimer>
#include "qcustomplot.h"

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

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


};

#endif // MAINWINDOW_H

mainwindow.cpp代码如下:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QVector>
#include <QTimer>
#include <QTime>



MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    setupRealtimeDataDemo(ui->customPlot);
    ui->customPlot->replot();

    ui->checkBox_temp->setChecked(true);
    ui->checkBox_hui->setChecked(true);
}

MainWindow::~MainWindow()
{
    delete ui;
}

//画图初始化
void MainWindow::setupRealtimeDataDemo(QCustomPlot *customPlot)
{
//#if QT_VERSION < QT_VERSION_CHECK(4, 7, 0)
  //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");
//#endif
  //demoName = "Real Time Data Demo";

  // include this section to fully disable antialiasing for higher performance:
  /*
  customPlot->setNotAntialiasedElements(QCP::aeAll);
  QFont font;
  font.setStyleStrategy(QFont::NoAntialias);
  customPlot->xAxis->setTickLabelFont(font);
  customPlot->yAxis->setTickLabelFont(font);
  customPlot->legend->setFont(font);
  */
  customPlot->addGraph(); // blue line
  customPlot->graph(0)->setPen(QPen(Qt::blue));
  customPlot->graph(0)->setName("temp");
  //customPlot->graph(0)->setBrush(QBrush(QColor(240, 255, 200)));
  //customPlot->graph(0)->setAntialiasedFill(false);
  customPlot->addGraph(); // red line
  customPlot->graph(1)->setPen(QPen(Qt::red));
  customPlot->graph(1)->setName("hui");
  //customPlot->graph(0)->setChannelFillGraph(customPlot->graph(1));


  customPlot->xAxis->setTickLabelType(QCPAxis::ltDateTime);
  customPlot->xAxis->setDateTimeFormat("hh:mm:ss");
  customPlot->xAxis->setAutoTickStep(false);
  customPlot->xAxis->setTickStep(2);
  customPlot->axisRect()->setupFullAxesBox();

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

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



}

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

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

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

  • 1
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
QCustomPlot 是一个用于绘制曲线图和其他图形的第三方库。要实现实时曲线,你可以按照以下步骤操作: 1. 在你的项目中引入 QCustomPlot 库,并确保正确配置了编译环境。 2. 创建一个 QCustomPlot 实例,并将其添加到你的界面中。 3. 添加一个 QCPGraph 对象到 QCustomPlot 实中,用于绘制曲线。 4. 创建一个定时器,用于定更新曲线数据。 5. 在定时器的触发事件中,更新曲线数据,然后重新绘制曲线。 下面是一个简单的示例代码,展示了如何实现实时曲线的功能: ```cpp #include <QApplication> #include <QTimer> #include "qcustomplot.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); // 创建 QCustomPlot 实例 QCustomPlot customPlot; // 添加 QCPGraph 对象到 QCustomPlot 实例中 QCPGraph *graph = customPlot.addGraph(); // 创建定时器 QTimer timer; // 设置定时器的定时间隔(以毫秒为单位) timer.setInterval(1000); // 连接定时器的触发事件到更新曲线数据的槽函数 QObject::connect(&timer, &QTimer::timeout, [&]() { // 更新曲线数据 QVector<double> xData, yData; // 假设获取最新的曲线数据并保存到 xData 和 yData 中 // ... // 清除原有的数据 graph->data()->clear(); // 添加新的数据点 for (int i = 0; i < xData.size(); ++i) { graph->addData(xData[i], yData[i]); } // 重新绘制曲线 customPlot.replot(); }); // 启动定时器 timer.start(); // 显示界面 customPlot.show(); return a.exec(); } ``` 你可以根据自己的实际需求进行修改和扩展。希望对你有帮助!
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值