PDA

View Full Version : Can I copy a QGraphicsItem on my scene?



JesperWe
10th February 2010, 17:51
I have a nice looking class inherited from QGraphicsItem that I use on my scene. Let's call an object of that class A

Some other items (B and C) of another class can have pointers (in the graphical sense, not the variable memory sense!) to A. I show this by drawing an arrow from B to A and from C to A, sort of like this:

B -> A <- C

When B and C are a long distance from each other I would like to instantiate another copy of A so that I can draw something like:


B->A


C->A



...but A can only be added once to the scene.

What is the smart way of solving this without making two copies of A that have to be modified simultaneously if they change?

ecanela
11th February 2010, 00:07
i don´t know the context of the aplication...
but if you need the three item show in scene A, B and C.

- adjust the scale factor in the view to show the 3 items (easy way)
- change the position of the item A if B and C are long distance from each other

if you choose the option 2, you need reimplement the QGraphicsItem::itemChange() function to detect when a item change your position (in item B and C) and if the distance between B and C are big enough, change the position of item A.

JesperWe
11th February 2010, 14:42
Well... that was a question I didn't ask :-)
I must have been unclear...

The question is: How can I display the same QGraphicsItem in two different locations of my scene?

ecanela
12th February 2010, 21:21
i don't think is posible display the same item in two diferent location. because each item have only one position QGraphicsItem::pos() in the scene.

try to use QGraphicsItemGroup class. this classs treat a group if tem as one item.