Unsolved Using data in a model for plotting to chart
-
Hi,
I have a list of data comming from a text file.
I load this file on the C++ side and have made a model to display these data in a QML ListView.Now I would like to plot these data on a ChartView.
How can I access the data in the model from outside the ListView in order to add them to a LineSeries which can be displayed on a ChartView?Thnaks
-
@Bert59 since your data is provided by a C++ backend it might make sense to populate your line series in C++. You should be able to instantiate a LineSeries object in your QML and pass that into a C++ API function that you expose to QML (probably via a context property). The C++ function can receive the line series argument as a
QAbstractSeries
and use the C++ API of that class to populate the series data.If I recall, the Oscilloscope example demonstrate how to do this. I think that was my starting point when I was figuring out QML charts. I'd recommend you take a look at it.
-
Hi, @Bert59.
You have a class or a function in the C++ side that receives a QAbstractSeries:
void YourClassExportedToQML::update(QAbstractSeries *series) { if (series) { QXYSeries *xySeries = static_cast<QXYSeries *>(series); // Here you replace the data in the xySeries with your data // use replace, it's better for performance } }
Now in the QML side, I supposed you have the class YourClassExportedToQML and it has a signal when the data came:
Connections{ target: yourClassExportedToQML onMySignalWhenDataIsReady: { var serie = chart.createSeries(ChartView.SeriesTypeLine, "MySerie", axisX, axisY); yourClassExportedToQML.update(serie); } }
If you have doubts, do not hesitate to ask!
-
Thank you Bob64 and oria66 for your replies.
I must say that I'm a beginner with Qt and I find the oscilloscope example quite complicated to understand.
Would it be possible to send me a minimum project with a few hard coded values using your hints oria66? -
Hi oria66,
Just wanted to let you know that finally I succeeded to implement your code examples
Thank you -
Hi, just an additional question.
Using your suggestion I have added below
QXYSeries *xySeries = static_cast<QXYSeries *>(series);
the following:QList<QAbstractAxis *> axis;
axis = xyseries->attachedAxes();
axis.at(0)->setMax(120);
This allow setting the max of the X axis.Unfortunately I could not find how retrieve the actual setting. There is no getMax or something similar available.
Do you have an idea how to do that ? -
What I would use instead to answer the original question is a
VXYModelMapper
-
Thank you for the suggestion.
Could you provide an small example on how this class has to be implemeted?
From the documentation it's not so obvious when you are a beginner with Qt.
Thanks -
Hi @Bert59. When I worked with charts I remember that I manage the axis value from the QML side.
For example:DateTimeAxis { id: axisX min: new Date(2020,3,30,0,0,0) max: new Date(2020,3,30,24,0,0) format: "hh:mm"; labelsColor: "white" }
You can manipulate the min and max value from axisX.
However, it would be interesting in implementing the idea proposed by @GrecKo.