Unsolved Adding series to qml Chartview
-
Hello, I came across a problem. How can I add new series and remove series from existing qml Chartview from C++?
I have data for series in C++, also I need qml Chartview as I am developing qml application. Please help. How could this be achieved -
This is an abstract version of what you should do to achieve what you need.
void PlotData::updateData(QAbstractSeries *p_series) { QVector<QPointF> pointList; // fill data into pointlist 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 inside QML
//QML ChartView { id: chartView width: 400 height: 300 antialiasing: true // Plot line LineSeries { id: lineSeries axisX: axisX axisY: axisY useOpenGL: true } } Button { id: reload onClicked: { PlotData.clearSeries(chartView.series(0)) PlotData.updateData(chartView.series(0)) } }
Hope it helps.
-
@Mammamia . Thanks for your answer. I do know how to update the existing series. What I do not know is to add "new series", and to remove "existing series" from the chartview from "C++".
-
@milan
What about thecreateSeries
method?
https://doc.qt.io/qt-5.11/qml-qtcharts-chartview.html#createSeries-method -
@Diracsbracket. I know how to add series from QML, I dont know how to add from C++.
-
@milan
My bad... it seems I am turning blind and obtuse (again)...What about this:
https://stackoverflow.com/questions/38467769/add-c-qabstractseries-to-qml-chartviewInstead of instantiating the interfacing object as a QML type as in the example, you probably want to instantiate it in C++ and then pass it to the QML context. This object would emit a signal when the data is ready and the graph needs to be updated.
In your QML you could then use a
Connections {}
component to connect to that signal and invoke the series-adding JS code from there.