使用QT绘制简单的动态数据折线图

两个核心类时QChart和QLineSeries

下面这个示例代码中,定时器每隔一段时间将曲线图中的数据点向右移动 一个单位,同时调整横坐标轴的范围,实现了一次滚动对应移动一个数据点的效果。

QLineSeries最多容纳4096+1024个点

cpp 复制代码
#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include <QtCharts/QChartView>
#include <QtCharts/QLineSeries>
#include <QtCharts/QValueAxis>
#include <QtCore/QRandomGenerator>
#include <QtCore/QTimer>
#include <QDebug>

QT_CHARTS_USE_NAMESPACE

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QMainWindow window;
    QChartView *chartView = new QChartView(&window);
    window.setCentralWidget(chartView);
    QChart *chart = new QChart();
    chart->legend()->hide();
    chartView->setChart(chart);
    chart->createDefaultAxes();

    QLineSeries *series = new QLineSeries();
    chart->addSeries(series);
    chart->createDefaultAxes();


    QValueAxis *axisX = qobject_cast<QValueAxis *>(chart->axes(Qt::Horizontal).at(0));
    QValueAxis *axisY = qobject_cast<QValueAxis *>(chart->axes(Qt::Vertical).at(0));

    axisY->setRange(0, 120);

    // 初始化数据点
    qreal startX = 0;
    for (int i = 0; i < 100; ++i) {
        series->append(startX + i, QRandomGenerator::global()->bounded(100));
    }

    int currentIndex = 0; // 当前数据点索引

    QTimer timer;
    timer.setInterval(100);
    QObject::connect(&timer, &QTimer::timeout, [&](){
        if (series->points().size() >= 4096 + 1024)
        {
            int n = series->points().size() - 4096;
            series->removePoints(0, n);
            currentIndex-= n;
            qDebug() << "remove " << n;
        }

        qDebug() << currentIndex;
        // 移动数据点
        //series->remove(0);
        series->append(series->points().last().x() + 1, QRandomGenerator::global()->bounded(100));

        // 计算滚动后的横坐标范围
        qreal minX = series->points().at(currentIndex).x();
        qreal maxX = series->points().last().x();
        qreal targetMinX = minX + 1;
        qreal targetMaxX = maxX + 1;
        axisX->setRange(targetMinX, targetMaxX);
        currentIndex++;

    });

    timer.start();

    window.resize(800, 600);
    window.show();

    return a.exec();
}

.pro文件

cpp 复制代码
QT       += core gui charts

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp

HEADERS +=

TRANSLATIONS += \
    myChart_zh_CN.ts

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
相关推荐
阿闽ooo3 小时前
外观模式:从家庭电源控制看“简化接口“的设计智慧
c++·设计模式·外观模式
你的冰西瓜4 小时前
C++中的list容器详解
开发语言·c++·stl·list
会飞的胖达喵7 小时前
Qt自动信号槽连接机制:深入解析与应用实践
开发语言·qt
CC.GG7 小时前
【C++】哈希表的实现
java·c++·散列表
bkspiderx8 小时前
C++变量生命周期:从创建到销毁的完整旅程
c++·生命周期·作用域·变量生命周期
T0uken9 小时前
现代 C++ 项目的 CMake 工程组织
c++
H CHY10 小时前
C++代码
c语言·开发语言·数据结构·c++·算法·青少年编程
xiaolang_8616_wjl10 小时前
c++题目_传桶(改编于atcoder(题目:Heavy Buckets))
数据结构·c++·算法
小小8程序员10 小时前
除了 gcc/g++,还有哪些常用的 C/C++ 编译器?
c语言·开发语言·c++
希望_睿智11 小时前
实战设计模式之中介者模式
c++·设计模式·架构