How to create a circular progress bar in pure QML+JS?

后端 未结 6 539
自闭症患者
自闭症患者 2021-02-01 05:59

My application is made using QML+JS and I am looking to create a circular progress bar widget. I can create the circle using a QML Rectangle and settings its radius equal to its

6条回答
  •  耶瑟儿~
    2021-02-01 06:28

    I came across an example by Diego Dotta on GitHub using two rotating circles that seems to work nicely for this use case. It involves setting the duration of a PropertyAnimation. So while this works well for a timer that you can set, it would need a different approach for something you didn't know how long it would take. This is tweaked a bit and ported to QtQuick 2.0:

    main.qml:

    import QtQuick 2.0
    import Ubuntu.Components 0.1
    
    Rectangle {
        width: units.gu(50)
        height: units.gu(50)
    
        property int seconds : 0
    
        LoadCircle {
            id: circle
            anchors.centerIn: parent
            loadtimer: 10*1000 // 10 seconds
            Component.onCompleted: start();
            onFinishedChanged: {
                timer.stop();
                borderColor = "green"
            }
        }
    
        Rectangle {
            id : theTimer
            anchors.centerIn: parent
            width : units.gu(10) ; height: units.gu(10)
    
            Label { 
                text: seconds
                font.bold: true
                fontSize: "x-large"
                anchors.centerIn: parent
            }
        }
    
        Timer {
            id: timer
            interval: 1000; running: true; repeat: true;
            onTriggered: seconds++;
        }
    
    }
    

    LoadCircle.qml:

    import QtQuick 2.0
    import Ubuntu.Components 0.1
    
    Row{
        id: circle
    
        property int loadtimer: 4000
        property color circleColor: "transparent"
        property color borderColor: "red"
        property int borderWidth: 10
        property alias running: initCircle.running
        property bool finished: false;
    
        width: units.gu(30)
        height: width
    
        function start(){
            part1.rotation = 180
            part2.rotation = 180
            initCircle.start()
        }
    
        function stop(){
            initCircle.stop()
        }
    
        Item{
            width: parent.width/2
            height: parent.height
            clip: true
    
            Item{
                id: part1
                width: parent.width
                height: parent.height
                clip: true
                rotation: 180
                transformOrigin: Item.Right
    
                Rectangle{
                    width: circle.width-(borderWidth*2)
                    height: circle.height-(borderWidth*2)
                    radius: width/2
                    x:borderWidth
                    y:borderWidth
                    color: circleColor
                    border.color: borderColor
                    border.width: borderWidth
                    smooth: true
                }
            }
        }
    
        Item{
            width: parent.width/2
            height: parent.height
            clip: true
    
            Item{
                id: part2
                width: parent.width
                height: parent.height
                clip: true
    
                rotation: 180
                transformOrigin: Item.Left
    
                Rectangle{
                    width: circle.width-(borderWidth*2)
                    height: circle.height-(borderWidth*2)
                    radius: width/2
                    x: -width/2
                    y: borderWidth
                    color: circleColor
                    border.color: borderColor
                    border.width: borderWidth
                    smooth: true
                }
            }
        }
        SequentialAnimation{
            id: initCircle
            PropertyAnimation{ target: part2; property: "rotation"; to:360; duration:loadtimer/2 }
            PropertyAnimation{ target: part1; property: "rotation"; to:360; duration:loadtimer/2 }
            ScriptAction { script: finished = true; }
        }
    }
    

    example image

提交回复
热议问题