In my Qt application, I'm dynamically creating QStandardItem objects and adding them to a QStandardItemModel using appendRow():
auto* item = new QStandardItem("text"); model->appendRow(item); // or appendRow(rowList) I know that Qt often uses parent-child object hierarchies for memory management, but I couldn't find explicit documentation confirming that QStandardItemModel takes ownership of the items passed to appendRow().
My model (m_GenerateProgramModel) is a class member variable, and I'm creating items on the heap with new. Do I need to manually delete these items, or is their lifetime managed by the model?
Specifically:
Will the model automatically delete the items when it is destroyed? Is it safe to rely on this behavior, or should I track and delete items manually? I want to avoid memory leaks or double-deletion crashes.
Thanks!
appendRow, the documentation forsetItemstates that the model takes ownership. Also, there aretakeItemandtakeRowmethods which are documented to release ownership, which would be kinda difficult if the model didn't take it in the first place.QStandardItemModel::appendRowyou will eventually arrive atQStandardItemPrivate::insertRows, which sets the parent and the model for the item(s) being inserted.