问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

Qt学习:使用QCustomPlot绘制曲线图

创作时间:
作者:
@小白创作中心

Qt学习:使用QCustomPlot绘制曲线图

引用
CSDN
1.
https://m.blog.csdn.net/m0_46436890/article/details/143204544

1. 下载和安装QCustomPlot

下载:
访问QCustomPlot官方网站下载最新的库。QCustomPlot
安装:
将下载的 文件夹中的 和 文件复制到您的Qt项目目录中。QCustomPlot qcustomplot.h qcustomplot.cpp

2. 创建Qt项目

打开Qt Creator:
选择 -> ,然后选择 。FileNew File or Project…Qt Widgets Application
项目设置:
输入项目名称和位置,然后点击 直到完成向导。Next

3. 配置项目文件

在项目的.pro文件中添加,以便包含必要的源文件:.proQCustomPlot

QT      += core gui printsupport
SOURCES += \
    main.cpp \
    mainwindow.cpp \
    qcustomplot.cpp
HEADERS += \
    mainwindow.h \
    qcustomplot.h  

4. 设计用户界面

打开 mainwindow.ui:
使用Qt Designer在主窗口中添加一个控件,并将其 属性设置为 。QWidgetobjectNamecustomPlot
调整大小:
将 的大小调整为合适的值,例如,宽500像素,高400像素。QWidget

5. 修改头文件

打开mainwindow.h,并添加的头文件:QCustomPlot

#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  

6. 实现主窗口逻辑

打开mainwindow.cpp,并在构造函数中初始化和定时器:QCustomPlot

#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;
}  

7. 实现数据更新逻辑

实现 updatePlot 方法:

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(); // 刷新图表
}  

8. 编译并运行项目

保存所有更改:
确保所有代码更改都已保存。
编译项目:
点击 -> debug,然后运行程序。BuildBuild Project
查看结果:
应该可以看到一个实时更新的正弦波曲线图。

总结

您现在已经成功使用在Qt中创建了一个实时曲线图。 可以根据需要调整数据生成逻辑、图表样式和更新频率,以实现不同的应用需求。 非常灵活,可以支持多种图表类型和高级特性,您可以进一步查阅其文档来探索更多功能。QCustomPlotQCustomPlot

© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号