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

【QT】】qcustomplot的使用

1.下载并添加qcustomplot.c和qcustomplot.h文件

在这里插入图片描述
拖动一个Widget,提升为qcustomplot
在这里插入图片描述
在这里插入图片描述
成功后是这样的,
在这里插入图片描述
改第三行:greaterThan(QT_MAJOR_VERSION, 4): QT += widgets printsupport 编译,不报错,出现带坐标轴的界面,成功
在这里插入图片描述

2.生成曲线

mainwindow.h增加定时器等等

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "qcustomplot.h"  // 引入QCustomPlot头文件

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    QCustomPlot *customPlot; // 添加QCustomPlot指针

    QTimer *dataTimer; // 定时器
    QVector<double> xData, yData; // 数据存储
    void updatePlot(); // 更新图表的函数
};

#endif // MAINWINDOW_H

main函数增加曲线等

#include "mainwindow.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}
void MainWindow::updatePlot()
{
    // 生成新的数据点
    static double time = 0;
    static double signalValue = 0;

    // 生成新信号值(如正弦波)
    signalValue = qSin(time);
    time += 0.1;

    // 保存数据
    xData.append(time);
    yData.append(signalValue);

    // 更新图表
    customPlot->graph(0)->setData(xData, yData);
    customPlot->xAxis->setRange(time, 10, Qt::AlignRight); // X轴范围保持在最新10秒内
    customPlot->replot(); // 刷新图表
}

mianwindow.c添加曲线属性以及定时器参数等

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTimer>
#include <QVector>
#include <cmath> // 使用数学函数

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

    // 初始化QCustomPlot
    customPlot = new QCustomPlot(this);
    setCentralWidget(customPlot);

    // 配置图表
    customPlot->addGraph();
    customPlot->graph(0)->setPen(QPen(Qt::blue)); // 设置曲线颜色
    customPlot->xAxis->setLabel("Time (s)");
    customPlot->yAxis->setLabel("Signal Value");

    // 启用自动缩放
    customPlot->xAxis->setRange(0, 10);
    customPlot->yAxis->setRange(-1, 1);

    // 初始化定时器
    dataTimer = new QTimer(this);
    connect(dataTimer, &QTimer::timeout, this, &MainWindow::updatePlot);
    dataTimer->start(100); // 每100毫秒更新一次

    // 初始化数据
    xData.clear();
    yData.clear();
}
MainWindow::~MainWindow()
{
    delete ui;
}


效果:动态正选曲线
在这里插入图片描述

相关文章:

  • leecode797.所有可能的路径
  • WPF窗口读取、显示、修改、另存excel文件——CAD c#二次开发
  • TEXT()的作用
  • 杨辉三角形(信息学奥赛一本通-2043)
  • C、C++打印地址用%u
  • DeepSeek面试——分词算法
  • 搭建基于flask的web应用框架
  • 源代码防泄漏之反向沙箱篇
  • 射频相关概念
  • 利用余弦相似度在大量文章中找出抄袭的文章
  • Peach配置文件中<Agent>模块的作用及参数解析
  • 分页查询的实现
  • ELK(Elasticsearch、Logstash、Kbana)安装及Spring应用
  • 【大模型】知识蒸馏(knowledge distillation)算法详解
  • Oracle中的INHERIT PRIVILEGES权限
  • 【搭建环境】windows环境redis\集群;rabbitmq
  • 通过mybatis的拦截器对SQL进行打标
  • 【GIT】什么是GitHub Actions ?
  • 【数据分享】1999—2023年地级市地方一般公共预算收支状况数据(科学技术支出/教育支出等)
  • python--面试题--基础题
  • 梅花奖在上海|秦海璐:演了15年《四世同堂》,想演一辈子
  • 英国知名歌手批政府:让AI公司免费使用艺术家作品是盗窃
  • 解读|战国子弹库帛书漂泊海外79年今归国,追索仍将继续
  • 美国新泽西客运公司遭遇罢工:40年来首次,35万人受影响
  • 通用汽车回应进口车业务调整传闻:因经济形势变化重组,致力于在中国持续发展
  • 诠释微末处的丰盈:“上海制造佳品汇”首届海外专场即将亮相日本大阪