Qt 动画只工作一次,单击一次即可

Qt 动画只工作一次,单击一次即可,qt,qml,Qt,Qml,我曾尝试用动画制作菜单,但动画只工作一次。 我可以打开它,但再次单击后它不会关闭,如下图所示: 菜单的代码: // The rectangle for the menu Rectangle { id: menuRectangle width: 91 color: "#1e1e24" anchors.left: parent.left anchors.top: parent.top

我曾尝试用动画制作菜单,但动画只工作一次。 我可以打开它,但再次单击后它不会关闭,如下图所示:

菜单的代码:

// The rectangle for the menu
Rectangle {
        id: menuRectangle
        width: 91
        color: "#1e1e24"
        anchors.left: parent.left
        anchors.top: parent.top
        anchors.bottom: parent.bottom
        anchors.topMargin: 64
        anchors.bottomMargin: 5
        anchors.leftMargin: 5

        // Checks if the menu is open or not            
        property bool menuOut: false


        // Part of the menu            

        Column {
            id: column
            width: 90
            anchors.fill: parent
            anchors.rightMargin: 0
            anchors.bottomMargin: 0
            anchors.leftMargin: 0
            anchors.topMargin: 0

            CustomMenuBtn{
                btnIconSource: "images/settings_icon.svg"
                text: "ATTACK"
                anchors.left: parent.left
                anchors.right: parent.right
                anchors.leftMargin: 0
                anchors.rightMargin: 0
            }
        }


        // The button that runs the animation            

        CustomMenuBtn {
            anchors.bottom: parent.bottom
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.bottomMargin: 30
            text: ""
            btnIconSource: "images/settings_icon.svg"
            onClicked: menuAnimation.running = true
        }


        //The animation

        PropertyAnimation{
            id: menuAnimation
            target: menuRectangle
            property: "width"
            to: if(menuRectangle.menuOut)
                {

                    menuRectangle.menuOut = false
                    return 91;
                }
                else
                {
                    menuRectangle.menuOut = true
                    console.log(menuRectangle.menuOut)
                    return 170;
                }

            easing.type: Easing.InOutQuint
            duration: 1000
        }
    }
此外,我还尝试使用console.log,但由于某些原因,我看不到输出。
有人知道吗?

在动画的
to
属性上有一个绑定循环。它的值取决于
menuOut
,但它也会更改
menuOut
,这会将
更改为
,从而形成一个循环

我认为最好有单独的打开和关闭动画,并确保两者都不依赖于
menuOut
。这里有一个解决方案:

        CustomMenuBtn {
            onClicked: {
                if (menuRectangle.menuOut) {
                    closeAnimation.start()
                } else {
                    openAnimation.start()
                }
                menuRectangle.menuOut = !menuRectangle.menuOut
            }
        }

        PropertyAnimation{
            id: openAnimation
            target: menuRectangle
            property: "width"
            to: 170
            easing.type: Easing.InOutQuint
            duration: 1000
        }

        PropertyAnimation{
            id: closeAnimation
            target: menuRectangle
            property: "width"
            to: 91
            easing.type: Easing.InOutQuint
            duration: 1000
        }

什么,但是我在if()之后更改了menuOut,它在我所知道的任何其他编程语言中都不是这样工作的,而且没有任何意义@JarManIt不要紧。问题在于,它在绑定本身中发生了更改。试试我的解决办法。它是有效的。我相信你,但即使它是真的,它是如何首先打开的?当你调用
openAnimation.start()
时,它会从
onClicked
处理程序打开菜单。