• 74 QML 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!

  • 相关阅读:
    Java_IO流03:处理流之一:缓冲流
    Leetcode P620 DFS解法,思路简单易懂
    jlink系列 v9 和 v11 调试器版本区别
    UE4插件-读取png图片
    了解Redis之命令操作
    凉鞋的 Unity 笔记 203. 变量的常用类型
    C++类设计:一个比较复杂的日志类 支持多线程、文件切换、信息缓存(源码)
    mysql数据库备份与恢复
    杰理之列播内置 flash 提示音控制播放暂停处理方法【篇】
    测试需求分析
  • 原文地址:https://blog.csdn.net/Chasing_Chasing/article/details/132788749