我目前正在尝试让自定义代理通过一个外部按钮来更改它正在绘制的颜色。我目前让它工作,因为按钮将改变代理绘制的颜色,但代理不会在按下按钮时更新,而是在我将鼠标移到相关查看器上时更新。
我为颜色做了一个自定义按钮(非常简单)
class ColorButton(QPushButton):
def __init__(self,parent=None,color=None):
super(ColorButton,self).__init__(parent)
self.color=color
def paintEvent(self,e):
painter=QPainter()
painter.begin(self)
painter.fillRect(self.rect(),self.color)
painter.end()
def getColor(self):
return self.color然后,我将彩色按钮的pressed()信号连接到我的委托的updateBG函数。
button1=ColorButton(color=QColor(0,0,0))
button1.pressed.connect(delegate.updateBG)委托的updateBG()函数只是更新自身内的颜色变量。
def updateBG(self):
color=self.sender().getColor()
self.bgBrush=QBrush(color)我让paint()函数只在绘制矩形时使用这种颜色。
def paint(self,painter,option,index):
painter.save()
painter.setBrush(self.bgBrush)
painter.drawRect(self.rect())
painter.restore()有没有办法强制代理重新绘制?update()和repaint()函数都不适用于委托本身。我必须在我的查看器(即QListView)上调用repaint()吗?如果这是唯一的方法,有没有从委托中获取查看器的方法?或者我应该将这个updateBG()函数移到委托类之外?
我通过QT.py使用QT5 (有效的pyside2或PyQt5)。
发布于 2018-03-06 07:20:22
我发现我可以通过将pressed()信号连接到我的查看器的重置函数来强制更新。
button1.pressed.connect(viewer.reset)不确定这是否是最好的方法,但它似乎有效。
https://stackoverflow.com/questions/49119611
复制相似问题