2

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.

schwemmdx
  • 31
  • 5
  • `In a slot from another thread some` - what does this mean? You must not modify the gui from another thread than the main gui thread. – chehrlic Jan 18 '21 at 18:04
  • Ah, sorry if this was a bit confusing. To clear this up: The Thread reads from a serial device and in the slot (partly shown above) the gui reacts to the emitted signal from the thread. There is no problem at all, the threads are working like a charm. But, in the slot the data is also appended to the ```series``` to be shown in the graph. – schwemmdx Jan 18 '21 at 19:39
  • Maybe this helps: https://stackoverflow.com/questions/43010572/how-to-update-redraw-qchart-after-data-is-added-to-qlineseries – chehrlic Jan 19 '21 at 06:07

1 Answers1

1

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.

schwemmdx
  • 31
  • 5
  • `easy_chart.cpp:29:12: warning: 'setAxisX' is deprecated qchart.h:107:5: note: 'setAxisX' has been explicitly marked deprecated here qcompilerdetection.h:227:45: note: expanded from macro 'Q_DECL_DEPRECATED'` – euraad Sep 08 '21 at 13:00