PropertyAction 不适用于 "AnchorChanges" 元素

PropertyAction doesn't work with an "AnchorChanges" element

这是一个显示我的问题的测试:

Window {
visible: true;width: 360;height: 360

MouseArea{
    anchors.fill: parent
    onClicked: container.state = (container.state=="estado1"?"estado2":"estado1")
}

Rectangle {
    id: container
    anchors.fill: parent
    color: "red"
    state: "estado1"
    onStateChanged:console.log("state -> "+state)

    Rectangle {
        id: prueba
        anchors.left: parent.left
        height: 100
        color: "blue"
        onWidthChanged:console.log("width -> "+width)
        onHeightChanged:console.log("height -> "+height)
        onOpacityChanged:console.log("opacity -> "+opacity)
        onYChanged: console.log("coordY -> "+y)

    }
    states: [
        State {
            name: "estado1"
            PropertyChanges {
                target: prueba
                width: 300
                opacity: 1
            }
            AnchorChanges {
                target: prueba
                anchors.bottom: container.bottom
            }
        },
        State {
            name: "estado2"
            PropertyChanges {
                target: prueba
                width: 50
                opacity: 0.5
            }
            AnchorChanges {
                target: prueba
                anchors.top: container.top
            }
        }
    ]
    transitions:[
        Transition {
            ParallelAnimation {
                PropertyAnimation {
                    target: prueba
                    properties: "width"
                    duration: 3000
                }
                PropertyAction {
                    target: prueba
                    property: "opacity"
                }
                /*PropertyAction {
                    target: prueba
                    property: "anchors.top" //doesn't work
                    //property: "anchors" //doesn't work neither
                }*/
                AnchorAnimation {
                    //works, but doesn't seem to be the most
                    //elegant solution to the problem
                    duration: 0
                }
            }
        }
    ]
}
}

在这里,您可以看到一个项目有两个状态,用 PropertyChanges 改变几个属性,还有一个 AnchorChanges。此外,还定义了一个 Transition 来控制状态变化。在该过渡中,width 属性 使用 PropertyChanges 元素进行动画处理,并且 opacity 在没有动画的过渡开始时更改,使用 PropertyAction.

问题是我还想更改没有动画的锚点。但是,如果我尝试使用 PropertyAction,它不起作用。 我可以使用持续时间为 0 的动画,但我认为这不是正确的方法。语法有问题吗,或者必须使用其他方法?

我询问了 Qt 支持,他们是这样说的:

The reason this is happening is because the anchor is being dealt with differently and not as a property animation. What you need to do is use the AnchorAnimation instead and set a duration for it so that it does the move at the rate you want. If you want the anchor change to have an effect right away then you can do:

AnchorAnimation { 
    duration: 1  
}

and it will instantly move first before the rest of the animation takes place.

答案是:你的做法正确。没有简单的方法可以省略使用 AnchorAnimation.