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!

prapin
7,1395 gold badges32 silver badges59 bronze badges
asked Aug 29, 2025 at 3:44
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, 2025 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, 2025 at 2:13

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.