Artis De Partis
Artis De Partis

Reputation: 321

c++ list of pointers to QGraphicsItem

I'm experimenting with the QT library and QGraphicsScene. I can add my own objects to the scene and all that is fine. What I would like to have now are some lists outside QGraphicsScene of the objects. Let's say Squares, Circles and Triangles. They all live in the scene, and are (if I'm correct) copied and owned by the scene when I add them.

The question is: what kind of list template containers would be best for implementing my outside lists.

I guess they would need to be pointers so I figured boost::ptr_container. I know I would need a system to make sure the lists are in sync with the scene once I start deleting items. that means removing the pointer form the lists just before removing the object from the scene.

Any ideas on how i should build this system would be very nice.

Upvotes: 2

Views: 1239

Answers (1)

SingerOfTheFall
SingerOfTheFall

Reputation: 29976

You don't need it, Qt does everything for you.

Once you add an item into QGraphicsScene,the scene takes ownership of the item. Which means, the scene is now responsible for storing the item, and freeing the memory occupied by the item when it's needed (usually when you delete an item manually with deleteItem(), or when the scene itself is deleted).

Whenever you want you can ask for the list of all items that have been added to the scene by calling the items() function:

MyGraphicScene.items();

This function returns a list of pointers to all of the scene's items as a QList< QGraphicsItem * >, which is exactly what you need.

Upvotes: 1

Related Questions