QCustomplot基本使用(一)

文章目录

简述

QCustomPlot是一个基于Qt C++的图形库,用于绘制和数据可视化 - 制作漂亮的2D图 - 曲线图、趋势图、坐标图、柱状图等,并为实时可视化应用程序提供高性能服务。它没有进一步的依赖关系,并有着良好的文档记录。
QCustomPlot可以导出为各种格式,比如:PDF文件和位图(如:PNG、JPG、BMP)。
可在自己的项目中直接使用两个源文件(qcustomplot.h与qcustomplot.cpp),或预先编译成库。

下载

QCustomPlot首页:http://www.qcustomplot.com/
进入QCustomPlot下载页,下载最新的完整包(包含:源码、文档、示例)!
将下载好的安装包进行解压缩,里面包含文档、示例、更改日志、GPL授权、以及最重要的两个文件qcustomplot.h与qcustomplot.cpp。

使用

首先创建一个正常的mainwindow主窗口带ui的qt的工程,然后将我们上面下载的头文件和源文件都放在这个工程中。
然后在.pro文件中添加一行QT += printsupport
在这里插入图片描述

再到ui界面添加一个widget,右键提升为QCustomPlot
在这里插入图片描述
构造函数中写如下代码。注释已经写的很详细了,此处不再赘述

	this->resize(700,700);//设置主窗口大小
    QCustomPlot *pCustomPlot = new QCustomPlot(this);//实例化一个对象
    pCustomPlot->resize(600, 600);//设置大小
 
    // 可变数组存放绘图的坐标的数据,分别存放x和y坐标的数据,101为数据长度
    QVector<double> x(101), y(101);
 
    // 添加数据,这里演示y = x^3,为了正负对称,x从-10到+10
    for (int i = 0; i < 101; ++i)//模拟数据
    {
        x[i] = i/5 - 10;        //一个间距 放五个点
        y[i] = qPow(x[i], 3);   // x的y次方;
    }
 
    // 向绘图区域QCustomPlot添加一条曲线   必须先有曲线才能添加数据 否则程序异常崩溃
    QCPGraph *pGraph = pCustomPlot->addGraph();
 
    // 添加数据
    pCustomPlot->graph(0)->setData(x, y);
 
    // 设置坐标轴名称
    pCustomPlot->xAxis->setLabel("x");
    pCustomPlot->yAxis->setLabel("y");
 
    // 设置背景色
    pCustomPlot->setBackground(QColor(50, 50, 50));
 
    // 设置画笔颜色
    pGraph->setPen(QPen(QColor(32, 178, 170)));
 
    // 设置x/y轴文本色、轴线色、字体等
    pCustomPlot->xAxis->setTickLabelColor(Qt::white);//设置水平的刻度下面的数字颜色
    pCustomPlot->xAxis->setLabelColor(QColor(0, 160, 230));//设置水平的坐标轴颜色
    pCustomPlot->xAxis->setBasePen(QPen(QColor(32, 178, 170)));//设置水平的刻度颜色(单位是1)
    pCustomPlot->xAxis->setTickPen(QPen(QColor(128, 0, 255)));//设置刻度画笔
    pCustomPlot->xAxis->setSubTickPen(QColor(255, 165, 0));//设置水平的刻度颜色(单位是0.2)
    QFont xFont = pCustomPlot->xAxis->labelFont();//获取水平的坐标轴名称的字体样式
    xFont.setPixelSize(20);//设置字体大小
    pCustomPlot->xAxis->setLabelFont(xFont);//设置水平的坐标轴名称的字体样式
 
    pCustomPlot->yAxis->setTickLabelColor(Qt::white);//同样的方式设置y轴
    pCustomPlot->yAxis->setLabelColor(QColor(0, 160, 230));
    pCustomPlot->yAxis->setBasePen(QPen(QColor(32, 178, 170)));
    pCustomPlot->yAxis->setTickPen(QPen(QColor(128, 0, 255)));
    pCustomPlot->yAxis->setSubTickPen(QColor(255, 165, 0));
    QFont yFont = pCustomPlot->yAxis->labelFont();
    yFont.setPixelSize(20);
    pCustomPlot->yAxis->setLabelFont(yFont);
 
    // 设置坐标轴显示范围,否则只能看到默认范围
    pCustomPlot->xAxis->setRange(-11, 11);
    pCustomPlot->yAxis->setRange(-1100, 1100);

一个简单的图形就出来了。其他功能将在后序展开。

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林夕07

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值