当前位置:   article > 正文

74 QML ProgressBar显示进度数字_在progressbar中怎么显示数字

在progressbar中怎么显示数字

1 引言

        由于目前使用的是qt.5.14版本,Qt Quick Controls 已经从1.0版本 变为2.0版本了,如果继续使用的Qt Quick Controls 1 的style:方式,改变进度条的样式已经不行了,其会报错:Invalid property name "style"。

  1. //原 Qt Quick Controls 1进度条样式修改
  2. //但目前Qt Quick Controls 2已经不支持这样方式了
  3. ProgressBar {
  4. value: slider.value
  5. style: ProgressBarStyle {
  6. background: Rectangle {
  7. radius: 2
  8. color: "lightgray"
  9. border.color: "gray"
  10. border.width: 1
  11. implicitWidth: 200
  12. implicitHeight: 24
  13. }
  14. progress: Rectangle {
  15. color: "lightsteelblue"
  16. border.color: "steelblue"
  17. }
  18. }
  19. }

        由于项目需要,想要在进度条上显示百分比数值,如下图示。

        基础的进度条样式无数值的显示,因此需要对进度条样式进行修改。 

2 实验代码

        经过学习及实验,终于尝试多次后实现要求的功能,现贴出代码,供参考学习。

  1. ProgressBar {
  2. id: root
  3. height: 20
  4. anchors.top: bleRssi.bottom
  5. anchors.topMargin: 2
  6. anchors.left: csq4G.right
  7. anchors.leftMargin: 2
  8. anchors.right: devQRTips.right
  9. property color color: "#b2d235"
  10. from:0
  11. to:31
  12. value: 13
  13. background: Rectangle {
  14. implicitWidth: root.width
  15. implicitHeight: 12
  16. color: "#EBEDEF"
  17. }
  18. contentItem: Item {
  19. implicitWidth: root.background.implicitWidth
  20. implicitHeight: root.background.implicitHeight
  21. Rectangle {
  22. width: root.visualPosition * parent.width
  23. height: parent.height
  24. color: root.color
  25. radius: 2
  26. }
  27. Text {
  28. x:root.width*root.visualPosition
  29. anchors.verticalCenter:parent.verticalCenter
  30. text: (root.value/root.to*100).toFixed(1)+"%";
  31. color: "#345684"
  32. font.pointSize: 16
  33. }
  34. }
  35. }

以上代码完成qml ProgressBar样式的修改,在ProgressBar进度条显示数字。

特此记录!over!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/42496
推荐阅读
相关标签
  

闽ICP备14008679号