你不应该打电话
paintEvent()
正如您已经知道的,您应该做的就是告诉GUI您需要重新绘制,我们可以使用这个方法
repaint()
和
update()
. 第一个方法要求同步更新,而在第二个方法中,我更喜欢第二个方法。
def __init__(self, foo_params):
super(Foo_class, self).__init__(super_foo_params)
self._text = "Welcome"
self._rect_color = QtGui.QColor(200, 0, 0)
def paintEvent(self, event):
super(Foo_class, self).paintEvent(event)
painter = QPainter(self)
pen = QtGui.QPen(QtCore.Qt.black, 2, QtCore.Qt.SolidLine)
painter.setPen(QtCore.Qt.red)
painter.setBrush(self._rect_color)
rect = QRect(1450,325,380,498)
painter.drawRect(rect)
painter.setFont(QtGui.QFont('Consolas', 30))
painter.setPen(QtGui.QColor(0, 0, 0))
painter.drawText(QRect(1450,325,380,498), QtCore.Qt.AlignCenter, self._text)
def updateRectanle(conditon):
if condition:
self._rect_color = new_color
self._text = new_text
self.update()