当前位置: 首页 > news >正文

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

两个核心类时QChart和QLineSeries

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

QLineSeries最多容纳4096+1024个点

#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_NAMESPACEint 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文件

QT       += core gui chartsgreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsCONFIG += 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.0SOURCES += \main.cppHEADERS +=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

http://www.lryc.cn/news/372298.html

相关文章:

  • Linux-centos7 nvm使用
  • 【Linux】Linux环境基础开发工具_6
  • Redis宣布商用后,Redis国产化替代方案有那些?
  • Go API
  • 基于STM32的简易智能家居设计(嘉立创支持)
  • 【YOLOv5/v7改进系列】改进池化层为RT-DETR的AIFI
  • 使用Python和Matplotlib绘制复杂数学函数图像
  • 淘宝/1688获得店铺的所有商品(商品列表)
  • 【MySQL】锁机制
  • LangChain入门学习笔记(一)——Hello World
  • [ROS 系列学习教程] 建模与仿真 - 使用 Arbotix 控制机器人
  • java:使用JSqlParser给sql语句增加tenant_id和deleted条件
  • 华三HCL模拟器安装及华三防火墙配置
  • MySQL基础---库的操作和表的操作(配着自己的实操图,简单易上手)
  • 【6】第一个Java程序:Hello World
  • pytorch神经网络训练(AlexNet)
  • 构建大语言模型友好型网站
  • Git代码冲突原理与三路合并算法
  • 聆思CSK6大模型开发板英语评测类开源SDK详解
  • 通用大模型VS垂直大模型,你更青睐哪一方?
  • Python第二语言(十四、高阶基础)
  • python脚本之调用其他目录脚本
  • C# 事件(Event)定义及其使用
  • 2.负载压力测试
  • 【AI工具】jupyter notebook和jupyterlab对比和安装
  • Linux 基本指令3
  • 在Linux系统中,可以使用OpenSSL来生成CSR(Certificate Signing Request)、PEM格式的公钥和PEM格式的私钥。
  • 【2024最新华为OD-C/D卷试题汇总】[支持在线评测] 团队派遣(100分) - 三语言AC题解(Python/Java/Cpp)
  • Python数据分析与机器学习在医疗诊断中的应用
  • vite.config.js如何使用env的环境变量