Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Delete a child of a QQuickItem



  • Hi,

    I created a Plot class derived from QQuickItem which displays x vs y plot curves. For the single plot curves I created a PlotCurve class which is also derived from QQuickItem and the plot curves are childs of the Plot object. The Plot object has a vector of pointers to its childs.

    I am wondering how I can reset my Plot Object to its original state. Simply calling delete or deleteLater 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)?


  • Qt Champions 2017

    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 via

       for(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 of PlotCurve (derived vom QQuickItem) which also sets the parent of the PlotCurve to be the Plot object.

    Is there something like a remove children function?



  • Hm,

    I tried to call removeAllChildren in the updatePaintNode function of Plot 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();
       }

Log in to reply