QT下使用QCustomPlot绘制曲线

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

QT下使⽤QCustomPlot绘制曲线
在QT下绘制曲线有两种⽅案,⼀种是通过Qwt绘制,另外⼀种是本⽂将要提到的QCustomPlot进⾏绘制。

在官⽹上下载QCustomPlot的相关压缩包,有beta以及release版本,这⾥我下载的release版本(⾃带的相关例程plot-examples)。

将相关的源码添加到⼯程pro,编译个时候会报错,我们需要添加⼀个库 QT += printsupport
使⽤QT Designer进⾏界⾯的设计,新建⼀个Widget,右键->提升为 QCustomPlot,
相关初始化
void TCWareWidget::InitCustomPlot()
{
QPen pen;
pen.setColor(Qt::blue);
ui->customPlot->legend->setVisible(true);
ui->customPlot->addGraph();
ui->customPlot->graph(0)->setPen(pen);
ui->customPlot->graph(0)->setName("数据1");
pen.setColor(Qt::red);
ui->customPlot->addGraph();
ui->customPlot->graph(1)->setPen(pen);
ui->customPlot->graph(1)->setName("数据2");
pen.setColor(Qt::green);
ui->customPlot->addGraph();
ui->customPlot->graph(2)->setPen(pen);
ui->customPlot->graph(2)->setName("数据3");
pen.setColor(Qt::cyan);
ui->customPlot->addGraph();
ui->customPlot->graph(3)->setPen(pen);
ui->customPlot->graph(3)->setName("数据4");
pen.setColor(Qt::magenta);
ui->customPlot->addGraph();
ui->customPlot->graph(4)->setPen(pen);
ui->customPlot->graph(4)->setName("数据5");
pen.setColor(Qt::yellow);
ui->customPlot->addGraph();
ui->customPlot->graph(5)->setPen(pen);
ui->customPlot->graph(5)->setName("数据6");
pen.setColor(Qt::darkRed);
ui->customPlot->addGraph();
ui->customPlot->graph(6)->setPen(pen);
ui->customPlot->graph(6)->setName("数据7");
pen.setColor(Qt::darkBlue);
ui->customPlot->addGraph();
ui->customPlot->graph(7)->setPen(pen);
ui->customPlot->graph(7)->setName("数据8");
// ui->customPlot->graph(0)->setLineStyle(QCPGraph::lsNone);
// ui->customPlot->graph(0)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCross,4));
// ui->customPlot->addGraph();
// pen.setColor(Qt::red);
// ui->customPlot->graph(1)->setPen(pen);
// ui->customPlot->graph(1)->setName("拟合");
// ui->customPlot->xAxis->setTickLabelType(QCPAxis::ltDateTime);
// ui->customPlot->xAxis->setDateTimeFormat("hh:mm:ss");
// ui->customPlot->xAxis->setAutoTickStep(false);
// ui->customPlot->xAxis->setTickStep(2);
// ui->customPlot->axisRect()->setupFullAxesBox();
ui->customPlot->xAxis->setLabel("x(V)");
ui->customPlot->yAxis->setLabel("y(ml)");
// ui->customPlot->resize(500,300);
// ui->customPlot->xAxis->setRange(0,11);
// ui->customPlot->yAxis->setRange(0,1100);
ui->customPlot->xAxis->setRange(0,1);
ui->customPlot->yAxis->setRange(0,1);
connect(ui->customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->xAxis2, SLOT(setRange(QCPRange)));
connect(ui->customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->yAxis2, SLOT(setRange(QCPRange))); ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
}
数据显⽰绘制
int TCWareWidget::ShowWavePlot()
{
QVector <QVector<double> > x;
int dataSize;
x.resize(readDataList.size());
for(int i = 0; i < readDataList.size();i++)
{
dataSize = readDataList[i].count();
x[i].resize(dataSize);
for(int j = 0; j < dataSize;j++)
{
x[i][j] = 0.02 * j;
}
ui->customPlot->graph(i)->setData(x[i],readDataList[i]);
}
double xRange = 0.02 * dataSize;
ui->customPlot->xAxis->setRange(0,xRange);
ui->customPlot->yAxis->setRange(minData -1,maxData + 1);
ui->customPlot->replot();
}
⾄此,绘制图形完毕。

相关文档
最新文档