Qt画实时曲线图

Qt引入QcustomPlot

首先下载QcustomPlot源代码,https://github.com/qcustomplot/qcustomplot

下载zip文件

运行所下载的项目生成库文件libqcustomplotd2.a文件和qcustomplotd2.dll文件。

在项目中添加printsupport。

并将qcustomplot.h文件和qcustomplot.cpp文件添加到项目,另外将库文件复制到项目中。

在项目ui 中拖出一个widget并提升为QCustomPlot接下来我们引入customplot.h文件进行画图。

cpp 复制代码
ui->customPlot->xAxis->setVisible(true); // 隐藏X轴
        ui->customPlot->yAxis->setVisible(true); // 隐藏Y轴
        ui->customPlot->xAxis->setTickLabels(true); // 隐藏X轴的刻度标签
        ui->customPlot->yAxis->setTickLabels(true); // 隐藏Y轴的刻度标签

        ui->customPlot->legend->setVisible(true);
        QSharedPointer<QCPAxisTickerDateTime> timeTicker(new QCPAxisTickerDateTime);
        //ui->customPlot->xAxis->setTickLabelType(QCPAxis::ltDateTime);
        timeTicker->setDateTimeFormat("yyyy-MM-dd HH:mm:ss"); // 设置时间格式
        ui->customPlot->xAxis->setTicker(timeTicker);
        ui->customPlot->graph(0)->setData(timee, i);
        ui->customPlot->graph(0)->setName("电流曲线");
        //ui->customPlot->graph(0)->setWidth(5);

        ui->customPlot->graph(1)->setData(timee, v);
        ui->customPlot->graph(1)->setName("电压曲线");
        ui->customPlot->rescaleAxes();
        ui->customPlot->xAxis->setLabel("时间轴");
        ui->customPlot->yAxis->setRange(0,150);
        ui->customPlot->replot();


        QObject::connect(ui->customPlot, &QCustomPlot::mousePress, [&](QMouseEvent *event)
        {

            // 当前鼠标位置(像素坐标)

            int x_pos = event->pos().x();
            int y_pos = event->pos().y();

            //鼠标坐标转化为CustomPlot内部坐标
            int x_val = ui->customPlot->xAxis->pixelToCoord(x_pos);
            int y_val = ui->customPlot->yAxis->pixelToCoord(y_pos);

            QString str,strToolTip;
            str = QString::number(x_val);
            strToolTip += "时间: ";
            strToolTip += QDateTime::fromTime_t(x_val).toString(
                        "yyyy-MM-dd hh:mm:ss");
            strToolTip += "\n";

            int index=keyIndexMap[x_val];
            //qDebug()<<x_val<<index;
            int y=ui->customPlot->graph(0)->data()->at(index)->value;
            str = QString::number(y);
            strToolTip += "电流:";
            strToolTip += str;
            strToolTip += "\n";
            //qDebug()<<str;
            int y1=ui->customPlot->graph(1)->data()->at(index)->value;
            str = QString::number(y1);
            strToolTip += "电压:";
            strToolTip += str;
            strToolTip += "\n";

            QToolTip::showText(cursor().pos(), strToolTip, ui->customPlot);
        });

    }

上面显示的是曲线

timee,i,v是vector数组,timee是

复制代码
timee[j]=time[j].toTime_t();

日期时间转换的毫秒数通过以下代码

复制代码
QSharedPointer<QCPAxisTickerDateTime> timeTicker(new QCPAxisTickerDateTime);
复制代码
        //ui->customPlot->xAxis->setTickLabelType(QCPAxis::ltDateTime);
复制代码
        timeTicker->setDateTimeFormat("yyyy-MM-dd HH:mm:ss"); // 设置时间格式
复制代码
        ui->customPlot->xAxis->setTicker(timeTicker);

实现在折线图中显示出日期时间。

如何显示实时曲线呢?

cpp 复制代码
timer=new QTimer(this);
    timer->setTimerType(Qt::PreciseTimer);
    timer->start(1000);
    connect(timer,&QTimer::timeout,this,&Dialog::update);

通过定时器每隔1000ms调用update函数,上面的代码就是update函数中的部分代码。

上面代码也包括了点击按钮出现时间轴和电压,电流的数据。

cpp 复制代码
QObject::connect(ui->customPlot, &QCustomPlot::mousePress, [&](QMouseEvent *event)
        {

            // 当前鼠标位置(像素坐标)

            int x_pos = event->pos().x();
            int y_pos = event->pos().y();

            //鼠标坐标转化为CustomPlot内部坐标
            int x_val = ui->customPlot->xAxis->pixelToCoord(x_pos);
            int y_val = ui->customPlot->yAxis->pixelToCoord(y_pos);

            QString str,strToolTip;
            str = QString::number(x_val);
            strToolTip += "时间: ";
            strToolTip += QDateTime::fromTime_t(x_val).toString(
                        "yyyy-MM-dd hh:mm:ss");
            strToolTip += "\n";

            int index=keyIndexMap[x_val];
            //qDebug()<<x_val<<index;
            int y=ui->customPlot->graph(0)->data()->at(index)->value;
            str = QString::number(y);
            strToolTip += "电流:";
            strToolTip += str;
            strToolTip += "\n";
            //qDebug()<<str;
            int y1=ui->customPlot->graph(1)->data()->at(index)->value;
            str = QString::number(y1);
            strToolTip += "电压:";
            strToolTip += str;
            strToolTip += "\n";

            QToolTip::showText(cursor().pos(), strToolTip, ui->customPlot);
        });

我们通过lambda公式实现鼠标点击事件。

复制代码
ui->customPlot->graph(0)->data()->at(index)->value

index是指曲线图的第几个数据,所以我们需要keyIndexMap将横坐标时间轴转换成整形数据找到是第几个点,进而获取曲线上的数值,俗称为y轴的数值。通过这样转换我们可以通过x轴时间获取y轴的数据,最后通过QToolTip显示到曲线中。

本文只是自己做的项目一个模块,就不给大家更多的代码。本文实现的是,实时曲线图,X轴是时间轴Y轴是电压和电流。

实现图像:

相关推荐
四维碎片1 天前
【Qt】UDP跨平台调试工具
qt·学习·udp
踏过山河,踏过海1 天前
【用ui文件做个简单工具的开发,为什么修改完ui后,程序重新编译运行后,GUI界面还是不变呢?】
qt·ui
向阳开的夏天1 天前
麒麟V10源码编译QT5.6.3 (x86 & arm64)
开发语言·qt
打码的猿1 天前
Qt对话框不锁死主程序的方法
开发语言·qt
小小码农Come on1 天前
Qt Creator常用设置
qt
wkm9561 天前
在arm64 ubuntu系统安装Qt后编译时找不到Qt3DExtras头文件
开发语言·arm开发·qt
小小码农Come on2 天前
QT开发环境安装
开发语言·qt
小小码农Come on2 天前
QT内存管理
开发语言·qt
有理想的打工人2 天前
QT的安装
qt
SilentSlot2 天前
【QT-QML】8. 输入元素
qt·qml