I have a custom GraphicsItem with a method to toggle on/off (painting the square in green/black). But calling the method power results in no change. What I'm doing wrong?
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)
Solved. The painter was constrained by a wrong boundingRect