Qt 具有多个(从、到)匹配项的QML转换

Qt 具有多个(从、到)匹配项的QML转换,qt,qml,qtquick2,Qt,Qml,Qtquick2,我有一个简单的QML脚本,它有四个状态,你要经历一个循环,比如说状态是1,2,3,4 从一到二以及从三到四的过渡是相同的 有没有一种简单的方法来定义它而不复制转换代码? 这不起作用: transitions: Transition { from: "one"; to: "two"; from: "three"; to: "four"; ParallelAnimation { NumberAnimation { p

我有一个简单的QML脚本,它有四个状态,你要经历一个循环,比如说状态是1,2,3,4

从一到二以及从三到四的过渡是相同的

有没有一种简单的方法来定义它而不复制转换代码? 这不起作用:

transitions: Transition {
         from: "one"; to: "two"; 
         from: "three"; to: "four"; 
         ParallelAnimation {
             NumberAnimation { properties: "x,y"; duration: 500; easing.type: Easing.InOutQuad }
             ColorAnimation { duration: 500 }
         }
 }
从文档来看,似乎没有办法指定这一点,但是

transitions: [ Transition {
             from: "one"; to: "two"; 
             ParallelAnimation {
                 NumberAnimation { properties: "x,y"; duration: 500; easing.type: Easing.InOutQuad }
                 ColorAnimation { duration: 500 }
             }
         }, Transition {
             from: "three"; to: "four"; 
             ParallelAnimation {
                 NumberAnimation { properties: "x,y"; duration: 500; easing.type: Easing.InOutQuad }
                 ColorAnimation { duration: 500 }
             }
         }] 

我在一个非常简单的应用程序上遇到了类似的问题,这不是一个很好的方法,但我发现的唯一方法是:

transitions: 
    Transition {
         from: "two"; to: "three"; 
         //whatever animation you have for this
    },
    //and obviously any other transitions you may have

    //any transition you don't specify will do this:
    Transition {
         from: "*"; to: "*"; 
         ParallelAnimation {
             NumberAnimation { properties: "x,y"; duration: 500; easing.type: Easing.InOutQuad }
             ColorAnimation { duration: 500 }
         }
 }

可以通过为动画指定id来重用动画

import QtQuick 2.0
import QtQuick.Controls 1.0

Rectangle {
    id: root
    width: 400; height: width

    Column {
        spacing: 20
        Repeater {
            model: ["one", "two", "three", "four"]
            Button {
                text: modelData
                onClicked: rect.state = modelData
            }
        }
    }

    Rectangle {
        id: rect
        x: 200; y: 0
        width: 60; height: width
        color: "red"

        state: "one"
        states: [
            State { name: "one" },
            State { name: "two" },
            State { name: "three" },
            State { name: "four" }
        ]
        NumberAnimation { id: na1; target: rect; property: "y"; to: 200 }
        NumberAnimation { id: na2; target: rect; property: "y"; to: 0 }
        transitions: [
            Transition { from: "one"; to: "two";  animations: na1 },
            Transition { from: "two"; to: "three";  animations: na2 },
            Transition { from: "three"; to: "four";  animations: na1 },
            Transition { from: "four"; to: "one";  animations: na2 }
        ]
    }
}

我想知道你是否知道这个概念。在您的代码中,
ParallelAnimation
被指定给
转换
的默认属性
animations
,您可以显式地将动画(通过其id)指定给该默认属性。

是的,这不是最好的解决方案,但方向正确。谢谢。嗯,我想没有简单的办法,你的答案是最好的(8^)