I am trying to make a QML Object that I can add an arbitrary number of children to and then manipulate them in C++. I have code that works, but I think that I have the wrong approach, especially since it involves a text search.
void Foo::componentComplete()
{
QObjectList children = this->children();
for (int i = 0; i < children.length(); i++)
{
if(QString(children[i]->metaObject()->className()).contains("MyClass"))
{
this->myChildList.append(static_cast<MyClass*>(children[i]));
}
}
}
One example of this can be found in the QML charts example.
Here is one excerpt from the code. Even though the PiceSlice child elements are not assigned to properties of the PieSeries element, they are discovered and displayed by the chart. How does this work under the hood? Is it just using this->children()
or is there a better method?
ChartView {
id: chart
title: "Top-5 car brand shares in Finland"
anchors.fill: parent
legend.alignment: Qt.AlignBottom
antialiasing: true
PieSeries {
id: pieSeries
PieSlice { label: "Volkswagen"; value: 13.5 }
PieSlice { label: "Toyota"; value: 10.9 }
PieSlice { label: "Ford"; value: 8.6 }
PieSlice { label: "Skoda"; value: 8.2 }
PieSlice { label: "Volvo"; value: 6.8 }
}
}
Here is the complete source at Github