I am creating a plotting component in QML. The structure of my QML component looks like this:
Rectangle {
id: canvas
objectName: "myPlot"
Rectangle {
id: plot
PlotArea {
id: pa
} // this is my c++ QQuickItem
MouseArea {} // for handling interaction with the plot, like zooming
XAxis{}
YAXis{}
}
}
here PlotArea is my c++ class. I need to interact with this QML component from C++, more precisely, I need to call a member function of PlotArea to add data to the plot. The usual way is to use findChild<QObject*>("objectName"), but as the component will get reused, I cannot give PlotArea and object name.
How can I access PlotArea if I have a pointer to "myPlot"? I tried
QObject *plot = rootObjects.value(0)->findChild<QObject*>("plotObject");
PlotArea * myplot = (plot->findChild<PlotArea*>("pa"));
but this does not work. However, what works is the above approach if I do
PlotArea {
id: pa
objectName: "pa"
} // this is my c++ QQuickItem
But I wonder if this is safe, as there will be serveal PlotAreas in my application, and all of them have the name "pa".