Qwt QwtPlotGrid网格类详解

编程入门 行业动态 更新时间:2024-10-21 13:29:35

Qwt QwtPlotGrid<a href=https://www.elefans.com/category/jswz/34/1770341.html style=网格类详解"/>

Qwt QwtPlotGrid网格类详解

1.概述

QwtPlotGrid类是Qwt绘图库中用于绘制网格的类。网格是图表中用于显示坐标轴刻度之间的辅助线的一种视觉元素。使用QwtPlotGrid类可以方便地添加水平和垂直网格线到绘图区域上。

以下是类继承关系图:

2.常用接口

分别启用或禁用x和y轴上的网格线。

enableX(bool enable)和enableY(bool enable)

分别启用或禁用x和y轴上的最小网格线。

enableXMin(true)和enableYMin(true); 

分别设置网格的主要线和次要线的画笔。可以使用QPen对象来设置线条的颜色、宽度等属性。

setMajorPen(const QPen &pen)和setMinorPen(const QPen &pen)

指定x轴比例分割。

void setXDiv (const QwtScaleDiv &)

指定y轴比例分割。

void setYDiv (const QwtScaleDiv &)

将网格附加到或从QwtPlot中分离。attach()方法用于将网格添加到QwtPlot图表中,而detach()方法则用于从图表中移除网格。

attach(QwtPlot *plot)和detach()

3.示例

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qwt_plot.h"
#include "qwt_plot_curve.h"
#include "qwt_text.h"
#include "qwt_legend.h"
#include "qwt_symbol.h"
#include "qwt_plot_marker.h"
#include "qwt_plot_grid.h"
#include "qwt_scale_div.h"MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow)
{ui->setupUi(this);QwtPlot *plot = new QwtPlot(QwtText("sin and cos"),this);//设置背景色plot->setCanvasBackground(QBrush(QColor(Qt::white)));//添加图例QwtLegend *legend = new QwtLegend();plot->insertLegend(legend);//设置坐标轴标题plot->setAxisTitle(QwtAxis::YLeft,QwtText("Y坐标轴"));plot->setAxisTitle(QwtAxis::XBottom,QwtText("X坐标轴"));plot->setAxisScale(QwtAxis::YLeft, -1, 1);//设置左Y轴范围plot->setAxisScale(QwtAxis::XBottom, 0, 4);//设置左Y轴范围//自定义X轴的刻度值QwtScaleDiv gridDiv( 0.0, 4.0, QList<double>(), QList<double>(), QList<double>() << 0.2 << 0.4 << 0.6 << 0.8 );plot->setAxisScaleDiv( QwtAxis::XBottom, gridDiv );// add curves1QwtPlotCurve *curve1 = new QwtPlotCurve( "cos" );curve1->setRenderHint( QwtPlotItem::RenderAntialiased );curve1->setPen( Qt::blue );//curve1->setBrush(QBrush(QColor(Qt::red)));curve1->setLegendAttribute( QwtPlotCurve::LegendShowLine );curve1->setYAxis( QwtAxis::YLeft );curve1->setStyle(QwtPlotCurve::Lines);curve1->attach( plot );// add curves2QwtPlotCurve *curve2 = new QwtPlotCurve( "sin" );curve2->setRenderHint( QwtPlotItem::RenderAntialiased );curve2->setPen( Qt::darkGreen );//curve3->setBrush(QBrush(QColor(Qt::black)));curve2->setLegendAttribute( QwtPlotCurve::LegendShowLine );curve2->setYAxis( QwtAxis::YLeft );curve2->setStyle(QwtPlotCurve::Lines);curve2->attach( plot );int numPoints = 100;QVector<QPointF> points1;QVector<QPointF> points2;for (int i = 0; i < numPoints; ++i) {double x = 4*i / (double)(numPoints - 1); // x范围从0到2double y = sin(2 * M_PI * x); // 计算sin函数的y值double y2 = cos(2 * M_PI * x); // 计算sin函数的y值points1.append(QPointF(x, y));points2.append(QPointF(x, y2));}curve1->setSamples(points2);curve2->setSamples(points1);// 创建水平参考线标记QwtPlotMarker *lineMarker = new QwtPlotMarker();lineMarker->setLabel(QwtText("zero axis")); // 设置标记的文本标签lineMarker->setLabelAlignment(Qt::AlignRight | Qt::AlignTop); // 设置标签的对齐方式lineMarker->setLabelOrientation(Qt::Horizontal); // 设置标签的方向为水平lineMarker->setLineStyle(QwtPlotMarker::HLine); // 设置标记的线条样式为水平线lineMarker->setLinePen(Qt::red, 2.0); // 设置标记的线条颜色和宽度lineMarker->setValue(0.0, 0.0); // 设置标记的坐标位置// 创建文本标记QwtPlotMarker *textMarker = new QwtPlotMarker();textMarker->setLabel(QwtText("(1,0)")); // 设置标记的文本标签textMarker->setLabelAlignment(Qt::AlignCenter | Qt::AlignTop); // 设置标签的对齐方式textMarker->setLabelOrientation(Qt::Horizontal); // 设置标签的方向为水平textMarker->setLineStyle(QwtPlotMarker::NoLine); // 设置标记没有线条textMarker->setXValue(1); // 设置标记的x坐标textMarker->setYValue(0); // 设置标记的y坐标// 将标记附加到QwtPlot中lineMarker->attach(plot);textMarker->attach(plot);// 创建网格对象QwtPlotGrid *grid = new QwtPlotGrid();// 启用x轴和y轴上的网格线grid->enableX(true);grid->enableY(true);grid->enableXMin(true);grid->enableYMin(true);// 设置网格线的画笔grid->setMajorPen(QPen(Qt::lightGray, 0, Qt::SolidLine));grid->setMinorPen(QPen(Qt::lightGray, 0, Qt::DotLine));// 将网格附加到QwtPlot图表中grid->attach(plot);// finally, refresh the plotplot->replot();ui->verticalLayout->addWidget(plot);
}MainWindow::~MainWindow()
{delete ui;
}

4.相关推荐

Qwt QwtPlotCurve曲线类详解-CSDN博客

更多推荐

Qwt QwtPlotGrid网格类详解

本文发布于:2023-12-03 07:32:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1652699.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:网格   详解   Qwt   QwtPlotGrid

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!