简体   繁体   中英

Qt what happens to QGraphicsItems when added to QGraphicsLinearLayout

I have a vector of pointers to QGraphicsItems, which have all been added to a QGraphicsLinearLayout.

I'm worried about memory management.

1) What happens when I add them? Do they get parented automatically or do I need to explicitly call item.setParentLayoutItem(layout)?

2) If they are parented does Qt handle their deletions? (when the parent gets deleted)

3) I also have a remove method that removes the item from my vector and the layout. Should I remove the item from the layout before deleting it/calling vector.erase(...)... or does this depend on whether I parented or not?

1) What happens when I add them? Do they get parented automatically or do I need to explicitly call item.setParentLayoutItem(layout)?

From qt documentation:

If the item inherits both QGraphicsItem and QGraphicsLayoutItem (such as QGraphicsWidget does) the item is really part of two ownership hierarchies. This property informs what the layout should do with its child items when it is destructed. In the case of QGraphicsWidget, it is preferred that when the layout is deleted it won't delete its children (since they are also part of the graphics item hierarchy).

see ownedByLayout and setOwnedByLayout

Theoretically they are, but you can parameterize the deletion with the documentation provided by the two links above.

2) If they are parented does Qt handle their deletions?

Yes

3) I also have a remove method that removes the item from my vector and the layout. Should I remove the item from the layout before deleting it/calling vector.erase(...)... or does this depend on whether I parented or not?

If your QGraphicsLayoutItem owns (parent/child relation) all your QGraphicsItem , they will all be deleted when the parent is deleted.

If you want to remove a single element, then a simple call to:

theItemYouWantToDelete->deleteLater()

should suffice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM