Qt Chart is not updated after updating the series



  • Hello, after I added new points to the series(tried both Lineseries and Scatterseries), the chartsview does not update the added points. What did I miss?

        // line series creation
        scatterseries= new QScatterSeries();
        QPointF point(5.0, 5.0);
        scatterseries->append(point);
        chart = new QChart();
        chart->addSeries(scatterseries);
        QValueAxis *axisX = new QValueAxis;
        axisX->setRange(0, 20);
        axisX->setTickCount(20);
        QValueAxis *axisY = new QValueAxis;
        axisY->setRange(0, 10);
        axisY->setTickCount(10);
        chartview = new QChartView(chart);
        chartview->chart()->setAxisX(axisX);
        chartview->chart()->setAxisY(axisY);
        chartview->setRenderHint(QPainter::Antialiasing);
    
        // Update function 
        auto m_y = QRandomGenerator::global()->bounded(5);
        auto m_x = QRandomGenerator::global()->bounded(5);
        QPointF point(m_x, m_y);
        scatterseries->append(m_x, m_y);
        chartview->repaint();
        qDebug() << point;
        qDebug() << scatterseries->points();
    

    0_1538571070827_3a81749d-9068-41c2-ad16-78a73d32a3ec-image.png

    I can see new points added to series by the debug statements.



  • Try attaching your axis to the series. Does that fix anything?



  • @MrShawn . No it didnt help.



  • I don't see the new point out of bounds of your axis so that is good. From what I understand when appending the series you should be good as long as it is the pointer to the correct series. Assuming the discrepancy for scatter series and line series is an artifact of you swapping between line and scatter series for troubleshooting, are you constantly running something without allowing the event handler to process? Try adding QApplication::processEvents(); after your repaint.



  • @MrShawn . I added QApplication::processEvents(); , but it also did not help.
    I do have other background functions working. But what I do not understand is why the charts is not updated despite the series have updated with new data points.



  • Are you sure you are sending it a pointer to the correct series? You are appending your line series but you are attaching a scatter series.



  • @MrShawn. I was switching line and scatter series. But this is not the cause of the problem. I do correctly added same series and appended the same series. I will edit the posted code.



  • I trust you I was just making sure. I really can't think of anything else given what I see from your code.



  • @MrShawn . You are right, you once suggested. Maybe the only updating the series did not update the pointer .I am not sure thought. But his fixed the problem. Every time I update the series, I have to remove the series and again add the series. I do not understand why is like this.

            chart->removeSeries(scatterseries);
            chart->addSeries(scatterseries);
    


  • I think on my first code I wrote for Charts I did a similar thing. As I got used to QCharts and how things were working I stopped doing that. Looking back at some code I do recently, where I pull data from DB and populate the series from that data. I can confirm that you should not have to do that. That is why my suspicion is that the series you were appending is not truly on the chart, and by adding that code it does now actually add it.

    Anyways if it works it works.



  • @MrShawn . Yes it works now. I am wondering what could be the better way instead of removing and adding series again and again. I can think only other way to be to create new class inheriting QCharts and create series as a class member of the class.



  • seen https://doc.qt.io/qt-5/qxyseries.html#replace-5 ?

    even if you are doing it just for one point - by destroying and creating a series over and over you do this anyhow. has to be quicker/better.

    I belive that also invokes
    https://doc.qt.io/qt-5/qxyseries.html#pointsReplaced

    ChartView should handle redraw and everything for you.


Log in to reply
 

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