当前位置:   article > 正文

QT进度条控件封装(QML)

qt进度条控件

 演示效果,可指定大小,前景色,背景色

 1.背景色

  1. //进度背景色
  2. background: Rectangle {
  3. implicitWidth: w
  4. implicitHeight: h
  5. color: cProgress.proBackgroundColor
  6. radius: cProgress.proRadius
  7. }

2.前景色

  1. //当前进度色
  2. contentItem: Item {
  3. implicitWidth: w
  4. implicitHeight: h
  5. Rectangle {
  6. width: cProgress.visualPosition * w
  7. height: h
  8. radius: cProgress.proRadius
  9. color: cProgress.proColor
  10. }
  11. }

3.更新进度的计时器

  1. Timer{
  2. id: timer
  3. running: run //默认不启动
  4. repeat: true //重复使用
  5. interval: 50 //每50毫秒响应一次
  6. onTriggered:{
  7. cProgress.progress++;//响应进度
  8. if (cProgress.progress > 100){
  9. cProgress.onStop();
  10. return;
  11. }
  12. }
  13. }

4.调用

  1. QmlProgress {
  2. w:parent.width
  3. h:10
  4. progress: 68
  5. proBackgroundColor: "yellow"
  6. proColor: "red"
  7. }

5.完整进度条控件代码与主界面qml代码

进度条控件

  1. import QtQuick 2.7
  2. import QtQuick.Controls 2.2
  3. ProgressBar {
  4. property color proColor: "#148014"
  5. property color proBackgroundColor: "#AAAAAA"
  6. property int proWidth: 2
  7. property real progress: 0
  8. property real proRadius: 50
  9. property alias interval: timer.interval
  10. property int w: 500
  11. property int h: 50
  12. property bool run: true
  13. //取计时器状态
  14. function isRunning(){
  15. return(timer.running)
  16. }
  17. //启动计时器来更新进度值
  18. function onStart(){
  19. cProgress.progress = 0;
  20. timer.running = true;//通知计时器
  21. }
  22. //进度已满,停止计时器
  23. function onStop(){
  24. timer.running = false;
  25. //停止后重置进度,重新开始跑进度
  26. cProgress.progress=0;
  27. cProgress.onStart()
  28. }
  29. id: cProgress
  30. anchors.centerIn: parent
  31. value: (progress/100) //进度条默认值
  32. padding: 2
  33. width: w;
  34. height:h;
  35. //进度背景色
  36. background: Rectangle {
  37. implicitWidth: w
  38. implicitHeight: h
  39. color: cProgress.proBackgroundColor
  40. radius: cProgress.proRadius
  41. }
  42. //当前进度色
  43. contentItem: Item {
  44. implicitWidth: w
  45. implicitHeight: h
  46. Rectangle {
  47. width: cProgress.visualPosition * w
  48. height: h
  49. radius: cProgress.proRadius
  50. color: cProgress.proColor
  51. }
  52. }
  53. Timer{
  54. id: timer
  55. running: run //默认不启动
  56. repeat: true //重复使用
  57. interval: 50 //每50毫秒响应一次
  58. onTriggered:{
  59. cProgress.progress++;//响应进度
  60. if (cProgress.progress > 100){
  61. cProgress.onStop();
  62. return;
  63. }
  64. }
  65. }
  66. }

主界面

  1. import QtQuick 2.7
  2. import QtQuick.Controls 2.2
  3. ApplicationWindow {
  4. visible: true
  5. width: 800
  6. height: 600
  7. title: qsTr("基于QML实现的进度条示例")
  8. Column{
  9. width:parent.width-50
  10. anchors.centerIn: parent
  11. Rectangle{
  12. width: parent.width - 200
  13. height: parent.height
  14. QmlProgress {
  15. w:parent.width
  16. h:10
  17. progress: 68
  18. proBackgroundColor: "yellow"
  19. proColor: "red"
  20. }
  21. }
  22. Rectangle{
  23. width: parent.width - 100
  24. height: parent.height
  25. y:50
  26. QmlProgress {
  27. w:parent.width
  28. h:20
  29. progress: 68
  30. proBackgroundColor: "red"
  31. proColor: "purple"
  32. }
  33. }
  34. Rectangle{
  35. width: parent.width
  36. height: parent.height
  37. y:120
  38. QmlProgress {
  39. w:parent.width
  40. h:30
  41. progress: 68
  42. proBackgroundColor: "blue"
  43. }
  44. }
  45. }
  46. }

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/article/detail/42515
推荐阅读
相关标签
  

闽ICP备14008679号