QGraphicsScene scene; QGraphicsRectItem *rectItem = new QGraphicsRectItem(0,0,50,50); scene.addItem(rectItem);
Nothing complicated here. You create a scene, create an item of type
QGraphicsRectItem, define the geometry of the item's rectangle, and then set the item to the scene by calling
addItem(). Pretty straightforward. But what you do not see here is what this implies for the scene. The scene is now responsible for the added item! First of all, the ownership of the item is transferred to the scene. For you, this means that you do not have to worry about freeing the item's memory because deleting the scene also deletes all items associated with the scene. Now remember what we said about the destructor of a custom item: it must be virtual!
QGraphicsScene operates with pointers to
QGraphicsItem. Thus, when it deletes the assigned items, it does that by calling
delete on the...