QCustomPlot绘制曲线

QCustomPlot绘制曲线

  • 1、前言
  • [2、下载 QCustomPlot 库](#2、下载 QCustomPlot 库)
  • 3、在项目中使用QCustomPlot库
    • [3.1 把 QCustomPlot 加入你的 .pro 文件](#3.1 把 QCustomPlot 加入你的 .pro 文件)
    • [3.2 UI 里放一个 Widget 并提升为 QCustomPlot](#3.2 UI 里放一个 Widget 并提升为 QCustomPlot)
    • [3.3 初始化 QCustomPlot](#3.3 初始化 QCustomPlot)
  • 4、项目文件
    • [4.1 .pro文件](#4.1 .pro文件)
    • [4.2 .h文件](#4.2 .h文件)
    • [4.3 .cpp文件](#4.3 .cpp文件)
  • 5、总结

1、前言

|------------------------------------------------|
| 记录一下QCustomPlot绘制曲线的实现方法,方便自己回顾,也可以给有需要的人提供帮助。 |

2、下载 QCustomPlot 库

|---------------------|
| 先从官网下载QCustomPlot 库 |

QCustomPlot 库下载链接

|------------|
| 把下载好的压缩包解压 |

|-------------------|
| 进入解压后的压缩包,找到这两个文件 |

|--------------------|
| 把这两个文件复制粘贴到项目同级目录下 |

3、在项目中使用QCustomPlot库

3.1 把 QCustomPlot 加入你的 .pro 文件

|----------------------|
| 把PrintSupport 模块添加进来 |

cpp 复制代码
QT       += core gui printsupport

|-------------------------|
| 把前面那两个文件添加到,pro文件中,然后构建 |

cpp 复制代码
SOURCES += \
    main.cpp \
    studyqcustomplot.cpp \
    qcustomplot.cpp 

HEADERS += \
    studyqcustomplot.h \
    qcustomplot.h 

FORMS += \
    studyqcustomplot.ui

3.2 UI 里放一个 Widget 并提升为 QCustomPlot

|------------------------------|
| 在Qt Designer(UI)中添加一个QWidget |

|-------------------|
| 右键这个QWidget,点击提升为 |

|----------------------|
| 输入QCustomPlot,接着点击添加 |

|------|
| 点击提升 |

|-------------------------------|
| 可以看到这个QWidget被提升为了QCustomPlot |

3.3 初始化 QCustomPlot

|-------|
| 添加头文件 |

|------|
| 绘制曲线 |

cpp 复制代码
// 生成 100 个 X/Y 点
QVector<double> x(100), y(100);
for (int i = 0; i < 100; ++i)
{
    x[i] = i;
    y[i] = qSin(i / 10.0);   // 画一条正弦曲线
}

// 添加一条曲线
ui->widget->addGraph();
ui->widget->graph(0)->setData(x, y);

// 设置坐标轴范围
ui->widget->xAxis->setRange(0, 100);
ui->widget->yAxis->setRange(-1, 1);

// 绘制
ui->widget->replot();

|------------|
| 如下图所示,成功绘制 |

4、项目文件

|-------------------------------|
| 为了方便大家参考,把.pro,.h,.cpp文件放在下面了 |

4.1 .pro文件

cpp 复制代码
QT       += core gui printsupport

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++17

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    studyqcustomplot.cpp \
    qcustomplot.cpp

HEADERS += \
    studyqcustomplot.h \
    qcustomplot.h

FORMS += \
    studyqcustomplot.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

4.2 .h文件

cpp 复制代码
#ifndef STUDYQCUSTOMPLOT_H
#define STUDYQCUSTOMPLOT_H

#include <QWidget>
#include <qcustomplot.h>

QT_BEGIN_NAMESPACE
namespace Ui {
class StudyQCustomPlot;
}
QT_END_NAMESPACE

class StudyQCustomPlot : public QWidget
{
    Q_OBJECT

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

private:
    Ui::StudyQCustomPlot *ui;
};
#endif // STUDYQCUSTOMPLOT_H

4.3 .cpp文件

cpp 复制代码
#include "studyqcustomplot.h"
#include "ui_studyqcustomplot.h"

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

    // 生成 100 个 X/Y 点
    QVector<double> x(100), y(100);
    for (int i = 0; i < 100; ++i)
    {
        x[i] = i;
        y[i] = qSin(i / 10.0);   // 画一条正弦曲线
    }

    // 添加一条曲线
    ui->widget->addGraph();
    ui->widget->graph(0)->setData(x, y);

    // 设置坐标轴范围
    ui->widget->xAxis->setRange(0, 100);
    ui->widget->yAxis->setRange(-1, 1);

    // 绘制
    ui->widget->replot();
}

StudyQCustomPlot::~StudyQCustomPlot()
{
    delete ui;
}

5、总结

|-----------------------------------------------------------------------------------------------------|
| 以上就是QCustomPlot绘制曲线的整个过程了,浏览过程中,如若发现错误,欢迎大家指正,有问题的可以评论区留言或者私信。最后,如果大家觉得有所帮助的话,可以点个赞,谢谢大家!祉猷并茂,顺遂无虞! |

相关推荐
LYOBOYI1231 天前
qml练习:创建地图玩家并且实现人物移动(2)
开发语言·qt
得鹿梦鱼、1 天前
Qt/halcon 总结halcon字典数据用法和QDataSteam序列化与反序列化结构体实例
qt·tuple字典·qdatasteam·序列化结构体·反序列化结构体
世转神风-1 天前
qt-基础打印-不换行打印
开发语言·qt
老歌老听老掉牙1 天前
PyQt5中RadioButton互斥选择的实现方法
开发语言·python·qt
864记忆1 天前
Qt Creator 常用命令的中英文对照表
开发语言·qt
Larry_Yanan1 天前
Qt多进程(六)共享内存和信号量
开发语言·qt
东方忘忧1 天前
Qt使用QDesktopServices::openUrl打开系统默认应用(如浏览器,文件,文件夹和邮件)
开发语言·qt
计算机内卷的N天1 天前
qt的模态和非模态状态
开发语言·qt
charlie072 天前
qmake工程不能显示手动配置的Kit
qt
深蓝海拓2 天前
PySide6从0开始学习的笔记(二十) qdarkstyle的深入应用
笔记·python·qt·学习·pyqt