QCustomPlot use experience one: installation and use

QCustomPlot is a graphics library based on Qt C++ for drawing and data visualization-making beautiful 2D graphs-curve graphs, trend graphs, coordinate graphs, histograms, etc.

There are detailed routines and help files, which are easy to use.

1. Download

Go to the official website https://www.qcustomplot.com/index.php/download to download the latest version

Two, install the help document

1. After downloading, unzip, there are the following documents

(1) Help document: documentation

(2) Routines: examples

(3) Source files: qcustomplot.cpp and qcustomplot.h

2. Copy the help document qcustomplot.qch to the QT directory, such as E:\Qt\Docs\Qt-5.13.0 (according to your own QT installation location)

 3. Open QT, Tools -> Options -> Help -> Add
 4. Select qcustomplot.qch, the help file is installed

Three, test

1. Create a new project, copy customplot.cpp and qcustomplot.h to the project file, and add qcustomplot.cpp and qcustomplot.h in the project, and add it in .pro

QT       += printsupport


2. Open the .ui file, drag in a widget control, right-click to upgrade
 3. Enter QcustomPlot, click Add, then click Upgrade, compile it, and see if there are any errors.

4. Add test code in the constructor

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    
    
    ui->setupUi(this);
 
    QCustomPlot* customPlot  = ui->customPlot;
    // add two new graphs and set their look:
    customPlot->addGraph();
    customPlot->graph(0)->setPen(QPen(Qt::blue)); // line color blue for first graph
    customPlot->graph(0)->setBrush(QBrush(QColor(0, 0, 255, 20))); // first graph will be filled with translucent blue
    customPlot->addGraph();
    customPlot->graph(1)->setPen(QPen(Qt::red)); // line color red for second graph
    // generate some points of data (y0 for first, y1 for second graph):
    QVector<double> x(251), y0(251), y1(251);
    for (int i=0; i<251; ++i)
    {
    
    
      x[i] = i;
      y0[i] = qExp(-i/150.0)*qCos(i/10.0); // exponentially decaying cosine
      y1[i] = qExp(-i/150.0);              // exponential envelope
    }
    // configure right and top axis to show ticks but no labels:
    // (see QCPAxisRect::setupFullAxesBox for a quicker method to do this)
    customPlot->xAxis2->setVisible(true);
    customPlot->xAxis2->setTickLabels(false);
    customPlot->yAxis2->setVisible(true);
    customPlot->yAxis2->setTickLabels(false);
    // make left and bottom axes always transfer their ranges to right and top axes:
    connect(customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->xAxis2, SLOT(setRange(QCPRange)));
    connect(customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->yAxis2, SLOT(setRange(QCPRange)));
    // pass data points to graphs:
    customPlot->graph(0)->setData(x, y0);
    customPlot->graph(1)->setData(x, y1);
    // let the ranges scale themselves so graph 0 fits perfectly in the visible area:
    customPlot->graph(0)->rescaleAxes();
    // same thing for graph 1, but only enlarge ranges (in case graph 1 is smaller than graph 0):
    customPlot->graph(1)->rescaleAxes(true);
    // Note: we could have also just called customPlot->rescaleAxes(); instead
    // Allow user to drag axis ranges with mouse, zoom with mouse wheel and select graphs by clicking:
    customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
}

5. Run, you can see the following waveform without error.

Guess you like

Origin blog.csdn.net/qq_39400113/article/details/114997896