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();
}

至此,绘制图形完毕。

原文地址:https://www.cnblogs.com/wanzaiyimeng/p/7119932.html