Important: Please read the Qt Code of Conduct -

QtCharts with Qt Creator

  • Dear all,

    I am trying to add a QChart to a simple dialog window, but I encounter some problems: I have created the dialog and I have inserted a QWidget from QtDesigner. Then I have promoted the widget to a class named ChartWidget and I have created this class that result like that:


    #include <QWidget>
    class ChartWidget : public QWidget
    	explicit ChartWidget(QWidget *parent = 0);


    #include "ChartWidget.h"
    #include <QtCharts/QChartView>
    #include <QtCharts/QLineSeries>
    #include <QtWidgets/QGridLayout>
    using namespace QtCharts;
    ChartWidget::ChartWidget(QWidget *parent) :
    	QLineSeries *series = new QLineSeries();
    	series->setName("Line 1");
    	series->append(0, 6);
    	series->append(2, 4);
    	series->append(3, 8);
    	series->append(7, 4);
    	series->append(10, 5);
    	*series << QPointF(11, 1) << QPointF(13, 3) << QPointF(17, 6) << QPointF(18, 3) << QPointF(20, 2);
    	QChart *chart = new QChart();
    	chart->setTitle("Simple line chart example");
    	QChartView *chartView = new QChartView(chart);
    	chartView->setMinimumSize(640, 480);
    	// create main layout
    	QGridLayout *mainLayout = new QGridLayout;
    	mainLayout->addWidget(chartView, 1, 1);
    	mainLayout->setColumnStretch(1, 1);
    	mainLayout->setColumnStretch(0, 0);

    But with this implementation, when i compile, the dialog window opens correctly, but it is empty. Instead if I uncomment the last lines in the cpp file, I see the chart correctly, but I am not controlling the layout with the QtDesigner anymore. How can I see the chart using the QtDesigner to setup the layout?


  • It might not be the answer but it is a answer. I declare my charts using QML to maintain declarative / visual design.

    I only control them from C++ with C++ data. I do all the processing of dynamic axis ranges inside that, I do stupidly high frequency updates.

    I'd recommend, if you very frequently update your line series like I do to use: void QXYSeries::replace(QVector<QPointF> points) (or QList...)

    and also set your LineSeries to use open GL either from QML or C++ works fine, you do lose some functionality but it saves using canvas (which is slower) if you have lots of points of data, with many fast updates.