当前位置:   article > 正文

QML 自定义进度条组件开发

QML 自定义进度条组件开发

一、效果预览

二、介绍:

自定义的QML 屏幕亮度拖动进度条组件CusProgressBar 可跟鼠标移动 更改进度条样式

三、代码

  1. import QtQuick 2.12
  2. import QtQuick.Controls 2.12
  3. import QtQuick.Controls.Material 2.12
  4. /**
  5. *@author:Zwj
  6. *csdn:来份煎蛋吧
  7. *date:2023/12/16
  8. */
  9. Rectangle {
  10. property int widthValue: 100
  11. width: widthValue
  12. height: 20
  13. radius: height / 2
  14. color: "lightgray"
  15. Rectangle {
  16. id: progressRect
  17. height: parent.height
  18. anchors.left: parent.left
  19. width: progressWidth
  20. radius: height / 2
  21. // 使用渐变颜色
  22. gradient: Gradient {
  23. id: progressGradient
  24. GradientStop { position: 0.0; color: "#8DE7F1" }
  25. GradientStop { position: 1.0; color: "#3FA6D9" }
  26. }
  27. Behavior on x {
  28. NumberAnimation { duration: 100 }
  29. }
  30. }
  31. Material.theme: Material.Dark
  32. MouseArea {
  33. id: mouseArea
  34. anchors.fill: parent
  35. onPressed: {
  36. if (mouse.button === Qt.LeftButton) {
  37. mouse.accepted = true
  38. }
  39. }
  40. onPositionChanged: {
  41. if (mouseArea.containsMouse && mouse.buttons === Qt.LeftButton) {
  42. var localMouse = mapToItem(progressRect, mouse.x, 0)
  43. if (localMouse.x >= 0 && localMouse.x <= parent.width) {
  44. progressRect.width = localMouse.x
  45. updateGradientStops(progressRect.width / parent.width)
  46. }
  47. }
  48. }
  49. }
  50. function updateGradientStops(progress) {
  51. progressGradient.stops[1].position = progress
  52. }
  53. property alias progressWidth: progressRect.width
  54. }

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

闽ICP备14008679号