轉自:https://blog.csdn.net/yxy244/article/details/99671911
QCustomPlot是一個基於Qt C++的圖形庫,用於繪制和數據可視化 - 制作漂亮的2D圖 - 曲線圖、趨勢圖、坐標圖、柱狀圖等。
有詳細的例程和幫助文檔,使用方便。
一、下載
到官網https://www.qcustomplot.com/index.php/download下載最新版本
二、安裝幫助文檔
1.下載后解壓,有如下文檔
(1)幫助文檔:documentation
(2)例程:examples
(3)源文件:qcustomplot.cpp和qcustomplot.h
2.將幫助文檔qcustomplot.qch,復制到QT目錄下,例如E:\Qt\Docs\Qt-5.13.0(根據自己QT的安裝位置)
3.打開QT,工具->選項->幫助->添加
4.選擇qcustomplot.qch,幫助文檔就安裝好了
三、測試
1.新建工程,customplot.cpp和qcustomplot.h復制到工程文件里,並在項目里qcustomplot.cpp和qcustomplot.h添加進來,.pro里添加
QT += printsupport
2.打開.ui文件,拖入一個widget控件,右鍵提升
3.輸入QcustomPlot,點添加,再點提升,編譯一下,看有沒錯誤。
4.在構造函數里添加測試代碼
1 MainWindow::MainWindow(QWidget *parent) : 2 QMainWindow(parent), 3 ui(new Ui::MainWindow) 4 { 5 ui->setupUi(this); 6 7 QCustomPlot* customPlot = ui->customPlot; 8 // add two new graphs and set their look: 9 customPlot->addGraph(); 10 customPlot->graph(0)->setPen(QPen(Qt::blue)); // line color blue for first graph 11 customPlot->graph(0)->setBrush(QBrush(QColor(0, 0, 255, 20))); // first graph will be filled with translucent blue 12 customPlot->addGraph(); 13 customPlot->graph(1)->setPen(QPen(Qt::red)); // line color red for second graph 14 // generate some points of data (y0 for first, y1 for second graph): 15 QVector<double> x(251), y0(251), y1(251); 16 for (int i=0; i<251; ++i) 17 { 18 x[i] = i; 19 y0[i] = qExp(-i/150.0)*qCos(i/10.0); // exponentially decaying cosine 20 y1[i] = qExp(-i/150.0); // exponential envelope 21 } 22 // configure right and top axis to show ticks but no labels: 23 // (see QCPAxisRect::setupFullAxesBox for a quicker method to do this) 24 customPlot->xAxis2->setVisible(true); 25 customPlot->xAxis2->setTickLabels(false); 26 customPlot->yAxis2->setVisible(true); 27 customPlot->yAxis2->setTickLabels(false); 28 // make left and bottom axes always transfer their ranges to right and top axes: 29 connect(customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->xAxis2, SLOT(setRange(QCPRange))); 30 connect(customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->yAxis2, SLOT(setRange(QCPRange))); 31 // pass data points to graphs: 32 customPlot->graph(0)->setData(x, y0); 33 customPlot->graph(1)->setData(x, y1); 34 // let the ranges scale themselves so graph 0 fits perfectly in the visible area: 35 customPlot->graph(0)->rescaleAxes(); 36 // same thing for graph 1, but only enlarge ranges (in case graph 1 is smaller than graph 0): 37 customPlot->graph(1)->rescaleAxes(true); 38 // Note: we could have also just called customPlot->rescaleAxes(); instead 39 // Allow user to drag axis ranges with mouse, zoom with mouse wheel and select graphs by clicking: 40 customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables); 41 }
5.運行,沒錯誤能看到如下波形。