QML —— ProgressBar示例(附完整源码)

本文详细介绍了如何在QtQuick中使用QML创建一个带有两种类型的进度条、动态旋转图像和按钮的窗口应用,展示了如何通过动画和定时器实现效果。
摘要由CSDN通过智能技术生成
示例 - 效果

在这里插入图片描述

实例 - 源码
import QtQuick 2.12
import QtQuick.Window 2.12

import QtQuick.Layouts 1.12
import QtQuick.Controls 2.5

Window
{
    id: rootId
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    Column
    {
        spacing: 40
        anchors.centerIn: parent

        Row
        {
            spacing: 20
            Text
            {
                text: qsTr("ProgressBar类型1:")
                font.family: "微软雅黑"
                font.pixelSize: 18
            }
            ProgressBar
            {
                id: pb1
                value: 0.0
                anchors.verticalCenter: parent.verticalCenter
            }
            Text
            {
                text: qsTr("小球当前角度:")
                anchors.verticalCenter: parent.verticalCenter
            }
            Text
            {
                id: pb1Value
                width: 80
                text: qsTr("--")
                anchors.verticalCenter: parent.verticalCenter
            }
        }

        Row
        {
            spacing: 20
            Text
            {
                text: qsTr("ProgressBar类型2:")
                font.family: "微软雅黑"
                font.pixelSize: 18
            }
            ProgressBar
            {
                id: pb2
                indeterminate: false
                anchors.verticalCenter: parent.verticalCenter
            }
            Text
            {
                text: qsTr("小球当前角度:")
                anchors.verticalCenter: parent.verticalCenter
            }
            Text
            {
                id: pb2Value
                width: 80
                text: qsTr("--")
                anchors.verticalCenter: parent.verticalCenter
            }
        }


        Timer
        {
            id: _timer
            interval: 100
            repeat: true
            triggeredOnStart: false
            running: false

            onTriggered:
            {
                let rotationValue = imgId.rotation / 360
                pb1.value = rotationValue
                if(rotationValue === 1.0)
                {
                    running = false
                    pb2.indeterminate = false
                    pb2.value = rotationValue
                }
                else
                {
                    pb2.indeterminate = true
                }

                pb1Value.text = pb2Value.text = Math.floor(imgId.rotation)
            }
        }

        Image
        {
            id: imgId
            height: 140
            width: 140
            source: "qrc:/ball.jpeg"
            anchors.horizontalCenter: parent.horizontalCenter

            NumberAnimation on rotation
            {
                id: imgAnimationId
                from: 0
                to:360
                duration: 6000
                running: false
                easing.type: Easing.Linear
            }
        }

        Button
        {
            anchors.horizontalCenter: parent.horizontalCenter
            text: qsTr("开始(6秒转球360°)")

            onClicked:
            {
                let txt = text;
                if(txt.indexOf("开始") !== -1)
                {
                    _timer.start()
                    imgAnimationId.start()
                    text = qsTr("停止")
                }
                else
                {
                    _timer.stop()
                    imgAnimationId.stop()
                    text = qsTr("开始(10秒转球360°)")
                }
            }
        }

    }
}

关注

笔者 - jxd

  • 6
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

信必诺

嗨,支持下哥们呗。

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

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

打赏作者

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

抵扣说明:

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

余额充值