Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.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
Python 3.x 对象移动时重新绘制线_Python 3.x_Pyqt5 - Fatal编程技术网

Python 3.x 对象移动时重新绘制线

Python 3.x 对象移动时重新绘制线,python-3.x,pyqt5,Python 3.x,Pyqt5,我有一个简单的例子,它代表了我正在制作的一个图表编辑器 在图中,我有可以有子对象的“父”对象,子对象应该创建并维护一条与父对象关联的线 在下面的示例中,黑色正方形是父对象,红色三角形是子对象 子对象与父对象创建一条线,但当我移动子对象时,该线不会重新对齐。理想情况下,我会扩展mousemoveent函数,使其能够执行任何与本机相关的操作,并添加行重画功能 让孩子重新画线的最佳方法是什么? from PyQt5.QtGui import * from PyQt5.QtCore import * f

我有一个简单的例子,它代表了我正在制作的一个图表编辑器

在图中,我有可以有子对象的“父”对象,子对象应该创建并维护一条与父对象关联的线

在下面的示例中,黑色正方形是父对象,红色三角形是子对象

子对象与父对象创建一条线,但当我移动子对象时,该线不会重新对齐。理想情况下,我会扩展mousemoveent函数,使其能够执行任何与本机相关的操作,并添加行重画功能

让孩子重新画线的最佳方法是什么?

from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import sys


class ParentNode(QGraphicsRectItem):

    def __init__(self, diagramScene, parent=None, h=60, w=60):
        QGraphicsItemGroup.__init__(self, parent)

        self.scene = diagramScene
        self.h = h
        self.w = w

        self.setPen(QPen(Qt.black, 2))
        self.setBrush(QBrush(Qt.black))
        self.setFlags(self.ItemIsSelectable | self.ItemIsMovable)
        self.setCursor(QCursor(Qt.PointingHandCursor))

        square = QGraphicsPolygonItem()
        square.setPolygon(QPolygonF([QPointF(0, 0), QPointF(20, 0), QPointF(20, 20), QPointF(0, 20)]))

        self.setRect(0.0, 0.0, self.w, self.h)


class ChildNode(QGraphicsItemGroup):

    def __init__(self, parent):
        QGraphicsItemGroup.__init__(self, parent)

        self.parent = parent

        self.setFlags(self.ItemIsSelectable | self.ItemIsMovable)

        triangle = QGraphicsPolygonItem()
        triangle.setPolygon(QPolygonF([QPointF(0, 0), QPointF(20, 0), QPointF(10, 20)]))
        triangle.setPen(QPen(Qt.red, 2))

        self.addToGroup(triangle)
        self.setPos(180, 180)

        #  define line at first
        self.line = self.parent.scene.addLine(self.x() + 10,
                                              self.y() + 0,
                                              self.parent.x() + self.parent.w / 2,
                                              self.parent.y() + self.parent.h)

if __name__ == '__main__':
    app = QApplication(sys.argv)

    scene = QGraphicsScene()

    n1 = ParentNode(scene)
    scene.addItem(n1)

    n2 = ChildNode(n1)
    scene.addItem(n2)

    view = QGraphicsView(scene)
    view.show()
    view.resize(600, 400)

    sys.exit(app.exec_())


PS:这个示例是一个更复杂程序的表示,因此我需要类继承尽可能少地更改。

最简单的方法是对依赖项进行子类化,并重新实现其方法,以便可以监视位置更改。下面是重新编写的脚本,它可以:

import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *


class LineUpdateMixin(object):
    def __init__(self, parent):
        super(LineUpdateMixin, self).__init__(parent)
        self.setFlag(QGraphicsItem.ItemSendsScenePositionChanges)

    def itemChange(self, change, value):
        if change == QGraphicsItem.ItemScenePositionHasChanged:
            self.parentItem().updateLine(value)
        return super(LineUpdateMixin, self).itemChange(change, value)


class Triangle(LineUpdateMixin, QGraphicsPolygonItem): pass


class ParentNode(QGraphicsRectItem):
    def __init__(self, diagramScene, parent=None, h=60, w=60):
        super(ParentNode, self).__init__(parent)
        self.setPen(QPen(Qt.black, 2))
        self.setBrush(QBrush(Qt.black))
        self.setFlags(QGraphicsItem.ItemIsSelectable | QGraphicsItem.ItemIsMovable)
        self.setCursor(QCursor(Qt.PointingHandCursor))

        square = QGraphicsPolygonItem()
        square.setPolygon(QPolygonF([QPointF(0, 0), QPointF(20, 0), QPointF(20, 20), QPointF(0, 20)]))

        self.setRect(0.0, 0.0, w, h)


class ChildNode(QGraphicsItemGroup):
    def __init__(self, parent):
        super(ChildNode, self).__init__(parent)
        self.setFlags(QGraphicsItem.ItemIsSelectable | QGraphicsItem.ItemIsMovable)

        self.line = QGraphicsLineItem()
        parent.scene().addItem(self.line)
        self.updateLine(self.pos())

        triangle = Triangle(self)
        triangle.setPolygon(QPolygonF([QPointF(0, 0), QPointF(20, 0), QPointF(10, 20)]))
        triangle.setPen(QPen(Qt.red, 2))

        self.addToGroup(triangle)
        self.setPos(180, 180)

    def updateLine(self, pos):
        parent = self.parentItem()
        rect = parent.rect()
        self.line.setLine(
            pos.x() + 10, pos.y() + 0,
            parent.x() + rect.width() / 2,
            parent.y() + rect.height(),
            )


if __name__ == '__main__':

    app = QApplication(sys.argv)

    scene = QGraphicsScene()

    n1 = ParentNode(scene)
    scene.addItem(n1)

    n2 = ChildNode(n1)

    view = QGraphicsView(scene)
    view.setGeometry(600, 100, 600, 400)
    view.show()

    sys.exit(app.exec_())