Solved Delete a child of a QQuickItem
-
Hi,
I created a
Plot
class derived fromQQuickItem
which displays x vs y plot curves. For the single plot curves I created aPlotCurve
class which is also derived fromQQuickItem
and the plot curves are childs of thePlot
object. ThePlot
object has a vector of pointers to its childs.I am wondering how I can reset my
Plot
Object to its original state. Simply callingdelete
ordeleteLater
on the adresses of the plot curves is not working.If it was c++ only I would probably delete the whole plot object, and create a new one. But as I instantiate the plot object in qml this is not an option.
How can I reset my object (remove its childs)?
-
Not quite clear about the question completed. If you want to delete the child object at QML, if you have identity property of QML object, you can say <id>.destroy() or iterate through the child objects using children property of an item and destroy them.
-
Sorry for the unclear question. How would I destroy it from c++? In c++ I got the object vis
QObject *qmlplotobject = rootObjects.value(0)->findChild<QObject*>("myPlot");
This object has a member
QVector<PlotCurve *> _curves
, but deleting the curves viafor(int i=0;i<_curves.size();i++) { delete _curves[i]; }
does not work. The plot curves were created via an
add_curve
function which calls the creator ofPlotCurve
(derived vomQQuickItem
) which also sets the parent of thePlotCurve
to be thePlot
object.Is there something like a remove children function?
-
Hm,
I tried to call
removeAllChildren
in theupdatePaintNode
function ofPlot
but it seems that it has no effect -
Here my stackoverflow post, where I tried to improve the question: https://stackoverflow.com/questions/50852203/qtquickscene-graph-delete-child-node
-
this is what i was looking for:
for(auto &curve : _curves) { curve->setParent(nullptr); curve->setParentItem(nullptr); curve->deleteLater(); }