2

http://qt-project.org/doc/qt-4.8/qgraphicsscene.html#addItem

said

If the item is already in a different scene, it will first be removed from its old scene, and then added to this scene as a top-level.

I want to keep the item in old scene. how can I do this?

myscene1.addItem(item);
myscene2.addItem(item);// I don't want to remove item from myscene1
Nejat
  • 31,784
  • 12
  • 106
  • 138
Taeyun
  • 211
  • 2
  • 16

3 Answers3

0

An item cannot occupy two scenes at the same time, in the way that you cannot be in two places at the same time.

The only way to do this is to make a copy of the item and place it in the second scene.

TheDarkKnight
  • 27,181
  • 6
  • 55
  • 85
0

You can make a copy of the item :

myscene1.addItem(item);
myscene2.addItem(item->clone());
Nejat
  • 31,784
  • 12
  • 106
  • 138
0

What you could do is to create a new class. e.g.

class Position
{
   ...
   QPoinfF pos;
   ...
}

Then you can add that class to your item.

class Item : public QGraphicsItem
{
   ...
public:
   void setSharedPos(Position *pos)
   {
      sharedPosition = pos;
   }

   //implement the paint(...) function
   //its beeing called by the scene
   void paint(...)
   {
      //set the shared position here
      setPos(sharedPos);
      //paint the item
      ...
   }
protected:
   void QGraphicsItem::mouseReleaseEvent ( QGraphicsSceneMouseEvent * event )
   {
      //get the position from the item that could have been moved
      //you could also check if the position actually changed
      sharedPosition->pos = pos();
   }

private
   Position *sharedPostion;
   ...
}

You would no have to create two items and give them both the same pointer to a Position object.

Item *item1 = new Item;
Item *item2 = new Item;
Position *sharedPos = new Position;

item1->setSharedPos(sharedPos);
item2->setSharedPos(sharedPos);

myScene1->addItem(item1);
myScene2->addItem(item2);

They should no at least share their position in the scenes. If this works then you'll have to change the Position class to fit your needs and it should be working.

I am not quite shure if setting the position in the paint() function works. But thats how I would try to synchronise the items. If it does not work then you'll have to look for another place to update the settings of the items.

Or you could give the items a Pointer to each other and let them change the positions/settings directly.

e.g.

class Item : public QGraphicsItem
{
...
   void QGraphicsItem::mouseReleaseEvent ( QGraphicsSceneMouseEvent * event )
   {
       otherItem->setPos(pos());
   }
...
   void setOtherItem(Item *item)
   {
      otherItem = item;
   }
private:
   Item *otherItem;
}

Item *item1 = new Item;
Item *item2 = new Item;

item1->setOtherItem(item2);
item2->setOtherItem(item1);

myScene1->addItem(item1);
myScene2->addItem(item2);
John Doe
  • 146
  • 4
  • The OP did not mention a requirement to synchronise the two objects. However, in this situation, a better method would be to link the two objects and on a moveEvent, send a signal to the linked object to update, but being careful not to end up in a loop of signalling each other, by checking if the new position is equal to the old one. – TheDarkKnight Jul 18 '14 at 12:18
  • You are right he didn't mention anything bout synchronisation. I just tried to explain him how to make them act the same in anyway he would like them to. That are just to concepts I had in mind to realize the behaviour I think he wants to realize. And syncing the position of the items was just an example by me. He didn't mention that either. – John Doe Jul 18 '14 at 13:43