Phil Hannent
Phil Hannent

Reputation: 12317

Qt QGraphicsScene copy

I have a QGraphicsScene that I want to copy and append to the start of a list. What is the best method of doing this?

QGraphicsScene* m_scene = new QGraphicsScene();
QGraphicsScene* m_DuplicateScene;

QList<QGraphicsScene *>m_list;

Upvotes: 6

Views: 2941

Answers (2)

Mahdi
Mahdi

Reputation: 105

I make a copy of QGraphicsScene's items in my project. I'm using this way. may be helpful.
I have a class like this:

class DiagramScene : public QGraphicsScene
{
[some datas]
}

and another class in this like:

class DiagramItem : public QGraphicsItem
{
}

and in copy function, I use a code like this:

Diagram_Scene myScene;

foreach (QGraphicsItem *item, diagram_scene1->items()) 
{
    if(item->type() == DiagramItem::Type)
    {
        DiagramItem *di = qgraphicsitem_cast<DiagramItem*>(item);
        myScene.addItem(di);
    }
}

Upvotes: 2

strager
strager

Reputation: 90042

Doing this would be very complicated because you don't know anything about the children of m_scene. Even if you dynamic_cast and create a clone() function for each type of QGraphicsItem, you still need to remember that other people can subclass QGraphicsItem and create their own type of graphics item, making them unclonable by you.

Basically, no, you can't duplicate a QGraphicsScene (cloning all items in the process). You can't even make references to the children of the original scene's children because a QGraphicsItem can only have one scene.

Unless I'm missing a method call, of course. Searches for "clone," "copy," and "duplicate" produced no results, though.

On your second question, use QList<T *>::push_front. Thus, m_list.push_front(m_DuplicateScene);

(Side note: you prepend to the start of a list, and append to the end of a list.)

Upvotes: 7

Related Questions