欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 名人名企 > 【QT】】qcustomplot的使用

【QT】】qcustomplot的使用

2025/3/16 20:43:00 来源:https://blog.csdn.net/qq_37768954/article/details/146259071  浏览:    关键词:【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_NAMESPACEclass MainWindow : public QMainWindow
{Q_OBJECTpublic: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);// 初始化QCustomPlotcustomPlot = 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;
}

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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词