0

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!

2
  • 2
    While it's not spelled out for appendRow, the documentation for setItem states that the model takes ownership. Also, there are takeItem and takeRow methods which are documented to release ownership, which would be kinda difficult if the model didn't take it in the first place. Commented Aug 30 at 0:07
  • If you follow function calls inside QStandardItemModel::appendRow you will eventually arrive at QStandardItemPrivate::insertRows, which sets the parent and the model for the item(s) being inserted. Commented Sep 3 at 2:13

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.