【问题标题】:QGraphicsLineItem follows QGraphicsEllipseItemQGraphicsLineItem 跟随 QGraphicsEllipseItem
【发布时间】:2015-06-30 09:04:48
【问题描述】:

有什么办法可以让QGraphicsLineItem 跟在两个QGraphicsEllipseItems 之后,因为该行的起点是第一个QGraphicsEllipseItem 终点的位置,终点是第二个@987654326 的位置@。我试图将一个椭圆设置为父级,但问题是,每个项目只有一个父级。所以我可以让它遵循其中一点,而不是第二点。

【问题讨论】:

  • 你必须更详细地概述你正在做的事情。例如,为什么 Ellipses 的父级不能行? (see this recent Q&A for a possibly related question) 亲子关系并不是让事情一起发展的唯一方式,如果合适的话,它只是一种方便的方式。您可以完全避免亲子关系,观看移动通知,并调整您喜欢的任何响应。 请注意,您可以使用 EDIT 按钮编辑您的问题以添加更多详细信息...
  • 这只是我已经尝试过的一个例子,我只是希望线条在移动时跟随点,这就是我认为可行的方法
  • 尝试查看elastic nodes sample 看看是否对您有帮助。

标签: c++ qt qgraphicsitem


【解决方案1】:

我遇到了类似的问题,并通过在 QGraphicsScene 的子类中实现 mousePressEvent 和 mouseMoveEvent 并具有存储当前选定节点的变量来解决它。大致如下:

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

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

class QtNode(QGraphicsEllipseItem):

   def __init__(self, node, layout):
       super(QGraphicsEllipseItem, self).__init__()
       self.id = node.id
       self.node = node
       self.incons  = []
       self.outcons = []

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

【讨论】:

    猜你喜欢
    • 2012-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-24
    • 2020-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多