Qt Creator 二维绘图库QCustomPlot的使用"/>
Qt Creator 二维绘图库QCustomPlot的使用
本文主要包含三个内容:QCustomPlot的加载、在主窗口绘图、在子窗口绘图
QCustomPlot的加载
1.下载链接[click here]
2.解压(解压到哪都行)
- documentation文档
- example示例(看示例比看文档有用)
- changelog更新日志
- *.cpp *.h这两个文件需要添加到自己的.pro文件中才可以画图
3.添加*.cpp *.h到工程文件
- 添加 *.cpp *.h到工程文件
- Qt 5 需要添加下面语句
QT += printsupport
4.在需要的地方[主窗口/子窗口]包含头文件
#include "qcustomplot.h"
QCustomPlot主窗口绘图
- 在ui文件中添加widget
- 提升为QCustomPlot
- 更改QCustomPlot的变量名为customplot
- *.h头文件声明绘图函数
public:void setupQuadraticDemo(QCustomPlot *customPlot);
- *.cpp文件中定义函数
#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);setupQuadraticDemo(ui->customplot);ui->customplot->replot();
}Widget::~Widget()
{delete ui;}void Widget::setupQuadraticDemo(QCustomPlot *customPlot)
{// generate some data:QVector<double> x(101), y(101); // initialize with entries 0..100for (int i=0; i<101; ++i){x[i] = i/50.0 - 1; // x goes from -1 to 1y[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(-1, 1);customPlot->yAxis->setRange(0, 1);}
效果展示
QCustomPlot子窗口绘图
子窗口绘图步骤和主窗口一样,唯一多一步就是要把子窗口绘制的图挂载在主窗口上,整个流程分为两步
(1)挂载子窗口[click here]
(2)模仿主窗口绘图
更多推荐
Qt Creator 二维绘图库QCustomPlot的使用
发布评论