2

是否可以在 QML 组件中的两个状态之间使用不同的过渡动画?以下示例不起作用并且程序崩溃(Linux 上的分段错误):

import QtQuick 1.0

Rectangle {
    id: canvas
    height: 500; width: 600

    Rectangle { id: rect; color: "#04A"; height: 100; width: 100 }

    state: "A"
    states: [
        State { name: "A"; PropertyChanges { target: rect; x: 0; y: 100 } },
        State { name: "B"; PropertyChanges { target: rect; x: 500; y: 100 } }
    ]


    transitions: trans1

    property list<Transition> trans1: [
        Transition {
            NumberAnimation { target: rect; property: "x"; duration: 500 }
        }
    ]

    property list<Transition> trans2: [
        Transition {
            from: "A"; to: "B"
            SequentialAnimation {
                NumberAnimation { target: rect; property: "x"; from: 0; to: -100; duration: 250 }
                NumberAnimation { target: rect; property: "x"; from: 600; to: 500; duration: 250 }
            }
        },
        Transition {
            from: "B"; to: "A"
            SequentialAnimation {
                NumberAnimation { target: rect; property: "x"; from: 500; to: 600; duration: 250 }
                NumberAnimation { target: rect; property: "x"; from: -100; to: 0; duration: 250 }
            }
        }
    ]


    // test script /////////////////////////////////////////////////////////

    Timer { interval: 1000; running: true; onTriggered: canvas.state = "B" }
    Timer { interval: 2000; running: true; onTriggered: canvas.state = "A" }

    // change kind of transition
    Timer { interval: 3000; running: true; onTriggered: canvas.transitions = trans2 }

    Timer { interval: 4000; running: true; onTriggered: canvas.state = "B" }
    Timer { interval: 5000; running: true; onTriggered: canvas.state = "A" }
}

QML-Doc说该属性transtition只读的,但通常会Transition{...}为该属性分配一个元素列表,所以它不能是真正的只读,不是吗?

一种解决方案是使用 4 个状态,例如A1、和B1,并定义一个 和 之间的转换,看起来像,另一个转换 和 之间,看起来像。 但是我想知道在不引入新状态的情况下是否可以实现这样的事情。A2B2A1B1trans1A2B2trans2


编辑:

gregschlom更改from/属性的建议to有效,这里有一个例子:

import QtQuick 1.0

Rectangle {
    id: canvas
    height: 500; width: 600

    Rectangle { id: rect; color: "#04A"; height: 100; width: 100 }

    state: "A"
    states: [
        State { name: "A"; PropertyChanges { target: rect; x: 0; y: 100 } },
        State { name: "B"; PropertyChanges { target: rect; x: 500; y: 100 } }
    ]


    property int transType: 1

    transitions: [
        Transition {
            from: transType == 1 ? "*" : "none"
            to:   transType == 1 ? "*" : "none"
            ParallelAnimation {
                RotationAnimation { target: rect; property: "rotation"; from: 0; to:360; duration: 500 }
                NumberAnimation { target: rect; property: "x"; duration: 500 }
            }
        },
        Transition {
            from: transType == 2 ? "*" : "none"
            to:   transType == 2 ? "*" : "none"
            NumberAnimation { target: rect; property: "x"; duration: 500 }
        }
    ]


    // test script /////////////////////////////////////////////////////////

    Timer { interval: 1000; running: true; onTriggered: canvas.state = "B" }
    Timer { interval: 2000; running: true; onTriggered: canvas.state = "A" }

    // change kind of transition
    Timer { interval: 3000; running: true; onTriggered: canvas.transType = 2 }

    Timer { interval: 4000; running: true; onTriggered: canvas.state = "B" }
    Timer { interval: 5000; running: true; onTriggered: canvas.state = "A" }
}
4

1 回答 1

2

以下示例不起作用并且程序崩溃(Linux 上的分段错误):

请务必向https://bugreports.qt.io报告此类崩溃。崩溃具有最高优先级,通常会在几天内修复。

QML-Doc 说转换属性是只读的,但通常将一个 Transition{...} 元素列表分配给这个属性,所以它不能是真正的只读,不是吗?

认为在运行时分配(即:在脚本中)与在你的 qml 文件中分配不同,所以当说“只读”时,它们可能意味着你不应该在初始化后更改值。

无论如何,如果文档说它是只读的,并且当您尝试写入它时它会崩溃,那么您知道您可能不应该这样做...... :)

但是我想知道在不引入新状态的情况下是否可以实现这样的事情。

Transitions 上的 qml 文档说:

如果指定了多个转换,则只有一个(最佳匹配)转换将应用于任何特定的状态更改。在上面的示例中,当更改为 state1 时,将使用第一个转换,而不是更通用的第二个转换。

因此,您可以尝试一次定义所有转换并动态更改“to”和“from”设置以尝试启用/禁用转换。不确定它是否可能,但它可以工作。

于 2011-07-01T09:17:00.507 回答