Data model to update chart content



  • I'm trying to create a data model that contains everything I need to display in my QML front end using slots and signals.

    The QML would be triggered to update and would load all it's data from the model. I want to provide all data from a Python program but a C++ solution would probably give me the information I need. I don't want to be loading chart series on the Python side, just loading up data that the QML side would extract. This way I can add extra series that one application might choose to show in a chart but another application might not. Same data model but QML picks what to use.

    The examples I have seen so far load the specific chart series for Python or C++. How can I just supply the series in the data model and get QML to extract it if required? I think the "slow method" in the following link is what I want but there is no back end code supplied so I can't see how to implement.

    QML Charts - Data from C++ and Tickmarks

    Specifically, what would you put in your main.py or main.c etc to make this work in qml:

    myLineSeries.clear()
    for(var i = 0; i < myData.dataX.length; i++) {
        myLineSeries.append(myData.dataX[i], myData.dataY[i]) // point by point append - slow
    }
    

    There seem to be lots of examples for sending across content to a listView but I can't find any for sending an array of numbers across to be read in to a chart series. Any suggestions where to look?



  • @BobbyG Below is the sample code which can be used to update the Chart from C++

    void PlotData::updateData(QAbstractSeries *p_series)
    {
        QVector<QPointF> pointList;
        pointList.reserve(numPts);
        for(int count = 0; count < plot_data_vector.size(); ++count)
        {
            pointList.append(x_data, y_data); // fill your plot data here
        }
    
        if(p_series)
        {
            QXYSeries *xySeries = static_cast<QXYSeries *>(p_series);
            xySeries->replace(pointList);
        }
    }
    
    void PlotData::clearSeries(QAbstractSeries *p_series)
    {
        if(p_series)
        {
            QXYSeries *xySeries = static_cast<QXYSeries *>(p_series);
            xySeries->clear();
        }
    }
    

    And from the QML you need to call this function by passing the chart view series.

    PlotData.updateData(chartView.series(1))
    

    Hope this helps...