mousePressEvent在QGraphicsCen中未按预期工作

2024-10-01 19:28:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在努力重新实现一个QGraphicsItem,但是当单击该项时,我尝试用该类运行的其他函数只会影响第一次单击的实例。我的问题是,如何绕过这个问题,只影响显式单击的实例?你知道吗

我的班级如下:

class PrefabPoly(QGraphicsPolygonItem):

    def __init__(self, points, pen, brush, graphicsItem, parent=None):
        super(PrefabPoly, self).__init__(QPolygon(points), graphicsItem, parent.scene)
        self.setPen(pen)
        self.setBrush(brush)
        self.setCursor(Qt.PointingHandCursor)
        self.parent = parent

    def mousePressEvent(self, e):
        print("press")
        if isinstance(self.parent, CreatePrefabGridWidget):
            self.setBrush(self.parent.cur_color) 
        #The brush is only changed for the item that was clicked first!?!?

Tags: 实例函数selfinitdefpointsclassparent
1条回答
网友
1楼 · 发布于 2024-10-01 19:28:48

这是通过调用原始类函数而不是完全重写它来修复的。你知道吗

在mousePressEvent中:

def mousePressEvent(self, e):
    print("press")
    if isinstance(self.parent, CreatePrefabGridWidget):
        self.setBrush(self.parent.cur_color)

    QGraphicsPolygonItem.mousePressEvent(self, e)

相关问题 更多 >

    热门问题