0
点赞
收藏
分享

微信扫一扫

Hello Qt(十七)——QCustomPlot

攻城狮Chova 2022-03-11 阅读 80

一、QCustomPlot简介

1、QCustomPlot简介

QCustomPlot是一个用于制作图形和图表,以及提供实时可视化应用程序的高性能绘图图表Qt C++控件,可以绘制曲线图、趋势图、坐标图、柱状图等,支持导出多种格式包括:PDF,PNG, JPG 和 BM。

QCustomPlot主要的类的关系如下:

 2、QCustomPlot的安装

QCustomPlot的使用:

将qcustomplot.h、qcustomplot.cpp文件加入工程中,在使用QCustomPlot的文件中包含头文件qcustomplot.h即可。QCustomPlot是继承自QWidget,因此使用方法与QWidget类一样。如果QT5以上版本,需要在工程描述文件.pro中增加printsupport模块。

QCustomPlot帮助文档使用:

将下载解压后的目录的documentation目录下有个qcustomplot.qch文件,将qcustomplot.qch文件拷贝QtCreator的某个目录下,在QtCreator中工具->选项->帮助->文档->添加,选择qcustomplot.qch文件,确定。

二、基本绘图

1、绘制抛物线Y=X*X

#include "qcustomplot.h"

void drawParabolicCurve()
{
    QCustomPlot *customplot = new QCustomPlot;
    QVector<double> x(101), y(101);
    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
    }

    customplot->addGraph();
    customplot->graph(0)->setData(x, y);

    // 为坐标轴添加标签
    customplot->xAxis->setLabel("x");
    customplot->yAxis->setLabel("y");

    // 设置坐标轴的范围,
    customplot->xAxis->setRange(-1, 1);
    customplot->yAxis->setRange(0, 1);

    //重绘图像
    customplot->replot();
    customplot->show();

    customplot->resize(400, 400);
}

举报

相关推荐

0 条评论