QML之状态(States)、过渡(Transitions)的个人理解和简单使用

本文详细介绍了QML中状态和过渡的使用方法,通过示例展示了状态切换如何控制UI元素的视觉效果,以及如何使用Transitions实现平滑的动画效果。作者还分享了对状态和过渡概念的理解,以及在实际项目中的应用技巧。
摘要由CSDN通过智能技术生成

一、状态、过渡的使用示例

1.携带过渡效果的状态切换演示

下图通过双击图形触发状态的更新(槽函数),首次双击并未指定状态过渡效果(所以变化突兀),仅在两个图形之间状态的切换指定过渡(因此仅图形之间双击才会触发下图效果)。
请添加图片描述

2.直接切换State的效果演示

下图左侧按钮按下或右侧复选框勾选时设置新效果,两个按钮代表分别代表两个图形的位置更新
请添加图片描述

二、状态与过渡的个人理解

1.关于状态(States)

状态是指UI元素在不同的场景或用户操作下的一种特定效果(如鼠标悬浮在按钮上,按钮颜色变化等效果)。

  1. 设置后仅仅是临时的改变QML元素的属性,当状态置空时相应属性值会恢复至初始状态。
  2. 状态设置存在先后顺序,当同一时间满足两个状态条件时,只有先定义的State会生效。

2.关于过渡(Transitions)

过渡用于QML元素状态之间平滑切换(动画效果个性化定义)

  1. 当State中某参数是根据原参数值关联变化(如width:width*2的关系),设置该参数动画时,动画结束时可能会以结束时的参数值做原参值关联变化(会有突兀变化的效果)。
    如下图:
    在这里插入图片描述

三、源码

import QtQuick 2.0

import QtQuick.Controls 2.12

Item {
    id: satt

    // (状态)按钮
    Button {
        id: leftRectMoveBtn
        text: "左侧矩形移位"
    }

    // (状态)复选框
    CheckBox {
        id: rightRectMoveCheckBox
        text: "右侧矩形移位"
        checked: false
        // 锚点布局
        anchors.left: leftRectMoveBtn.right
    }

    // 测试按钮
//    Button {
//        id: testBtn
//        text: "状态置空"
//        onClicked: satt.state = "" //console.log(leftRect.width)
//        anchors.left: rightRectMoveCheckBox.right
//        anchors.leftMargin: 20

//    }

    // 定义第一个图形
    Rectangle {
        id: leftRect
        x: 100
        y: 100
        width: 50
        height: width
        radius: width / 2
        color: "skyblue"
        // 状态更新操作
        MouseArea {
            id: leftMouseArea
            anchors.fill: parent
            onDoubleClicked: satt.state = "leftDoubleClickWithTransition"
        }
    }


    // 定义第二个图形
    Rectangle {
        id:  rightRect
        x: 200
        y: 100
        width: leftRect.width
        height: width
        radius: width / 2
        color: "lightgray"
        // 状态更新操作
        MouseArea {
            id: rightMouseArea
            anchors.fill: parent
            onDoubleClicked: satt.state = "rightDoubleClickWithTransition"
        }
    }

    states: [
        // **********
        // Sate通过when指定属性变化使用状态
        // **********
        State {
            name: "leftRectMove"
            // 当leftRectMoveBtn按下时触发,释放即恢复
            when: leftRectMoveBtn.pressed
            // 位置属性值更新
            PropertyChanges {
                target: leftRect
                x: 300
                y: 300
            }
        },

        State {
            name: "rightRectMove"
            // 当rightRectMoveCheckBox勾选时触发,取消勾选即恢复
            when: rightRectMoveCheckBox.checkState
            // 位置属性值更新
            PropertyChanges {
                target: rightRect
                x: 5
                y: 300
            }
        },

        // **********
        // Sate通过主动设置使用状态
        // **********
        State {
            name: "leftDoubleClickWithTransition"
            // 左侧元素 宽度、高度、颜色变化
            PropertyChanges {
                target: leftRect
                width: 100
                height: width
                color: "blue"
            }

            // 右侧元素 宽度、高度、颜色变化
            PropertyChanges {
                target: rightRect
                width: 50
                height: width
                color: "lightgray"
            }
        },

        State {
            name: "rightDoubleClickWithTransition"
            // 左侧侧元素 宽度、高度、颜色变化
            PropertyChanges {
                target: leftRect
                width: 50
                height: width
                color: "lightblue"
            }

            // 右侧元素 宽度、高度、颜色变化
            PropertyChanges {
                target: rightRect
                width: 100
                height: width
                color: "gray"
            }
        }
    ]

    // **********
    // Sate切换的过渡
    // **********
    transitions: [
        Transition {
            from: "leftDoubleClickWithTransition"
            to: "rightDoubleClickWithTransition"
            // 左侧图形的动画效果设置
            ColorAnimation {
                target: leftRect
                properties: "color"
                duration: 1500
            }
            PropertyAnimation {
                target: leftRect
                properties: "width,height"
                duration: 1500
            }

            // 右侧熟悉的动画属性设置
            ColorAnimation {
                target: rightRect
                properties: "color"
                duration: 1500
            }
            PropertyAnimation {
                target: rightRect
                properties: "width,height"
                duration: 1500
            }
        },

        Transition {
            from: "rightDoubleClickWithTransition"
            to: "leftDoubleClickWithTransition"
            // 左侧图形的动画效果设置
            ColorAnimation {
                target: leftRect
                properties: "color"
                duration: 1500
            }
            PropertyAnimation {
                target: leftRect
                properties: "width,height"
                duration: 1500
            }

            // 左侧图形的动画效果设置
            ColorAnimation {
                target: rightRect
                properties: "color"
                duration: 1500
            }
            PropertyAnimation {
                target: rightRect
                properties: "width,height"
                duration: 1500
            }
        }
    ]
}

友情提示——哪里看不懂可私哦,让我们一起互相进步吧
(创作不易,请留下一个免费的赞叭 谢谢 ^o^/)

注:文章为作者编程过程中所遇到的问题和总结,内容仅供参考,若有错误欢迎指出。
注:如有侵权,请联系作者删除

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lw向北.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值