QML 如何使Behavior 作用于多个属性

问题描述 投票:0回答:1

有没有办法让Behavior同时作用于多个属性来简化代码?

ApplicationWindow {
    width: 1280
    height: 720
    visible: true
    title: qsTr("Hello World")
    color:"#202020"

    Rectangle{
        id:rect
        x:100
        y:100
        width: 100
        height: 50
        color:"red"

        Behavior on x, y, width, height, color{ //there will be error here
            PropertyAnimation{duration:1000}
        }
    }

    Button{
        onClicked: {
            rect.x=300
            rect.y=400
            rect.width=200
            rect.height100
            rect.color="green"
        }
    }
}

我希望所有5个属性都有平滑的变化效果,所以我必须定义5个Behaviors并将它们分别应用到这5个属性中的每一个,我认为这很麻烦。

qt qml qtquick2 behavior qt-quick
1个回答
0
投票

虽然不完全相同,但我们可以用状态和转换来近似:

import QtQuick
import QtQuick.Controls
Page {
    id: root
    Rectangle {
        id: rect
        x:100
        y:100
        width: 100
        height: 50
        color:"red"
        states: State {
            name: "moved"; when: chk.checked
            PropertyChanges { target: rect; x: 300; y: 400; width: 200; height: 100; color: "green" }
        }
        transitions: Transition {
            NumberAnimation { properties: "x,y,width,height,color"; easing.type: Easing.InOutQuad; duration: 1000 }
        }
    }
    CheckBox {
        id: chk
        text: "Move"
    }
}

您可以在线尝试!

© www.soinside.com 2019 - 2024. All rights reserved.