QGraphicsLineItem跟随QGraphicsEllipseItem

时间:2014-02-16 12:30:12

标签: c++ qt qgraphicsitem

QGraphicsLineItem是否有任何方式QGraphicsEllipseItems跟随两个QGraphicsEllipseItem,因为该行的起点是第一个QGraphicsEllipseItem结尾的位置,而结束点是第二个{{1}}的位置。我试图将一个椭圆设置为父级,但问题是,每个项只有一个父级。所以我可以让它遵循其中一个要点,而不是第二个要点。

1 个答案:

答案 0 :(得分:0)

我遇到了类似的问题,并通过在QGraphicsScene的子类中实现mousePressEvent和mouseMoveEvent并具有存储当前所选节点的变量来解决它。这些方面的东西:

)

为此,您还必须对QGraphicsEllipseItem进行子类化,以便您拥有一个包含传入和传出连接的数组。类似的东西:

# 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()

此外,更好的解决方案可能是为项目本身实现mousePressEvent和mouseMoveEvent