Chinaunix首页 | 论坛 | 博客
  • 博客访问: 980732
  • 博文数量: 192
  • 博客积分: 3070
  • 博客等级: 中校
  • 技术积分: 1861
  • 用 户 组: 普通用户
  • 注册时间: 2007-06-27 23:44
个人简介

Start Linux Leave Linux a while Back to Linux

文章分类

全部博文(192)

文章存档

2023年(18)

2022年(11)

2021年(8)

2020年(14)

2019年(7)

2018年(13)

2017年(16)

2016年(4)

2012年(2)

2011年(13)

2010年(26)

2009年(13)

2008年(27)

2007年(20)

我的朋友

分类: 其他平台

2017-03-06 10:21:25

原文:http://blog.csdn.net/shado_walker/article/details/51684865

在QML中原声的进度条为水平或垂直的直线型进度条,可以用ProgressBar配合style: ProgressBarStyle{}进行实现,代码如下:


点击(此处)折叠或打开

  1. import QtQuick 2.4
  2. import QtQuick.Controls 1.3
  3. import QtQuick.Controls.Styles 1.2
  4. import QtGraphicalEffects 1.0
  5. import QtQml 2.2

  6. Rectangle {
  7.     id:root;
  8.     color: "white";
  9.     visible: true;
  10.     width: 600;
  11.     height: 600;

  12.     Timer{
  13.         interval: 300;
  14.         running: true;
  15.         repeat: true;
  16.         onTriggered: {
  17.             if(progressBar3.value >= 1)
  18.                 progressBar3.value = 0;
  19.             progressBar3.value += 0.05;

  20.             if(progressBar4.value >= 1)
  21.                 progressBar4.value = 0;
  22.             progressBar4.value += 0.05;
  23.         }

  24.     }

  25.     ProgressBar {
  26.         id: progressBar3
  27.         x: 83
  28.         y: 226
  29.         width: 397
  30.         height: 23
  31.         value: 0.01;
  32.         style: ProgressBarStyle{
  33.             id:progressBar3Style;
  34.             background: Rectangle{
  35.                 border.width: 1;
  36.                 border.color: control.hovered?"green":"#25b1e8";
  37.                 color:"lightgray";
  38.                 Text {
  39.                     anchors.right: parent.right;
  40.                     anchors.rightMargin: 5;
  41.                     anchors.verticalCenter: parent.verticalCenter;
  42.                     text: Math.round(currentProgress*100) + "%";
  43.                     color: "#25b1e8"
  44.                 }
  45.             }

  46.             progress: Rectangle{
  47.                 color: "#25b1e8";//
  48.                 clip: true;
  49.                 Rectangle{
  50.                     anchors.left: parent.left;
  51.                     //anchors.top: parent.top;
  52.                     //anchors.bottom: parent.bottom;
  53.                     height: progressBar3.width;
  54.                     width: progressBar3.width;
  55.                     LinearGradient{
  56.                         anchors.fill: parent;
  57.                         gradient: Gradient {
  58.                             GradientStop {
  59.                                 position: 0.00;
  60.                                 color: "#ffffff";
  61.                             }
  62.                             GradientStop {
  63.                                 position: 1.00;
  64.                                 color: "#36d1e8";
  65.                             }
  66.                         }
  67.                         start:Qt.point(0, 0);
  68.                         end: Qt.point(parent.width, 0);
  69.                     }
  70.                 }
  71.             }

  72.             panel: Item{
  73.                 implicitHeight: 20;
  74.                 implicitWidth: 200;

  75.                 Loader{
  76.                     anchors.fill: parent;
  77.                     sourceComponent: background;
  78.                 }

  79.                 Loader{
  80.                     anchors.top: parent.top;
  81.                     anchors.left: parent.left;
  82.                     anchors.bottom: parent.bottom;
  83.                     anchors.margins: 2;
  84.                     width: currentProgress * (parent.width - 4)
  85.                     sourceComponent: progressBar3Style.progress;
  86.                 }
  87.             }
  88.         }

  89.     }

  90.     ProgressBar {
  91.         id: progressBar4
  92.         x: 83
  93.         y: 289
  94.         width: 397
  95.         height: 23
  96.         value: 0.01;

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

  98.         style: ProgressBarStyle{
  99.             id:progressBar4Style;
  100.             background: Rectangle{
  101.                 border.width: 1;
  102.                 border.color: control.hovered?"green":"#25b1e8";
  103.                 color:"lightgray";
  104.             }

  105.             progress: Rectangle{
  106.                 //color: "#25b1e8";//
  107.                 //color: Math.round(currentProgress*100);
  108.                 color: progressBar4.colorValue;
  109.                 onColorChanged: {
  110.                     console.log("onColorChanged")
  111.                 }
  112.             }

  113.             panel: Item{
  114.                 implicitHeight: 20;
  115.                 implicitWidth: 200;

  116.                 Loader{
  117.                     anchors.fill: parent;
  118.                     sourceComponent: background;
  119.                 }

  120.                 Loader{
  121.                     anchors.top: parent.top;
  122.                     anchors.left: parent.left;
  123.                     anchors.bottom: parent.bottom;
  124.                     anchors.margins: 2;
  125.                     width: currentProgress * (parent.width - 4)
  126.                     sourceComponent: progressBar4Style.progress;

  127.                     onWidthChanged: {
  128.                         console.log("onWidthChanged")
  129.                         progressBar4.colorValue = Qt.rgba(1-currentProgress, 1-currentProgress, 1-currentProgress, 1)
  130.                     }
  131.                 }

  132.                 Text {
  133.                     anchors.right: parent.right;
  134.                     anchors.rightMargin: 5;
  135.                     anchors.verticalCenter: parent.verticalCenter;
  136.                     text: Math.round(currentProgress*100) + "%";
  137.                     color: "#25b1e8"
  138.                 }
  139.             }
  140.         }
  141.     }

  142. }
效果如下:


圆形进度条的实现代码如下:


点击(此处)折叠或打开

  1. import QtQuick 2.5
  2. import QtQuick.Layouts 1.2
  3. import QtQuick.Controls 1.4
  4. import QtQuick.Controls.Styles 1.4
  5. import Material 0.1 as Material
  6. import QtQuick.Controls.Styles.Material 0.1 as MaterialStyle

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

  10. Canvas {
  11.     id: canvas
  12.     width: 60
  13.     height: 60
  14.     antialiasing: true

  15.     property color primaryColor: "orange"
  16.     property color secondaryColor: "lightblue"

  17.     property real centerWidth: width / 2
  18.     property real centerHeight: height / 2
  19.     //property real radius: Math.min(canvas.width, canvas.height) / 2
  20.     property real radius: Math.min(canvas.width-10, canvas.height-10) / 2

  21.     property real minimumValue: 0
  22.     property real maximumValue: 100
  23.     property real currentValue: 0

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

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

  31.     signal clicked()

  32.     onPrimaryColorChanged: requestPaint()
  33.     onSecondaryColorChanged: requestPaint()
  34.     onMinimumValueChanged: requestPaint()
  35.     onMaximumValueChanged: requestPaint()
  36.     onCurrentValueChanged: requestPaint()

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

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

  41.         // fills the mouse area when pressed
  42.         // the fill color is a lighter version of the
  43.         // secondary color

  44.         if (mouseArea.pressed) {
  45.             ctx.beginPath();
  46.             ctx.lineWidth = 10;
  47.             ctx.fillStyle = Qt.lighter(canvas.secondaryColor, 1.25);
  48.             ctx.arc(canvas.centerWidth,
  49.                     canvas.centerHeight,
  50.                     canvas.radius,
  51.                     0,
  52.                     2*Math.PI);
  53.             ctx.fill();

  54.             timer.running = true;
  55.         }

  56.         // First, thinner arc
  57.         // From angle to 2*PI

  58.         ctx.beginPath();
  59.         ctx.lineWidth = 10;
  60.         ctx.strokeStyle = primaryColor;
  61.         ctx.arc(canvas.centerWidth,
  62.                 canvas.centerHeight,
  63.                 canvas.radius,
  64.                 angleOffset + canvas.angle,
  65.                 angleOffset + 2*Math.PI);
  66.         ctx.stroke();


  67.         // Second, thicker arc
  68.         // From 0 to angle

  69.         ctx.beginPath();
  70.         ctx.lineWidth = 10;
  71.         ctx.strokeStyle = canvas.secondaryColor;
  72.         ctx.arc(canvas.centerWidth,
  73.                 canvas.centerHeight,
  74.                 canvas.radius,
  75.                 canvas.angleOffset,
  76.                 canvas.angleOffset + canvas.angle);
  77.         ctx.stroke();

  78.         ctx.restore();
  79.     }

  80.     Text {
  81.         id: txt_progress
  82.         anchors.centerIn: parent

  83.         font.pixelSize: 16
  84.         text: canvas.text
  85.         color: canvas.primaryColor
  86.     }

  87.     MouseArea {
  88.         id: mouseArea

  89.         anchors.fill: parent
  90.         onClicked: canvas.clicked()
  91.         onPressedChanged: canvas.requestPaint()
  92.     }


  93.     Timer{
  94.         id: timer
  95.         interval: 300;
  96.         running: false;
  97.         repeat: true;
  98.         onTriggered: {
  99.             if(currentValue === 100) {
  100.                 currentValue = 0;
  101.                 txt_progress.text = "0"
  102.             }
  103.             currentValue += 1;
  104.             txt_progress.text = currentValue.toString()+"%";
  105.         }

  106.     }
  107. }

实现效果如下:


更改以上代码中的弧线宽度和半径长度,可以实现扇形进度(弧线变粗到半径,半径变短到0),实现效果如下:



阅读(4228) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~