gpt4 book ai didi

python - 更新从其他函数创建的paintEvent对象的颜色

转载 作者:行者123 更新时间:2023-12-01 01:33:13 25 4
gpt4 key购买 nike

我在paintEvent函数中使用QPainter创建了一个矩形。以下是我的代码

def paintEvent(self, event):
QWidget.paintEvent(self, event)
painter = QPainter(self)
pen = QtGui.QPen(QtCore.Qt.black, 2, QtCore.Qt.SolidLine)

painter.setPen(QtCore.Qt.red)
painter.setBrush(QtGui.QColor(200, 0, 0))
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, str("Welcome"))

如何从其他函数更新矩形和文本内容(矩形内)的颜色?

def updateRectanle(conditon):
if condition:
update_rectangle_color
update_rectangle_text

最佳答案

正如您所知,您不应该直接调用 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()

关于python - 更新从其他函数创建的paintEvent对象的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52623040/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com