在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(); }
至此,繪制圖形完畢。