schwemmdx
schwemmdx

Reputation: 31

Updating QLineSeries in Chart doesnt work at all

I'm melting my brain right now and cant figure out, why my chart isn't updating at all. I've declared the following in my applications *.h-File:

QT_CHARTS_NAMESPACE::QChart* chart;
QT_CHARTS_NAMESPACE::QLineSeries* series;
QT_CHARTS_NAMESPACE::QChartView* chartView;

In my constructor of my mainwindow *.cpp file I've written

this->series= new QT_CHARTS_NAMESPACE::QLineSeries();
this->chart = new QT_CHARTS_NAMESPACE::QChart();
this->chartView = new QT_CHARTS_NAMESPACE::QChartView(chart);
this->chartView->setRenderHint(QPainter::Antialiasing);
.
. 
.
this->series->clear();
this->chart->legend()->hide();

this->setPlotAreaBackgroundVisible(true);

this->series->setVisible(true);
this->chart->addSeries(this->series);
this->chart->createDefaultAxes();

Right now series is empty, and i know from the Qt Docs that my graph should be updated every time the append() emits it's signal.

After all this instantiation my application does its jobs. In a slot from another thread some data should be appended to the series:

 /*Some other things*/
 
 this->seriesXIncrement++;
 QPoint point = QPoint(this->seriesXIncrement,this->parsedReadCommand.toUInt());
 this->series->append(point);
 qDebug()<<this->series->points().size();

 this->chart->createDefaultAxes();
 this->chartView->repaint();

I've validated the append method with qDebug. It's size is growing as expected, also the points I'm creating are valid, but on my graph nothing happens. I'm not sure if createDefaultAxes() is the way to go for updating the axes as well, but i thought this is a relatively easy and lazy approach.

Upvotes: 2

Views: 1495

Answers (1)

schwemmdx
schwemmdx

Reputation: 31

I found out, that createDefaultAxes() caused this problem. I inserted both (x,y) axis insted of createDefaultAxes() like so:

 QT_CHARTS_NAMESPACE::QValueAxis *axisX = new QT_CHARTS_NAMESPACE::QValueAxis;
  axisX->setRange(0, this->series->points().length());
  axisX->setTickCount(10);
  axisX->setLabelFormat("%d");
  chartView->chart()->setAxisX(axisX, series);

  QT_CHARTS_NAMESPACE::QValueAxis *axisY = new QT_CHARTS_NAMESPACE::QValueAxis;
  axisY->setRange(0, 50);
  axisY->setTickCount(1);
  axisY->setLabelFormat("%d");
  chartView->chart()->setAxisY(axisY, series);

This solves the problem.

Upvotes: 1

Related Questions