The simplest realization of QT line graph

Time:2019-11-8

This paper will demonstrate how to use QT charts to display a simple line chart quickly.

If you want to directly run the experience effect, you can directly click here to download the source code compilation:

0x00 final effect

Finally, the following effects will be achieved:

The simplest realization of QT line graph

0x01 add charts module

Qt ChartsAs an independent module, it is necessary toproAdd to file:

  • QtExample.pro:
#Add QT charts module
QT    += charts

0x02 contains charts header file and references QT charts namespace

  • main.cpp:
//Include header files required by line chart
#include <QChartView>
#include <QLineSeries>
//Reference namespace
QT_CHARTS_USE_NAMESPACE

int main(){
    ...
}

0x03 create qlineseries and add data

The implementation of line chart needs to create aQLineSeriesObject to save and draw polyline data:

  • main.cpp:
//New a qlineseries instance
QLineSeries *series = new QLineSeries();
//To add experimental data, you can use append method or > > operator
series->append(0,2);
series->append(QPointF(2,6));
series->append(3,8);
series->append(7,9);
series->append(11,3);

*series << QPointF(11,2) << QPointF(15,5) << QPointF(18,4) << QPointF(19,2);

0x04 create qchart to display data

CreateseriesAfter that, you need to create aQChartInstance and Associationseries, create coordinates to display the data:

  • main.cpp:
QChart *chart = new QChart();
//Hide legend
chart->legend()->hide();
//To associate series, this step is very important. You must associate series with qchart to render the data:
chart->addSeries(series);
//When OpenGL is enabled, qlineseries supports GPU drawing. Other chart types of QT are not supported.
series->setUseOpenGL(true);
//Create the default coordinate system (Cartesian coordinates)
chart->createDefaultAxes();
//Set chart title
chart->setTitle(QStringLiteral("Qt line chart example"));

0x05 create qchartview object and display chart

Create hereQChartViewObject is used to display the final result to the interface if you do not want to useQChartView, or you can chooseQGraphicsView sceneTo show.

  • main.cpp:
QChartView *view = new QChartView(chart);
//Turn on anti aliasing for better display
view->setRenderHint(QPainter::Antialiasing);
view->resize(400,300);
//Show chart
view->show();

0x06 complete source code

  • QtExamples.cpp:
QT  += charts
SOURCES += \
        main.cpp
  • main.cpp:
//Include header file
#include <QApplication>
#include <QChartView>
#include <QLineSeries>
//Reference namespace
QT_CHARTS_USE_NAMESPACE

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);


    QLineSeries *series = new QLineSeries();

    series->append(0,2);
    series->append(QPointF(2,6));
    series->append(3,8);
    series->append(7,9);
    series->append(11,3);

    *series << QPointF(11,2) << QPointF(15,5) << QPointF(18,4) << QPointF(19,2);

    QChart *chart = new QChart();
    //Hide legend
    chart->legend()->hide();
    //To associate series, this step is very important. You must associate series with qchart to render the data:
    chart->addSeries(series);
    //When OpenGL is enabled, qlineseries supports GPU drawing. Other chart types of QT are not supported.
    series->setUseOpenGL(true);
    //Create the default coordinate system (Cartesian coordinates)
    chart->createDefaultAxes();
    //Set chart title
    chart->setTitle(QStringLiteral("Qt line chart example"));

    QChartView *view = new QChartView(chart);
    //Turn on anti aliasing for better display
    view->setRenderHint(QPainter::Antialiasing);
    view->resize(400,300);
    //Show chart
    view->show();

    return a.exec();
}

0x07 reference

  • QT linechart example, or directly atQt Creatorsearchlinechart

    The simplest realization of QT line graph

0x08 The end 🙂

The article begins with the public address:nullobject
Personal site: https://www.nullobject.cn