PropertyAction不能与" AnchorChanges"元素

时间:2015-08-04 19:35:51

标签: qt qml qtquick2 qt-quick

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

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的动画,但我不认为这是正确的方法。语法有问题,或者必须使用其他方法吗?

1 个答案:

答案 0 :(得分:3)

我问过Qt的支持,这就是他们所说的:

  

发生这种情况的原因是因为正在处理锚   不同而不是作为属性动画。你需要做的是   改为使用AnchorAnimation并为其设置持续时间   以您想要的速度移动。如果你想改变锚   立即生效,然后就可以了:

AnchorAnimation { 
    duration: 1  
}
     

并且在动画的其余部分发生之前它会立即移动。

答案是:您的方法正确。没有简单的方法可以省略AnchorAnimation