PyQt5を使用して2つの楕円を線で接続しようとしました。これを行うために、githubで取ったクラスを少し変更しました。 PySideの代わりに、PyQt5を使用します。 コードはここから取られる:Qt5のラッパーが作成さhttps://github.com/PySide/Examples/blob/master/examples/graphicsview/diagramscene/diagramscene.pyスーパーメソッドのエラー
class Arrow(QGraphicsLineItem):
def __init__(self, start_item, end_item, parent=None, scene=None):
super(Arrow, self).__init__(parent, scene)
self.arrowHead = QPolygonF()
self.my_start_item = start_item
self.my_end_item = end_item
self.setFlag(QGraphicsItem.ItemIsSelectable, True)
self.my_color = QtCore.Qt.black
self.setPen(QPen(self.my_color, 2, QtCore.Qt.SolidLine,
QtCore.Qt.RoundCap, QtCore.Qt.RoundJoin))
def set_color(self, color):
self.my_color = color
def start_item(self):
return self.my_start_item
def end_item(self):
return self.my_end_item
def boundingRect(self):
extra = (self.pen().width() + 20)/2.0
p1 = self.line().p1()
p2 = self.line().p2()
return QtCore.QRectF(p1, QtCore.QSizeF(p2.x() - p1.x(), p2.y() - p1.y())).normalized().adjusted(-extra, -extra, extra, extra)
def shape(self):
path = super(Arrow, self).shape()
path.addPolygon(self.arrowHead)
return path
def update_position(self):
line = QtCore.QLineF(self.mapFromItem(self.my_start_item, 0, 0), self.mapFromItem(self.my_end_item, 0, 0))
self.setLine(line)
def paint(self, painter, option, widget=None):
if self.my_start_item.collidesWithItem(self.my_end_item):
return
my_start_item = self.my_start_item
my_end_item = self.my_end_item
my_color = self.my_color
my_pen = self.pen()
my_pen.setColor(self.my_color)
arrow_size = 20.0
painter.setPen(my_pen)
painter.setBrush(self.my_color)
center_line = QtCore.QLineF(my_start_item.pos(), my_end_item.pos())
end_polygon = my_end_item.polygon()
p1 = end_polygon.at(0) + my_end_item.pos()
intersect_point = QtCore.QPointF()
for i in end_polygon:
p2 = i + my_end_item.pos()
poly_line = QtCore.QLineF(p1, p2)
intersect_type, intersect_point = poly_line.intersect(center_line)
if intersect_type == QtCore.QLineF.BoundedIntersection:
break
p1 = p2
self.setLine(QtCore.QLineF(intersect_point, my_start_item.pos()))
line = self.line()
angle = math.acos(line.dx()/line.length())
if line.dy() >= 0:
angle = (math.pi * 2.0) - angle
arrow_p1 = line.p1() + QtCore.QPointF(math.sin(angle + math.pi/3.0) * arrow_size,
math.cos(angle + math.pi/3) * arrow_size)
arrow_p2 = line.p1() + QtCore.QPointF(math.sin(angle + math.pi - math.pi/3.0) * arrow_size,
math.cos(angle + math.pi - math.pi/3.0) * arrow_size)
self.arrowHead.clear()
for point in [line.p1(), arrow_p1, arrow_p2]:
self.arrowHead.append(point)
painter.drawLine(line)
painter.drawPolygon(self.arrowHead)
if self.isSelected():
painter.setPen(QPen(my_color, 1, QtCore.Qt.DashLine))
my_line = QtCore.QLineF(line)
my_line.translate(0, 4.0)
painter.drawLine(my_line)
my_line.translate(0, -8.0)
painter.drawLine(my_line)
があるコードが
コードがあなたのリンクから取得され、エラーコードでプログラムがクラッシュ 'START = scene.addEllipse(50、50、100、100) 端 "プロセスは終了コード-1073740791(0xC0000409)で仕上げ" = scene.addEllipse(150,150,100,100) arrow =矢印(開始、終了) scene.addItem(矢印) arrow。updatePosition() ' – SergeyAndSmart
@SergeyAndSmartこれらの行は、私が提案したコードの一部ではありません。私のコードを変更しましたか? – eyllanesc
はい。クラスの初めは 'class Arrow(QGraphicsLineItem): def __init __(self、start_item、end_item、parent = None): super(Arrow、self).__ init __(親)' – SergeyAndSmart