PDA

View Full Version : How do i duplicate a QGraphicsScene and make a deep copy of it ?



keshav2010
24th May 2018, 15:52
Okay, i've a class that goes by the name "Frame" (represents a single animation frame) , now this frame for sure contains objects of type AnimatedSpriteItem (this class inherits QGraphicsItem).

I want to have the functionality so whenever user create a new Frame (scene), it should not be blank but rather clone of another existing frame.
However, after searching on web for some time, i was unable to find any concrete solution, as im afraid chances of making shallow copies are quite high and so i want to know if there is any concrete way to solve this issue ? do i need to make a private data member say, frameItems like this
QMap<QString, AnimatedSpriteItem *> frameItems; that maps item names to their references and then traverse this container in order to have much more control over process of cloning or there is a better way ?

d_stranz
24th May 2018, 16:48
Like all classes in Qt that have parent-child ownership relationships (QObject, QGraphicsItem, etc.), QGraphicsScene and QGraphicsItem have no copy constructors. In order to clone a scene, you will have to write the code to make a deep copy yourself, starting at the top level with QGraphicsScene::items(), and then for each QGraphicsItem in that list, cloning it and all of its child items.

If everything in the scene is a QGraphicsItem of your own derived class(es), then you could add a clone() method to these that would make the process somewhat simpler. For out-of-the-box QGraphicsItem-bsed classes, you can creat and copy them based on their QGraphicsItem::type() values.

You don't really need to keep any separate data structures. The whole hierarchy of object instances in the scene is there in the QGraphicsScene::items() method.