1. 程式人生 > >運用QML製作圓形進度條效果

運用QML製作圓形進度條效果

在QML中原聲的進度條為水平或垂直的直線型進度條,可以用ProgressBar配合style: ProgressBarStyle{}進行實現,程式碼如下:

import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Controls.Styles 1.2
import QtGraphicalEffects 1.0
import QtQml 2.2

Rectangle {
    id:root;
    color: "white";
    visible: true;
    width: 600;
    height: 600;

    Timer{
        interval: 300;
        running: true;
        repeat: true;
        onTriggered: {
            if(progressBar3.value >= 1)
                progressBar3.value = 0;
            progressBar3.value += 0.05;

            if(progressBar4.value >= 1)
                progressBar4.value = 0;
            progressBar4.value += 0.05;
        }

    }

    ProgressBar {
        id: progressBar3
        x: 83
        y: 226
        width: 397
        height: 23
        value: 0.01;
        style: ProgressBarStyle{
            id:progressBar3Style;
            background: Rectangle{
                border.width: 1;
                border.color: control.hovered?"green":"#25b1e8";
                color:"lightgray";
                Text {
                    anchors.right: parent.right;
                    anchors.rightMargin: 5;
                    anchors.verticalCenter: parent.verticalCenter;
                    text: Math.round(currentProgress*100) + "%";
                    color: "#25b1e8"
                }
            }

            progress: Rectangle{
                color: "#25b1e8";//
                clip: true;
                Rectangle{
                    anchors.left: parent.left;
                    //anchors.top: parent.top;
                    //anchors.bottom: parent.bottom;
                    height: progressBar3.width;
                    width: progressBar3.width;
                    LinearGradient{
                        anchors.fill: parent;
                        gradient: Gradient {
                            GradientStop {
                                position: 0.00;
                                color: "#ffffff";
                            }
                            GradientStop {
                                position: 1.00;
                                color: "#36d1e8";
                            }
                        }
                        start:Qt.point(0, 0);
                        end: Qt.point(parent.width, 0);
                    }
                }
            }

            panel: Item{
                implicitHeight: 20;
                implicitWidth: 200;

                Loader{
                    anchors.fill: parent;
                    sourceComponent: background;
                }

                Loader{
                    anchors.top: parent.top;
                    anchors.left: parent.left;
                    anchors.bottom: parent.bottom;
                    anchors.margins: 2;
                    width: currentProgress * (parent.width - 4)
                    sourceComponent: progressBar3Style.progress;
                }
            }
        }

    }

    ProgressBar {
        id: progressBar4
        x: 83
        y: 289
        width: 397
        height: 23
        value: 0.01;

        property color colorValue: Qt.rgba(37/255, 177/255, 232/255, 1);

        style: ProgressBarStyle{
            id:progressBar4Style;
            background: Rectangle{
                border.width: 1;
                border.color: control.hovered?"green":"#25b1e8";
                color:"lightgray";
            }

            progress: Rectangle{
                //color: "#25b1e8";//
                //color: Math.round(currentProgress*100);
                color: progressBar4.colorValue;
                onColorChanged: {
                    console.log("onColorChanged")
                }
            }

            panel: Item{
                implicitHeight: 20;
                implicitWidth: 200;

                Loader{
                    anchors.fill: parent;
                    sourceComponent: background;
                }

                Loader{
                    anchors.top: parent.top;
                    anchors.left: parent.left;
                    anchors.bottom: parent.bottom;
                    anchors.margins: 2;
                    width: currentProgress * (parent.width - 4)
                    sourceComponent: progressBar4Style.progress;

                    onWidthChanged: {
                        console.log("onWidthChanged")
                        progressBar4.colorValue = Qt.rgba(1-currentProgress, 1-currentProgress, 1-currentProgress, 1)
                    }
                }

                Text {
                    anchors.right: parent.right;
                    anchors.rightMargin: 5;
                    anchors.verticalCenter: parent.verticalCenter;
                    text: Math.round(currentProgress*100) + "%";
                    color: "#25b1e8"
                }
            }
        }
    }

}
效果如下:

圓形進度條的實現程式碼如下:

import QtQuick 2.5
import QtQuick.Layouts 1.2
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4
import Material 0.1 as Material
import QtQuick.Controls.Styles.Material 0.1 as MaterialStyle

// draws two arcs (portion of a circle)
// fills the circle with a lighter secondary color
// when pressed

Canvas {
    id: canvas
    width: 60
    height: 60
    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 radius: Math.min(canvas.width-10, canvas.height-10) / 2

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

    // 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

    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 = 10;
            ctx.fillStyle = Qt.lighter(canvas.secondaryColor, 1.25);
            ctx.arc(canvas.centerWidth,
                    canvas.centerHeight,
                    canvas.radius,
                    0,
                    2*Math.PI);
            ctx.fill();

            timer.running = true;
        }

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

        ctx.beginPath();
        ctx.lineWidth = 10;
        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 = 10;
        ctx.strokeStyle = canvas.secondaryColor;
        ctx.arc(canvas.centerWidth,
                canvas.centerHeight,
                canvas.radius,
                canvas.angleOffset,
                canvas.angleOffset + canvas.angle);
        ctx.stroke();

        ctx.restore();
    }

    Text {
        id: txt_progress
        anchors.centerIn: parent

        font.pixelSize: 16
        text: canvas.text
        color: canvas.primaryColor
    }

    MouseArea {
        id: mouseArea

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


    Timer{
        id: timer
        interval: 300;
        running: false;
        repeat: true;
        onTriggered: {
            if(currentValue === 100) {
                currentValue = 0;
                txt_progress.text = "0"
            }
            currentValue += 1;
            txt_progress.text = currentValue.toString()+"%";
        }

    }
}

實現效果如下:


更改以上程式碼中的弧線寬度和半徑長度,可以實現扇形進度(弧線變粗到半徑,半徑變短到0),實現效果如下:


相關推薦

運用QML製作圓形進度效果

在QML中原聲的進度條為水平或垂直的直線型進度條,可以用ProgressBar配合style: ProgressBarStyle{}進行實現,程式碼如下: import QtQuick 2.4 import QtQuick.Controls 1.3 import QtQui

使用自定義View繪製圓形進度效果

首先自定義屬性 res - values - attrs(自己建立): <?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="MyCicle">

移動端純CSS3製作圓形進度所遇到的問題

本文轉載於:猿2048網站移動端純CSS3製作圓形進度條所遇到的問題     近日在開發的頁面中,需要製作一個動態的

自定義view,可拖拽進度和吸附效果圓形進度

前言 最近接到一個需求,第一眼看到ui互動效果時,瞬間想對產品小哥說“尼瑪,這麼會玩,你咋不上天”。確認了具體互動細節,喝了兩口農夫三拳,開始了兩耳不聞窗外事,一心只想擼程式碼的過程。 先上ui效果 說明: 外圈弧形上面是進度的標記點,預設在12點位置,也是

android自定義圓形進度,實現動態畫圓效果

自定義圓形進度條效果圖如下:應用場景如動態顯示分數等。 view的自定義屬性如下attr.xml <?xml version="1.0" encoding="UTF-8"?> <resources> <declare-style

Qml圓形進度

使用Qml的Canvas來畫圓形 onPaint: { var ctx = getContext("2d") ctx.clearRect(0,0,width,hei

帶動畫效果圓形進度顯示定時器倒計時

在實際專案開發中我們經常要使用進度條監聽下載進度,使用的大多是在UIView中的- (void)drawRect:(CGRect)rect方法中描繪圓形路徑,然後通過傳過來的進度值計算圓形路徑百分比達到監聽效果,這種監聽進度方法適合檔案下載,但是我們開發中有時會

HTML5效果:Canvas 實現圓形進度並顯示數字百分比

實現效果 1.首先建立html程式碼 <canvas id="canvas" width="500" he

canvas 繪制圓形進度

設置字體大小 文字 height center body 字體 指定位置 str n) <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8">

純CSS3制作圓形進度所遇到的問題

order 我們 消失 樣式 近日 內部 右上角 圓形進度 spa   近日在開發的頁面中,需要制作一個動態的圓形進度條,首先想到的是利用兩個矩形,寬等於直徑的一半,高等於直徑,兩個矩形利用浮動貼在一起,設置overflow:hidden屬性,作為盒子,內部有一個與其寬高相

Android 從無到有打造一個炫酷的進度效果

fault ref size art href 一個 font itl top 從無到有打造一個炫酷的進度條效果Android 從無到有打造一個炫酷的進度條效果

圓形進度

最小 element ali logs isp ber per 圖片 center 基於angularJS1/jquery和radialIndicator插件 效果圖: 1、首先引入一下文件: 2、在jsp中引入需要的文件 <script src="js

異步下載圓形進度顯示進度

https ati ack blog nbsp idt 進度 circle osi 圓形進度條參考鏈接即可:使用css3實現圓形進度條 需求點擊下載後遮罩層顯示下載進度: 1.圓形進度條參考以上鏈接,有點小瑕疵,可更改定位距離實現重合。 2.遮罩層: .lbOverla

CARVARS 圓形進度

sele int 20px 進度 arc null type eight 進度條 <!DOCTYPE html><html><head><meta charset="UTF-8"><title></title

JS實現進度效果

gpo cti parse width border () mage clas get 源代碼: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <t

純css進度效果

frame lock 20px har div -html box overflow order <!--html代碼--><!DOCTYPE html> <html lang="zh"> <head> <meta c

53.原型2 仿真進度效果

但是 自增 分享圖片 隨機 矩形 動作 之間 毫秒 10個 添加一個面板 寬為300 高為5 同時添加背景色 首先給面板設置載入時的動作 作為第一次改變面板狀態 然後給面板設置改變大小時的動作 首先等待200毫秒 然後自增10個像素 用this.width 生成

突發奇想,JavaScript模仿下載進度效果

fun 調用函數 auto pro meta har onload 更新 box 不定時的更新,這一次采用JavaScript 模仿下載進度條效果。原理:兩個div嵌套,裏面的寬度0,外部自己隨便定義,采用 setInterval() 函數增加 裏面的div 的 width

模仿進度效果

div wid 瀏覽器 script cti ima lac onload osc 進度條效果 <!DOCTYPE html> <html lang="en"> <head> <meta chars

記錄小文件上傳的幾個例子(含進度效果,附源碼下載)

clas 運行 open ntb httppost 會有 text recent mappath 1、簡單原生上傳 無javascript腳本、無進度條; 借助iframe實現post提交後的無刷新效果; jquery插件ajaxFileUpload.js的實現原