6. QML自定义两种等待提示框

1. 说明:

在QT的低版本和高版本都提供了BusyIndicator控件,但是在某种情况下,这种提供的原生等待提示框的效果并不能达到项目需求,所以本篇文章参考原生效果将代码重现,可以方便更改样式。
参考博客:https://blog.csdn.net/gongjianbo1992/article/details/112748866
效果展示:(两者差别不大,仔细看还是有差别的…)

自定义等待提示框

2. 完整代码:

WaitTipOne.qml:

import QtQuick 2.15

Item{
    id:control

    implicitHeight: 60
    implicitWidth: 60

    property int itemCount:10
    property int itemSize:8
    property int itemLargeRange:10
    property color itemColor:"blue"
    property int itemIndex:0
    property int duration:1500
    property bool running:visible

    //使用动画控制itemIndex属性循环从0到总个数自增自减
    NumberAnimation{
        target: control
        property: "itemIndex"
        from:0
        to:control.itemCount - 1
        loops: Animation.Infinite
        duration: control.duration
        running: control.running
    }
    //设计旋转小圈圈
    Item{
        id:content
        anchors.fill: parent
        anchors.margins: control.itemLargeRange/2+1
        Repeater{
            id:repeater
            model:control.itemCount
            Rectangle{
                id:item
                height: control.itemSize
                width: height
                //起始位置设置在中心
                x:content.width/2-width/2
                y:content.height/2-height/2
                radius: height/2
                color: 'white'
                border.color: control.itemColor
                border.width: 1
                //设定旋转
                transform: [
                    //对每个球进行移动(移动距离为半径)
                    Translate{
                        y:content.height/2-height/2
                    },
                    Rotation{
                        //每个球进行角度旋转
                        angle: index / repeater.count * 360
                        origin.x:width/2
                        origin.y:height/2
                    }
                ]

                //利用状态转换,当index与itemIndex重合时,更改小球的状态
                state: control.itemIndex === index ? "second":"first"

                states: [
                    State {
                        name: "second"
                        PropertyChanges {
                            target: item
                            color:control.itemColor
                        }
                    },
                    State{
                        name:"first"
                        PropertyChanges {
                            target: item
                            color:'white'
                        }
                    }
                ]
                //为状态过渡添加动画
                transitions: [
                    Transition {
                        from: "second"
                        to: "first"
                        ColorAnimation{
                            properties: "color"
                            duration: control.duration
                        }
                    },
                    Transition {
                        from: "first"
                        to: "second"
                        ColorAnimation{
                            properties: "color"
                            duration: 0
                        }
                    }
                ]
            }
        }
    }
}

WaitTipTwo.qml:

import QtQuick 2.15

Item{
    id:control

    implicitHeight: 60
    implicitWidth: 60

    property int itemCount:10
    property int itemSize:8
    property int itemLargeRange:10
    property color itemColor:"orange"
    property int itemIndex:0
    property int duration:1500
    property bool running:visible
    property bool reStart:false

    //使用动画控制itemIndex属性循环从0到总个数自增自减
    NumberAnimation{
        target: control
        property: "itemIndex"
        from:0
        to:control.itemCount
        loops: Animation.Infinite
        duration: control.duration
        running: control.running
    }
    //判断itemIndex是否循环了一圈,循环一圈后要更改状态切换的方式
    onItemIndexChanged: {
        if(control.itemIndex === control.itemCount){
            control.reStart = !control.reStart
        }
    }

    //设计旋转小圈圈
    Item{
        id:content
        anchors.fill: parent
        anchors.margins: control.itemLargeRange/2+1
        Repeater{
            id:repeater
            model:control.itemCount
            Rectangle{
                id:item
                height: control.itemSize
                width: height
                //起始位置设置在中心
                x:content.width/2-width/2
                y:content.height/2-height/2
                radius: height/2
                color: 'white'
                border.color: control.itemColor
                border.width: 1
                //设定旋转
                transform: [
                    //对每个球进行移动(移动距离为半径)
                    Translate{
                        y:-(content.height/2-height/2)
                    },
                    Rotation{
                        //每个球进行角度旋转
                        angle: index / repeater.count * 360
                        origin.x:width/2
                        origin.y:height/2
                    }
                ]
                //使用定时器更改每个小球球的状态,注意触发时间是记录小球索引属性itemIndex变化的平均值
                Timer{
                    id:chTimer
                    repeat: true
                    interval: control.duration/control.itemCount
                    running: true
                    triggeredOnStart: true//启动时立即触发定时器
                    onTriggered: {
                        if(control.itemIndex===index && !control.reStart){
                            item.color = control.itemColor
                        }else if(control.itemIndex===index && control.reStart){
                            item.color = "white"
                        }
                    }
                }
            }
        }
    }
}

持续更新中,请大家多多关注…

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

山间点烟雨

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

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

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

打赏作者

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

抵扣说明:

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

余额充值