javascript - 如何在纯 QML+JS 中创建圆形进度条?

标签 javascript qt qml ubuntu-touch

我的应用程序是使用 QML+JS 制作的,我希望创建一个圆形进度条小部件。我可以使用 QML Rectangle 创建圆并将其半径设置为等于其宽度/2 以使其成为一个圆。如何从中创建进度条?

我计划实现以下模型。

enter image description here

最佳答案

我已经使用 Canvas 实现了一个基本的循环进度。

enter image description here

import QtQml 2.2
import QtQuick 2.0

// draws two arcs (portion of a circle)
// fills the circle with a lighter secondary color
// when pressed
Canvas {
    id: canvas
    width: 240
    height: 240
    antialiasing: true

    property color primaryColor: "orange"
    property color secondaryColor: "lightblue"

    property real centerWidth: width / 2
    property real centerHeight: height / 2
    property real radius: Math.min(canvas.width, canvas.height) / 2

    property real minimumValue: 0
    property real maximumValue: 100
    property real currentValue: 33

    // this is the angle that splits the circle in two arcs
    // first arc is drawn from 0 radians to angle radians
    // second arc is angle radians to 2*PI radians
    property real angle: (currentValue - minimumValue) / (maximumValue - minimumValue) * 2 * Math.PI

    // we want both circle to start / end at 12 o'clock
    // without this offset we would start / end at 9 o'clock
    property real angleOffset: -Math.PI / 2

    property string text: "Text"

    signal clicked()

    onPrimaryColorChanged: requestPaint()
    onSecondaryColorChanged: requestPaint()
    onMinimumValueChanged: requestPaint()
    onMaximumValueChanged: requestPaint()
    onCurrentValueChanged: requestPaint()

    onPaint: {
        var ctx = getContext("2d");
        ctx.save();

        ctx.clearRect(0, 0, canvas.width, canvas.height);

        // fills the mouse area when pressed
        // the fill color is a lighter version of the
        // secondary color

        if (mouseArea.pressed) {
            ctx.beginPath();
            ctx.lineWidth = 1;
            ctx.fillStyle = Qt.lighter(canvas.secondaryColor, 1.25);
            ctx.arc(canvas.centerWidth,
                    canvas.centerHeight,
                    canvas.radius,
                    0,
                    2*Math.PI);
            ctx.fill();
        }

        // First, thinner arc
        // From angle to 2*PI

        ctx.beginPath();
        ctx.lineWidth = 1;
        ctx.strokeStyle = primaryColor;
        ctx.arc(canvas.centerWidth,
                canvas.centerHeight,
                canvas.radius,
                angleOffset + canvas.angle,
                angleOffset + 2*Math.PI);
        ctx.stroke();


        // Second, thicker arc
        // From 0 to angle

        ctx.beginPath();
        ctx.lineWidth = 3;
        ctx.strokeStyle = canvas.secondaryColor;
        ctx.arc(canvas.centerWidth,
                canvas.centerHeight,
                canvas.radius,
                canvas.angleOffset,
                canvas.angleOffset + canvas.angle);
        ctx.stroke();

        ctx.restore();
    }

    Text {
        anchors.centerIn: parent

        text: canvas.text
        color: canvas.primaryColor
    }

    MouseArea {
        id: mouseArea

        anchors.fill: parent
        onClicked: canvas.clicked()
        onPressedChanged: canvas.requestPaint()
    }
}

关于javascript - 如何在纯 QML+JS 中创建圆形进度条?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22873550/

相关文章:

c++ - 写入内核配置时为 "Not a directory"

qt - 按住Qml MouseArea移动窗口

c++ - 如何从 C++ 访问特定 QML 控件的事件

javascript - Illustrator - 在文本路径脚本中批量插入文件名会使 Illustrator 崩溃

c++ - 将 Boost Serialize 与 Qt 插件一起使用

c++ - QT设置窗口背景

c++ - 将 Q_GADGET 作为信号参数从 C++ 传递到 QML

HTML 的 Javascript 函数只发生一次

javascript - TypeError : By. cssSelector 不是函数

javascript - 将行添加到现有表中