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

利用qcustomplot绘制曲线图

本文详细介绍了qcustomplot绘制曲线图的流程,一段代码一段代码运行看效果。通过阅读本文,读者可以了解到每一项怎么用代码进行配置,进而实现自己想要的图表效果。(本文只针对曲线图)

1 最简单的图形(入门)

  • 头文件
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include "qcustomplot.h"
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();public:void paintLine(QCustomPlot *customPlot);private:Ui::Widget *ui;
};
#endif // WIDGET_H
  • 源文件
#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);paintLine(ui->lineWidget);}Widget::~Widget()
{delete ui;
}void Widget::paintLine(QCustomPlot *customPlot)
{QVector<double> x(101), y(101);for (int i = 0; i < x.size(); i++){x[i] = i / 50.0 - 1;    // x从-1 到 1y[i] = x[i] * x[i];   // 二次函数}customPlot->addGraph();    // 添加一幅折线图customPlot->graph(0)->setData(x, y);    // 为曲线图添加数据 注意这个方法只能是double类型 库内就是这样声明与定义的customPlot->graph(0)->setName("y=x^{2}");// 设置x与y坐标轴的范围customPlot->xAxis->setRange(-1, 1);customPlot->yAxis->setRange(0, 1);// 设置x与y坐标轴的标签customPlot->xAxis->setLabel("x");customPlot->yAxis->setLabel("y");// 显示图例customPlot->legend->setVisible(true);
}
  • 运行效果

在这里插入图片描述

2 美化一下

美化之前,先有几个名词声明一下

  • 轴线
  • 轴刻度线(长的刻度线)
  • 轴子刻度线(短的刻度线)
  • 轴刻度值
  • 轴标签

2.1 设置背景颜色、轴、刻度线、刻度值、刻度标签

    QLinearGradient plotGradient;plotGradient.setStart(0, 0);plotGradient.setFinalStop(0, 350);plotGradient.setColorAt(0, QColor(80, 80, 80));plotGradient.setColorAt(1, QColor(50, 50, 50));customPlot->setBackground(plotGradient);      // 设置背景颜色

在这里插入图片描述

customPlot->axisRect()->setBackground(Qt::red);

在这里插入图片描述

// 设置轴风格customPlot->xAxis->setBasePen(QPen(Qt::white, 1));

在这里插入图片描述

customPlot->xAxis->setTickPen(QPen(Qt::white, 1));    // 轴刻度线和画笔

在这里插入图片描述

注意这两张图像的区别,非常微小的区别

上边这张是设置轴,下边这张是设置轴刻度

customPlot->xAxis->setSubTickPen(QPen(Qt::white, 1));  // 轴子刻度线的画笔

在这里插入图片描述
发现区别了吗?发现具体配置的效果显示在哪了吗?如果没有发现,请仔细对比。

customPlot->xAxis->setTickLabelColor(Qt::white);  // 设置轴刻度颜色

在这里插入图片描述

    customPlot->xAxis->setLabel("标签");                  // 只有设置了标签,轴标签颜色才会显示customPlot->xAxis->setLabelColor(Qt::white);          // 设置轴标签颜色

在这里插入图片描述

    customPlot->xAxis->setTickLengthIn(133);   // 为了效果明显 将值设置的大一点,原来是这个效果 轴线内刻度的长度

在这里插入图片描述

customPlot->xAxis->setTickLengthOut(125);    // 为了效果明显 将值设置的大一点,原来是这个效果 轴线外刻度的长度

在这里插入图片描述

customPlot->xAxis->setUpperEnding(QCPLineEnding::esSpikeArrow); // 设置轴结束的箭头

在这里插入图片描述

	// 设置网格的风格customPlot->xAxis->grid()->setPen(QPen(Qt::red, 1, Qt::DotLine));

在这里插入图片描述

    // 设置水平线customPlot->yAxis->grid()->setPen(QPen(Qt::yellow, 1, Qt::DotLine));

在这里插入图片描述

    // 设置子刻度线customPlot->xAxis->grid()->setSubGridVisible(true);customPlot->xAxis->grid()->setSubGridPen(QPen(Qt::blue, 1, Qt::DotLine));

在这里插入图片描述

 	customPlot->yAxis->grid()->setSubGridVisible(true);customPlot->yAxis->grid()->setSubGridPen(QPen(Qt::green, 1, Qt::DashLine));

在这里插入图片描述

    // 设置刻度为0时的网格线的画笔customPlot->xAxis->grid()->setZeroLinePen(QPen(Qt::red, 3));customPlot->yAxis->grid()->setZeroLinePen(QPen(Qt::red));

在这里插入图片描述

至此,有关图像轴的设置就介绍完了

2.2 线型

利用官网的例子进行介绍

    // 图表的风格QPen pen;// 存放曲线的风格名称QStringList lineNames;lineNames << "lsNone" << "lsLine" << "lsStepLeft" << "lsStepRight" << "lsStrepCenter" << "lsImpulse";for(int i = 0; i < lineNames.size(); i++){customPlot->addGraph();pen.setColor(QColor(qSin(i * 1 + 1.2) * 80 + 80, qSin(i * 0.3) * 80 + 80, qSin(i * 0.3 + 1.5) * 80 + 80));customPlot->graph()->setPen(pen);customPlot->graph()->setName(lineNames.at(i));customPlot->graph()->setLineStyle((QCPGraph::LineStyle)i);customPlot->graph()->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, 6));QVector<double> x(15), y(15);for (int j = 0; j < x.size(); j++){x[j] = j / 15.0 * 5 * 3.14 + 0.01;y[j] = 7 * qSin(x[j]) / x[j]  - (i - QCPGraph::lsNone) * 5 + (QCPGraph::lsImpulse) * 5 + 2;}customPlot->graph()->setData(x, y);customPlot->graph()->rescaleAxes(true);}

在这里插入图片描述

本文参考:

https://blog.csdn.net/qq10097355/article/details/104845706https://www.qcustomplot.com/  官网

相关文章:

  • [yolov11改进系列]基于yolov11使用CPA-Enhancer自适应增强器替换backbone提高低照度目标的python源码+训练源码
  • 【C/C++】algorithm清单以及适用场景
  • Ntfs!NtfsWriteLog函数分析之ntfs!NTFS_LOG_RECORD_HEADER结构的一个例子----NTFS源代码分析之七
  • leetcode刷题日记——1.组合总和
  • 【论文解读】DeepSeek-R1
  • 常用存储器介绍
  • 操作系统:分页存储管理方式(精简版、含例题)
  • 无需登录即可使用的Web应用网站
  • MySQL事务与锁中的MVCC 深度解析与面试题讲解
  • C++学习-入门到精通【15】异常处理深入剖析
  • Docker安装MQEX
  • 20250607在荣品的PRO-RK3566开发板的Android13的uboot中使用gpio命令来配置GPIO的状态
  • VBA进度条ProgressForm1
  • 振动力学:多自由度系统
  • 现代C++特性(一):基本数据类型扩展
  • WaytoAGI东京大会开启AI全球化新对话:技术无国界,合作促创新
  • PCB设计教程【大师篇】——STM32开发板电源设计(电压基准、滤波电容)
  • PG 分区表的缺陷
  • 自制操作系统(五、重写引导部分和C语言的使用)
  • c++学习-this指针
  • 临沂网站建设周口/网络推广公司名字大全
  • 网站将要准备建设的内容有哪些/游戏推广代理app
  • 网站打开为建设中/西安百度公司开户
  • 开发微信微网站建设/网店推广的渠道有哪些
  • 正能量网站建设/seo外包公司
  • 大型商业广场网站建设/个人网页制作成品欣赏