我已经厌倦了搜索!
我从QPushbutton子类化了一个按钮,并将我的QSS设置为它。样式是期望的。
我想要的只是当鼠标悬停在按钮上时(触发enter事件),按钮的颜色会在特定时间内(例如0.2秒)变化,而不是立即改变(一种柔和的颜色变化)。
我该怎么办?
*******PyQt4中的答案*********
class MyButton(QPushButton):
def __init__(self):
super(MyButton, self).__init__()
self.setMinimumSize(80,50)
self.setText('QPushButton')
def getColor(self):
return Qt.black
def setColor(self, color):
self.setStyleSheet("background-color: rgb({0}, {1}, {2});border:none;".format(color.red(), color.green(), color.blue()))
color=QtCore.pyqtProperty(QColor, getColor, setColor)
def enterEvent(self, event):
global anim
anim=QPropertyAnimation(self, "color")
anim.setDuration(200)
anim.setStartValue(QColor(216, 140, 230))
anim.setEndValue(QColor(230, 230, 230))
anim.start()
def leaveEvent(self, event):
self.setStyleSheet("background:none;")
anim
不是全局的,当方法执行完毕时就会被销毁。我在anim=QPropertyAnimation(self, "color")
前添加了一行global anim
,然后它就正确地进行了动画处理。 - Shf