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

后端 未结 6 578
自闭症患者
自闭症患者 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:23

    I found a kinda elegant solution in plain QML which can be also used for styling a regular QtQuick ProgressBar component. The idea behind this is to use a ConicalGradient on a border-only Rectangle.

    Here is the code:

    import QtQuick 2.3
    import QtQuick.Controls.Styles 1.2
    import QtGraphicalEffects 1.0
    
    ProgressBarStyle
    {
       panel : Rectangle
       {
          color: "transparent"
          implicitWidth: 80
          implicitHeight: implicitWidth
    
          Rectangle
          {
             id: outerRing
             z: 0
             anchors.fill: parent
             radius: Math.max(width, height) / 2
             color: "transparent"
             border.color: "gray"
             order.width: 8
          }
    
          Rectangle
          {
             id: innerRing
             z: 1
             anchors.fill: parent
             anchors.margins: (outerRing.border.width - border.width) / 2
             radius: outerRing.radius
             color: "transparent"
             border.color: "darkgray"
             border.width: 4
    
             ConicalGradient
             {
                source: innerRing
                anchors.fill: parent
                gradient: Gradient
                {
                   GradientStop { position: 0.00; color: "white" }
                   GradientStop { position: control.value; color: "white" }
                   GradientStop { position: control.value + 0.01; color: "transparent" }
                   GradientStop { position: 1.00; color: "transparent" }
                }
             }
          }
    
          Text
          {
             id: progressLabel
             anchors.centerIn: parent
             color: "black"
             text: (control.value * 100).toFixed() + "%"
          }
       }
    }
    

    enter image description here

提交回复
热议问题