您当前的位置: 首页 > 

wendy_ya

暂无认证

  • 2浏览

    0关注

    342博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

QCustomPlot基础教程(九)——QCustomPlot中轴的相关属性和方法

wendy_ya 发布时间:2021-11-03 20:42:13 ,浏览量:2

目录
    • 1、前言
    • 2、QCustomPlot中的轴介绍
    • 3、设置轴标签与清空轴标签
    • 4、设置轴范围
    • 5、设置轴与记号标签的显示与隐藏
    • 6、设置轴的颜色
    • 7、设置背景颜色
    • 8、设置背景图片

本文将对QCustomPlot中最重要的一部分——轴进行详细介绍。

1、前言

基础部分就不过多介绍了,不懂得可以直接参考:QCustomPlot基础教程(一)——QCustomPlot的安装及基础实例,代码部分也不做过多介绍,代码使用的是QCustomPlot基础教程(二)——在Qt中绘制多个曲线的相关代码。

2、QCustomPlot中的轴介绍

QCustomPlot中由QCPAxis类管理QCustomPlot内的单个轴。 通常不需要外部实例化。通过QCustomPlot::xAxis(底部)、QCustomPlot::yAxis(左侧)、QCustomPlot::xAxis2(顶部)和QCustomPlot::yAxis2(右侧)访问QCustomPlot的默认四个轴。其位置如下图所示: 在这里插入图片描述 QCustomPlot有四个QCPAxis成员变量,分别代表四个坐标轴:xAxis(下)yAxis(左)xAxis2(上)yAxis2(右)。

3、设置轴标签与清空轴标签

设置轴标签利用setLabel函数: void QCPAxis::setLabel const QString &str)

代码示例:

customPlot->xAxis->setLabel("x");
customPlot->yAxis->setLabel("y");

这段代码的意思是将下坐标轴标签设置为x,将右坐标轴标签设置为y。

清空轴标签同样利用setLabel()函数,代码如下:

customPlot->xAxis->setLabel("");
customPlot->yAxis->setLabel("");
4、设置轴范围

设置轴的范围利用setRange函数: void QCPAxis::setRange (double lower, double upper) lower表示最小值,upper表示最大值

代码示例:

customPlot->xAxis->setRange(-1, 1);
customPlot->yAxis->setRange(0, 1);

这段代码的意思是将x轴范围设置为[-1,1],将y轴范围设置为[0,1]。

大多数情况下,需要将设置使坐标轴自行缩放能够完全显示,代码如下:

    customPlot->graph(0)->rescaleAxes();
    customPlot->graph(1)->rescaleAxes(true);

【注意】: 调用 customPlot->graph(0)->rescaleAxes();后范围被缩小了,曲线正好占满整个区域,但是调用customPlot->graph(0)->rescaleAxes(true)就不会有变化,因为区域不会缩小。

利用这点可以通过多次调用rescaleaxis来完整地显示多个graph的数据。让graph(0)自动缩放rescaleAxes(),在graph(0)范围的基础上rescaleAxes(true)只会扩大范围或不变而不缩小,这样最终能够显示graph(n)所有的数据。 n条曲线自行缩放能够完全显示代码示例如下:

    customPlot->graph(0)->rescaleAxes();
    customPlot->graph(1)->rescaleAxes(true);
    customPlot->graph(2)->rescaleAxes(true);
    ...
    customPlot->graph(n)->rescaleAxes(true);
5、设置轴与记号标签的显示与隐藏

设置是否显示轴利用setVisible()函数: void QCPLayer::setVisible(bool visible) 轴默认显示,如果不显示可以将bool设置为false,如下:

customPlot->xAxis->setVisible(false);

在这里插入图片描述

设置是否显示记号标签利用setTickLabels()函数: void QCPAxis::setTickLabels(bool show) 记号标签是在记号旁边绘制的数字。 记号标签默认显示,如果不显示可以将bool设置为false,如下:

customPlot->xAxis->setTickLabels(false);

在这里插入图片描述

【注:左轴和下轴默认显示轴和记号标签,右轴和上轴默认不显示轴和记号标签,如果想为右轴和上轴显示刻度但不显示标签,可以进行如下设置:】

    customPlot->xAxis2->setVisible(true);
    customPlot->xAxis2->setTickLabels(false);
    customPlot->yAxis2->setVisible(true);
    customPlot->yAxis2->setTickLabels(false);

设置后的图像分别如下图所示: 在这里插入图片描述 设置后,还需要设置使左轴和下轴始终将其范围转移到上轴和右轴上,代码如下:

    connect(customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->xAxis2, SLOT(setRange(QCPRange)));
    connect(customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->yAxis2, SLOT(setRange(QCPRange)));

设置后效果如图: 在这里插入图片描述

6、设置轴的颜色

设置轴的颜色利用setBasePen函数: void QCPAxis::setBasePen(const QPen & pen)

代码示例:

    customPlot->xAxis->setBasePen(QPen(Qt::red,4));//设置下轴为红色
    customPlot->yAxis->setBasePen(QPen(Qt::blue,4));//设置左轴为蓝色
    customPlot->xAxis2->setBasePen(QPen(Qt::yellow,4));//设置上轴为黄色
    customPlot->yAxis2->setBasePen(QPen(Qt::green,4));//设置右轴为绿色

运行结果: 在这里插入图片描述

7、设置背景颜色

设置背景颜色利用setBackground函数: void QPainter::setBackground(const QBrush &brush) 设置背景为黑色代码如下:

customPlot->axisRect()->setBackground(QBrush(Qt::black));//背景黑色

运行结果: 在这里插入图片描述

8、设置背景图片

设置背景图片同样利用setBackground()函数: void QCPAxisRect::setBackground (const QPixmap & pm) 设置背景缩放使用setBackgroundScaled()函数: void QCPAxisRect::setBackgroundScaled (bool scaled) 设置缩放图形的横纵比利用setBackgroundScaledMode函数: void QCPAxisRect::setBackgroundScaledMode(Qt::AspectRatioMode mode) 该函数常与setBackgroundScaled函数集合使用,代表缩放图形时的横纵比。常用参数如下: 在这里插入图片描述

参数描述Qt::IgnoreAspectRatio大小可自由缩放。不保留纵横比Qt::KeepAspectRatio在给定的矩形内,大小被缩放为一个尽可能大的矩形,保留纵横比Qt::KeepAspectRatioByExpanding将大小缩放为给定矩形外尽可能小的矩形,保留纵横比

设置背景图片代码如下:

    customplot->axisRect()->setBackgroundScaled(true);//启用背景缩放 
    customplot->axisRect()->setBackgroundScaledMode(Qt::AspectRatioMode::IgnoreAspectRatio);//自由缩放
    customplot->axisRect()->setBackground(QPixmap(":/image/background.jpg"));//背景图片

运行结果: 在这里插入图片描述

以上便是本文的全部内容了,如果对你有所帮助,记得点个赞哟~

参考: 【1】https://blog.csdn.net/yxy244/article/details/100311112

关注
打赏
1659256378
查看更多评论
立即登录/注册

微信扫码登录

0.0352s