Is there any way that a QGraphicsLineItem
follows two QGraphicsEllipseItems
in the sense that the starting point of the line is the position of the first QGraphicsEllipseItem
end, and the end point is the position of the second QGraphicsEllipseItem
. I tried to set one ellipse as parent, but the problem is, that there is only one parent for each Item. So I can get it to follow one of the points, but not the second one.
Asked
Active
Viewed 419 times
0
-
You'll have to outline what you're doing in more detail. For instance, why couldn't the line the parent of the Ellipses? ([see this recent Q&A for a possibly related question](http://stackoverflow.com/questions/21745025/)) A parent-child relationship isn't the only way to make things move together, just a convenient one if it fits. You could avoid parent-child relationships altogether, watch the move notifications, and adjust whatever you liked in response. *Notice you can edit your question to add more detail with the EDIT button...* – HostileFork says dont trust SE Feb 16 '14 at 12:48
-
This was just an example of what I already tried, I just want the line to follow the points when they move and that was what I thought would work – Rooxo Feb 16 '14 at 12:50
-
Try looking at the [elastic nodes sample](http://qt-project.org/doc/qt-5.0/qtwidgets/graphicsview-elasticnodes.html) and see if that helps you. – HostileFork says dont trust SE Feb 16 '14 at 13:52
1 Answers
0
I had a similar problem and solved it by implementing the mousePressEvent and mouseMoveEvent in a subclass of the QGraphicsScene and having a variable that stores the current selected node. Something along these lines:
# Allows moving nodes across the scene updating the connections
def mouseLeftClickMoveEvent(self,event):
# If no node is selected, skip
if self.selected_item is None:
return
# QtConnections are not movable
if isinstance( self.selected_item, QtConnection ):
return
# Update the position of the selected node
new_pos = event.scenePos()
new_x = int(new_pos.x()/self.grid_size)*self.grid_size
new_y = int(new_pos.y()/self.grid_size)*self.grid_size
self.selected_item.setRect( QRectF(new_x,new_y,self.selected_item.node_size,self.selected_item.node_size) )
# Update the position of the connections to the node
for c in self.selected_item.incons:
x1 = c.line().p1().x()
y1 = c.line().p1().y()
x2 = self.selected_item.rect().center().x()
y2 = self.selected_item.rect().center().y()
c.setLine(x1,y1,x2,y2)
# Update the position of the connections from the node
for c in self.selected_item.outcons:
x1 = self.selected_item.rect().center().x()
y1 = self.selected_item.rect().center().y()
x2 = c.line().p2().x()
y2 = c.line().p2().y()
c.setLine(x1,y1,x2,y2)
# Update the scene to repaint the background
self.update()
For this to work, you will also have to subclass the QGraphicsEllipseItem so that you have an array with the incoming and outgoing connections. Something like:
class QtNode(QGraphicsEllipseItem):
def __init__(self, node, layout):
super(QGraphicsEllipseItem, self).__init__()
self.id = node.id
self.node = node
self.incons = []
self.outcons = []
Also, a better solution might be to implement the mousePressEvent and mouseMoveEvent for the items themselves

user3870353
- 31
- 3