I have two QGraphicsItem in a scene and I want to draw a line between this two objects. However, these objects are movable and I don't know how to update a line after every movement ?
-
You have to subclass the items and have them store a pointer to the line-object when the connection is made. Then override their `itemChange` method so they update the line on geometry changes. It is not the most trivial task and you have to take care about correctly deleting the objects and removing the stored pointers at the right time. – Bowdzone Aug 19 '15 at 05:51
-
Minor grammar mistakes and fixed puntuation. – Rico Aug 21 '15 at 00:03
-
But how to add the line to scene which is connecting those two objects ? What about boundingRect ? – Makumba Aug 23 '15 at 15:53
2 Answers
I just answer one of your other questions demonstrating just that: connecting 2 movable eclipses with a line.
There is a fully working example to get you going. See this other answer for more details.
Add a comment or update your question if something is still not clear. Otherwise please mark it as accepted.
In one application I implemented lines between objects by drawing actually three lines following way:
A----
:
:----B
To update the lines, I added 6 pointers to objects
QGraphicsLineItem *prvLineItems[3];
QGraphicsLineItem *nxtLineItems[3];
When objects and lines were created, I set pointers prvLineItems and nxtLineItems point to the created lines.
Then when location of the object changed, I moved corresponding lines also (in my case in the mouseReleaseEvent).
In your case you need just one pointer to the line to objects 1 and 2. When one of the objects changes location, change coordinates of the line also.

- 171
- 5