PDA

View Full Version : Garbage collection



Septi
5th July 2010, 17:51
Does QGraphicsScene delete all the graphic items/widgets/layouts upon deletion, or should I take care of them?
Because I don't need to access most of them, so I feel it's pointless to keep the pointers.

tbscope
5th July 2010, 18:21
The scene takes ownership. You don't need to delete the items yourself.

Septi
6th July 2010, 06:36
tbscope, thanks!

By the way, that means that if I decide to keep some pointers, they will become invalid, when scene gets deleted, or null, if they're smart pointers (QPointer), doesn't it?

tbscope
6th July 2010, 06:46
Yes

Be carefull with that. There's nothing wrong with keeping a list to all the objects but before using them check if they are valid. But I guess you already know that.
Edit: not that there is a reason to keep a list on your own, the scene already does that for you.
Edit 2: and if you want to have several lists, one for rectangles, one for text etc... you can always subclass the scene of course

Septi
6th July 2010, 09:59
Okay, I see, thanks.


and if you want to have several lists, one for rectangles, one for text etc... you can always subclass the scene of course

You mean by keeping a list of needed items inside a scene, reimplementing functions to add items and adding a function to retrieve the list? If so, in my case it's probably easier to just keep that list outside of QGraphicsScene, it's only used from outside anyway.

tbscope
6th July 2010, 15:13
No, I mean return a list of rectangles for example.
You keep the items list of the scene unchanged. But you use that list to return more information.

Pseudo code example:
foreach item in scene items
{
if item = rectangle
add to custom list
}
return custom list