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:

    ChartWidget.h

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

    ChartWidget.cpp

    #include "ChartWidget.h"
    #include <QtCharts/QChartView>
    #include <QtCharts/QLineSeries>
    #include <QtWidgets/QGridLayout>
    
    
    using namespace QtCharts;
    
    ChartWidget::ChartWidget(QWidget *parent) :
    	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->setAnimationOptions(QChart::AllAnimations);
    
    	chart->legend()->hide();
    	chart->addSeries(series);
    	chart->createDefaultAxes();
    	chart->setTitle("Simple line chart example");
    
    	QChartView *chartView = new QChartView(chart);
    	chartView->setRenderHint(QPainter::Antialiasing);
    	chartView->setMinimumSize(640, 480);
    	/*
    	// create main layout
    	QGridLayout *mainLayout = new QGridLayout;
    	mainLayout->addWidget(chartView, 1, 1);
    	mainLayout->setColumnStretch(1, 1);
    	mainLayout->setColumnStretch(0, 0);
    	setLayout(mainLayout);*/
    }
    

    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?

    Thanks



  • 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.

    HTH


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.