我有一个自定义 GraphicsItem,它具有打开/关闭的方法(将正方形绘制为绿色/黑色)。但调用 power 方法不会导致任何变化。我做错了什么?
class quadrupole(QtWidgets.QGraphicsItem):
status_color = [QtGui.QColor('#626262'), QtGui.QColor('#32FF62')]
def __init__(self, name):
super(quadrupole, self).__init__()
self.name = name
self._on = False
#self.setBrush(QtGui.QBrush(quadrupole.status_color[self._on]))
self.bordersize = 2
def power(self):
self._on = not self._on
self.update()
print(f'power changed to {self._on}')
#self.setBrush(QtGui.QBrush(quadrupole.status_color[self._on]))
def boundingRect(self):
return QtCore.QRectF(-25 - self.bordersize / 2, -70 - self.bordersize / 2,
25 + self.bordersize, 25 + self.bordersize)
def paint(self, painter=None, style=None, widget=None):
painter.setRenderHint(QtGui.QPainter.Antialiasing)
#painter.setBrush(painter.brush())
rect = QtCore.QRectF(-25, -25, 50, 50)
painter.rotate(45)
painter.fillRect(rect, quadrupole.status_color[self._on])
painter.rotate(-45)
font = painter.font()
font.setPixelSize(14)
painter.setFont(font)
painter.setPen(QtGui.QColor('#FBFBFB'))
t = QtCore.QRectF(-25, -70, 50, 20)
painter.drawText(t, QtCore.Qt.AlignCenter, self.name)
解决了。画家被错误的boundingRect限制了