最近,我一直在努力理解pyqtProperty的用法.在下面的代码段中,我尝试创建一个自定义按钮,单击该按钮将更改其文本和外观.我想将逻辑和外观分开,所以我创建了自定义属性“ state”,该属性在样式表中使用.
结果对我来说有点奇怪.程序抛出“ CustomButton”对象没有属性“ _state”异常,但仍会运行.单击按钮后,按钮的文本会更改,但颜色保持不变.
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtGui, QtCore
STYLE = '''
CustomButton {
margin: 50px;
padding: 10px;
}
CustomButton[state='true'] {
background: yellowgreen;
}
CustomButton[state='false'] {
background: orangered;
}
'''
class CustomButton(QtGui.QPushButton):
def __init__(self):
super(CustomButton, self).__init__()
self.setText('ON')
self.setStyleSheet(STYLE)
self._state = True
def g_state(self):
return self._state
def s_state(self, value):
self._state = value
state = QtCore.pyqtProperty(bool, fget=g_state, fset=s_state)
def mousePressEvent(self, event):
print 'clicked'
if self.state == True:
self.state = False
self.setText('OFF')
else:
self.state = True
self.setText('ON')
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
b = CustomButton()
b.show()
sys.exit(app.exec_())
解决方法:
这里有几个问题.
首先,重新实现的mousePressEvent应该调用基类实现,以保留正常的行为.
其次,对属性的更改不会自动触发样式更改,因此您需要在代码中显式地进行更改.
另外,值得注意的是pyqtProperty可以用作装饰器,这可能会稍微提高代码的可读性.
因此,经过这些更改,代码可能看起来像这样:
...
@QtCore.pyqtProperty(bool)
def state(self):
return self._state
@state.setter
def state(self, value):
self._state = value
def mousePressEvent(self, event):
print 'clicked'
if self.state:
self.state = False
self.setText('OFF')
else:
self.state = True
self.setText('ON')
self.style().unpolish(self)
self.style().polish(self)
self.update()
super(CustomButton, self).mousePressEvent(event)