Kyle Strand Kyle Strand - 24 days ago 11
C++ Question

Iterate over points in QML XYSeries

I'm trying to iterate over the points in the Qt 5.5 QtCharts

type, which inherits from
XYSeries
.

I tried a simple
for
-loop, but I can't figure out a way to determine the number of points in the series:

for (var i=0; i < my_line_series.contentItem.children.length; ++i) {
// do something with the index
}


Unfortunately, it seems that none of the following are members of
LineSeries
:


  • contentItem

  • children

  • length

  • count

  • size



Alternatively, I could try iterating over the series until there's no point to access at the given index--but the method used to access a point at a given index,
at()
, returns
(0,0)
if the index is invalid, and it's entirely possible that there are real
(0,0)
points in my
LineSeries
.

....So is it possible to do this iteration in pure QML (see edit)?

EDIT: The underlying C++ type
QLineSeries
provides the
count()
method, so it's possible to pass the
LineSeries
QML type to a
Q_INVOKABLE
method taking a
QAbstractSeries*
and get the size that way. This seems like an absurd workaround for a simple problem, though.

Answer

The QML LineSeries type simply does not provide access to the methods that are required for any non-trivial tasks involving the series.

The solution is to implement these tasks in C++ by passing the LineSeries QML type to a Q_INVOKABLE method taking a QAbstractSeries*.