Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ QGraphicsLineItem遵循QGraphicsSellipsItem_C++_Qt_Qgraphicsitem - Fatal编程技术网

C++ QGraphicsLineItem遵循QGraphicsSellipsItem

C++ QGraphicsLineItem遵循QGraphicsSellipsItem,c++,qt,qgraphicsitem,C++,Qt,Qgraphicsitem,一个qgraphicslipseitem是否有任何方式可以跟随两个QGraphicsEllipseItem,即行的起点是第一个QGraphicsEllipseItem结束的位置,终点是第二个QGraphicsEllipseItem的位置。我试图将一个椭圆设置为父项,但问题是,每个项目只有一个父项。因此,我可以让它遵循其中一点,但不遵循第二点。我有一个类似的问题,通过在QGraphicscene的子类中实现mousePressEvent和mouseMoveEvent,并使用一个存储当前选定节点的变

一个
qgraphicslipseitem
是否有任何方式可以跟随两个
QGraphicsEllipseItem
,即行的起点是第一个
QGraphicsEllipseItem
结束的位置,终点是第二个
QGraphicsEllipseItem
的位置。我试图将一个椭圆设置为父项,但问题是,每个项目只有一个父项。因此,我可以让它遵循其中一点,但不遵循第二点。

我有一个类似的问题,通过在QGraphicscene的子类中实现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()
为了实现这一点,您还必须对QGraphicsSellipseitem进行子类化,以便拥有一个包含传入和传出连接的数组。比如:

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

您必须更详细地概述您正在做的事情。例如,为什么线条不能成为椭圆的父元素?()亲子关系不是让事物一起运动的唯一方式,如果合适的话,只是一种方便的方式。您可以完全避免父子关系,查看移动通知,并根据需要调整任何响应。请注意,您可以通过“编辑”按钮编辑您的问题以添加更多细节…这只是我已经尝试过的一个示例,我只希望线条在点移动时跟随这些点,这就是我认为可以尝试查看的内容,看看这是否对您有帮助。