亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

動畫不更新

動畫不更新

Smart貓小萌 2022-01-11 17:56:05
我正在嘗試重做這個 Qt 代碼https://github.com/laserpants/qt-material-widgets但使用 Python 和 PyQt4 并且我遇到了動畫問題。我想重新創建示例的復選框,除動畫外一切正常;它不更新。主要問題是我想保留按鈕的 stateMachine 和轉換,但我找到的解決方案不使用它們。我只希望圖標在單擊時淡入淡出任何想法為什么這不起作用?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)
查看完整描述

1 回答

?
幕布斯7119047

TA貢獻1794條經驗 獲得超8個贊

每次不透明度更改時都必須調用 update() 方法,因此創建 pyqtProperty 比創建動態屬性更好:


import os

from PyQt4 import QtCore, QtGui


root_path = os.path.dirname(os.path.realpath(__file__))

icons_path = file = os.path.join(root_path, "icons")



class MaterialCheckBox(QtGui.QWidget):

    clicked = QtCore.pyqtSignal()

    toggled = QtCore.pyqtSignal(bool)


    def __init__(self, parent=None):

        super(MaterialCheckBox, self).__init__(parent)

        self._is_checked = False


        checkedIcon = MaterialIcon(

            self, os.path.join(icons_path, "baseline-check_box-24px.svg")

        )

        uncheckedIcon = MaterialIcon(

            self,

            os.path.join(

                icons_path, "baseline-check_box_outline_blank-24px.svg"

            ),

        )


        stateMachine = QtCore.QStateMachine(self)


        checkedState = QtCore.QState()

        checkedState.assignProperty(self, b"checked", True)

        checkedState.assignProperty(checkedIcon, b"opacity", 1.0)

        checkedState.assignProperty(uncheckedIcon, b"opacity", 0.0)


        uncheckedState = QtCore.QState()

        uncheckedState.assignProperty(self, b"checked", False)

        uncheckedState.assignProperty(checkedIcon, b"opacity", 0.0)

        uncheckedState.assignProperty(uncheckedIcon, b"opacity", 1.0)


        stateMachine.addState(checkedState)

        stateMachine.addState(uncheckedState)

        stateMachine.setInitialState(uncheckedState)


        duration = 2000


        transition1 = checkedState.addTransition(self.clicked, uncheckedState)

        animation1 = QtCore.QPropertyAnimation(

            checkedIcon, b"opacity", self, duration=duration

        )

        transition1.addAnimation(animation1)

        animation2 = QtCore.QPropertyAnimation(

            uncheckedIcon, b"opacity", self, duration=duration

        )

        transition1.addAnimation(animation2)


        transition2 = uncheckedState.addTransition(self.clicked, checkedState)

        animation3 = QtCore.QPropertyAnimation(

            checkedIcon, b"opacity", self, duration=duration

        )

        transition2.addAnimation(animation3)

        animation4 = QtCore.QPropertyAnimation(

            uncheckedIcon, b"opacity", self, duration=duration

        )

        transition2.addAnimation(animation4)


        stateMachine.start()


    def sizeHint(self):

        return QtCore.QSize(24, 24)


    def isChecked(self):

        return self._is_checked


    def setChecked(self, value):

        if self._is_checked != value:

            self._is_checked = value

            self.toggled.emit(self._is_checked)


    checked = QtCore.pyqtProperty(

        bool, fget=isChecked, fset=setChecked, notify=toggled

    )


    def mousePressEvent(self, event):

        self.clicked.emit()

        self.update()

        super(MaterialCheckBox, self).mousePressEvent(event)



class MaterialIcon(QtGui.QWidget):

    opacityChanged = QtCore.pyqtSignal()


    def __init__(self, parent, address):

        super(MaterialIcon, self).__init__(parent)

        self.icon = QtGui.QPixmap(address)

        self._opacity = 0.0


    def opacity(self):

        return self._opacity


    def setOpacity(self, o):

        if o != self._opacity:

            self._opacity = o

            self.opacityChanged.emit()

            self.update()


    opacity = QtCore.pyqtProperty(

        float, fget=opacity, fset=setOpacity, notify=opacityChanged

    )


    def paintEvent(self, event):

        painter = QtGui.QPainter(self)

        painter.setOpacity(self.opacity)

        mask = QtGui.QPainter(self.icon)

        mask.setCompositionMode(QtGui.QPainter.CompositionMode_SourceIn)

        mask.fillRect(self.icon.rect(), QtGui.QColor(0, 158, 227))

        mask.end()

        painter.drawPixmap(0, 0, self.icon)



if __name__ == "__main__":

    import sys


    app = QtGui.QApplication(sys.argv)

    w = MaterialCheckBox()

    w.show()

    sys.exit(app.exec_())


查看完整回答
反對 回復 2022-01-11
  • 1 回答
  • 0 關注
  • 215 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號