我试图重做这个Qt代码https://github.com/laserpants/qt-material-widgets,但使用Python&PyQt4时,我遇到了动画方面的问题。你知道吗
我想重新创建这个示例的复选框,除了动画之外,其他一切都正常工作;它不会更新。你知道吗
主要问题是我想保留按钮的状态机和转换,但是我找到的解决方案没有使用它们。你知道吗
我只希望图标在单击时淡入淡出
你知道为什么这样不行吗?你知道吗
class materialCheckBox(QWidget):
clicked = pyqtSignal()
def __init__(self, parent):
super(materialCheckBox,self).__init__(parent)
self.setProperty("value", bool)
checkedIcon = materialIcon(self, "C:/Users/User/.qgis2/python/plugins/Material/icons/baseline-check_box-24px.svg")
uncheckedIcon = materialIcon(self, "C:/Users/User/.qgis2/python/plugins/Material/icons/baseline-check_box_outline_blank-24px.svg")
self.stateMachine = QStateMachine()
self.checkedState = QState()
self.checkedState.assignProperty(self, "value", True)
self.checkedState.assignProperty(checkedIcon, "opacity", 1.0)
self.checkedState.assignProperty(uncheckedIcon, "opacity", 0.0)
self.uncheckedState = QState()
self.uncheckedState.assignProperty(self, "value", False)
self.uncheckedState.assignProperty(checkedIcon, "opacity", 0.0)
self.uncheckedState.assignProperty(uncheckedIcon, "opacity", 1.0)
self.stateMachine.addState(self.checkedState)
self.stateMachine.addState(self.uncheckedState)
self.stateMachine.setInitialState(self.uncheckedState)
transition1 = self.checkedState.addTransition(self.clicked, self.uncheckedState)
animation1 = QPropertyAnimation(checkedIcon, "opacity", self)
animation1.setDuration(2000)
transition1.addAnimation(animation1)
animation2 = QPropertyAnimation(uncheckedIcon, "opacity", self)
animation2.setDuration(2000)
transition1.addAnimation(animation2)
transition2 = self.uncheckedState.addTransition(self.clicked, self.checkedState)
animation3 = QPropertyAnimation(checkedIcon, "opacity", self)
animation3.setDuration(2000)
transition2.addAnimation(animation3)
animation4 = QPropertyAnimation(uncheckedIcon, "opacity", self)
animation4.setDuration(2000)
transition2.addAnimation(animation4)
self.stateMachine.start()
self.clicked.connect(self.update)
self.setGeometry(0, 0, 24, 24)
def isChecked(self):
return self.property("value")
def mousePressEvent(self, event):
self.clicked.emit()
class materialIcon(QWidget):
def __init__(self, parent, address):
super(materialIcon, self).__init__(parent)
self.icon = QPixmap(address)
self.setProperty("opacity", float)
def paintEvent(self, event):
painter = QPainter(self)
painter.begin(self)
painter.setOpacity(self.property("opacity"))
mask = QPainter(self.icon)
mask.begin(self.icon)
mask.setCompositionMode(QPainter.CompositionMode_SourceIn)
mask.fillRect(self.icon.rect(), QColor(0, 158, 227))
mask.end()
painter.drawPixmap(0, 0, self.icon)
painter.end()
每次不透明度更改时都必须调用update()方法,因此创建pyqtProperty比创建动态属性更好:
完整的例子是here。你知道吗
相关问题 更多 >
编程相关推荐