3.使用QCustomPlot绘图基础

QCustomPlot绘图基础

customPlot 是 QCustomPlot实例的指针。如果您已在QtCreator中提升了widget,则可能会通过ui->customPlot 访问相应的widget。

您可以通过 customPlot->addGraph() 创建一个新图形 。然后为图形分配一些数据点,例如通过 customPlot->graph(0)->setData(..)例如QVector<double>  为x和y()赋值。QCustomPlot使用 和 而不是x 和y的原因 是,在分配哪个轴具有什么作用时允许更大的灵活性。因此,如果将左轴定义为“键 坐标轴”,将底部定义为“值 坐标轴”,则可以绘制在图的左侧竖立的图形。默认情况下,一个QCustomPlot部件有四个轴:customPlot->xAxisyAxisxAxis2, yAxis2  ,类型为QCPAxis ,对应于下、左、上和右轴。

轴的范围定义了当前可见的绘图部分:customPlot->xAxis->setRange(-1, 1)

要在屏幕上显示对绘图的任何更改,调用 customPlot->replot() 。请注意,replot 函数 在 当调整widget的大小并触发内置用户交互时,将自动发生重新绘制。比如:用户交互例、用鼠标拖动轴范围、用鼠标滚轮缩放。

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

// generate some data:

QVector<double> x(101), y(101); // initialize with entries 0..100

for (int i=0; i<101; ++i)

{

  x[i] = i/50.0 1// x goes from -1 to 1

  y[i] = x[i]*x[i]; // let's plot a quadratic function

}

// create graph and assign data to it:

customPlot->addGraph();

customPlot->graph(0)->setData(x, y);

// give the axes some labels:

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

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

// set axes ranges, so we see all data:

customPlot->xAxis->setRange(-11);

customPlot->yAxis->setRange(01);

customPlot->replot();

输出应如下所示。

刻度和标签是由 当前使用的轴刻度器自动选择的。QCPAxisTicker的实例访问。通过 xAxis->ticker() 获取步长,通过xAxis->ticker()->setTickCount(6) 设置步长。默认的轴行情指示器非常适合简单的数字显示,但是有专门的类,例如时间跨度,日历日期,类别,pi(或其他符号单位)和对数轴。有关详细信息,请参见QCPAxisTicker文档。

轴的刻度标签(数字)即使到达更宽的范围也永远不会到达小部件边框之外。这是由于自动计算保证的,默认情况下该功能处于打开状态customPlot->axisRect()->setAutoMargins(QCP::msNone)。如果刻度标签和轴标签需要更多空间,您可以通过手动调整边距。customPlot->axisRect()->setMargins(..).

 

改变图形外观

图形的外观具有许多因素,所有这些因素都可以修改。这里是最重要的:

  • 线型(Line style): 调用 graph->setLineStyle(..) 更改线型,线型参见QCPGraph :: LineStyle文档。
  • 线笔(Line pen):  QPainter框架提供的所有笔都可用,例如,实线,虚线,点划线,不同的宽度,颜色,透明度等。通过graph->setPen(..) 设置。
  • 散点符号(Scatter symbol):调用graph->setScatterStyle(..) 以更改散点符号的外观。有关所有可能的散点样式,请参见QCPScatterStyle文档或介绍页面上显示的散点样式演示屏幕截图。
  • 在图形下方或两个图形之间进行填充:QPainter框架提供的所有画笔均可用于图形填充:实体,各种图案,纹理,渐变,颜色,透明度等。通过设置配置的画笔。调用  graph->setBrush(..)。

坐标轴

轴的外观可以通过更改用它们绘制的笔和标签使用的字体来修改。看看QCPAxis的文档。属性的简单总结:setBasePensetTickPensetTickLengthsetSubTickLengthsetSubTickPensetTickLabelFontsetLabelFontsetTickLabelPaddingsetLabelPadding。您可以使用反转轴(例如,使值从左向右减小而不是从左向右增大)setRangeReversed。如果要在轴端装饰(例如箭头),请使用setLowerEndingsetUpperEnding

 

网格线

通过访问轴的相应QCPGrid实例可以修改网格。例如,通过访问来更改与左侧轴绑定的水平网格线的外观。网格线的外观基本上是绘制它们的笔,可以通过设置customPlot->yAxis->grid() 刻度线0处的网格线可以用不同的笔绘制,也可以用进行配置。如果您不想使用特殊的笔绘制零线,只需将其设置为,刻度线0处的网格线将使用普通的网格笔绘制。 默认情况下,子网格线设置为不可见。可以使用激活它们。grid()->setSubGridVisible(true)。

 

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用qcustomplot绘图的基本步骤: 1. 下载qcustomplot库,解压到本地目录。 2. 在Qt Creator中创建一个新的Qt Widgets应用程序项目。 3. 在项目文件.pro中添加以下行: ``` INCLUDEPATH += /path/to/qcustomplot LIBS += /path/to/qcustomplot/libqcustomplot.a ``` 注意将 /path/to/qcustomplot 替换为实际的路径。 4. 在Qt Creator中添加一个QWidget或QChartView控件,用于显示绘图结果。 5. 在代码中引入qcustomplot库的头文件: ``` #include "qcustomplot.h" ``` 6. 在QWidget或QChartView控件中添加一个QCustomPlot对象,并设置其大小、坐标轴等属性。 ``` QCustomPlot *customPlot = new QCustomPlot(this); customPlot->setGeometry(x, y, width, height); customPlot->xAxis->setLabel("x"); customPlot->yAxis->setLabel("y"); ``` 7. 使用QVector、std::vector等容器存储数据,然后将数据添加到QCustomPlot对象中的曲线或柱状图中。 ``` QVector<double> xData, yData; // fill xData and yData with data points QCPGraph *graph = customPlot->addGraph(); graph->setData(xData, yData); ``` 8. 调用QCustomPlot对象的replot()函数,重新绘制图形。 ``` customPlot->replot(); ``` 完整示例代码如下: ``` #include "mainwindow.h" #include "ui_mainwindow.h" #include "qcustomplot.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); // create QCustomPlot object QCustomPlot *customPlot = new QCustomPlot(this); customPlot->setGeometry(10, 10, 400, 300); customPlot->xAxis->setLabel("x"); customPlot->yAxis->setLabel("y"); // generate some data QVector<double> xData, yData; for (double x = 0; x <= 10; x += 0.1) { xData.append(x); yData.append(sin(x)); } // add data to graph and set properties QCPGraph *graph = customPlot->addGraph(); graph->setData(xData, yData); graph->setPen(QPen(Qt::blue)); // replot the graph customPlot->replot(); } MainWindow::~MainWindow() { delete ui; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值