UserSwitch.qml 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import QtQuick 2.12
  2. import QtQuick.Controls 2.12
  3. Switch {
  4. id: control
  5. text: qsTr("Switch")
  6. //background: Rectangle{
  7. //color: "lightyellow"
  8. // implicitWidth: 60
  9. // implicitHeight: 40
  10. // }
  11. indicator: Rectangle {
  12. implicitWidth: 48
  13. implicitHeight: 26
  14. x: control.leftPadding
  15. y: parent.height / 2 - height / 2
  16. radius: 13
  17. color: control.checked ? "green" : "#ffffff"
  18. border.color: control.checked ? "green" : "#cccccc"
  19. //小圆点
  20. Rectangle {
  21. id : smallRect
  22. width: 26
  23. height: 26
  24. radius: 13
  25. color: control.down ? "#cccccc" : "#ffffff"
  26. border.color: control.checked ? (control.down ? "#17a81a" : "#21be2b") : "#999999"
  27. //改变小圆点的位置
  28. NumberAnimation on x{
  29. to: smallRect.width
  30. running: control.checked ? true : false
  31. duration: 200
  32. }
  33. //改变小圆点的位置
  34. NumberAnimation on x{
  35. to: 0
  36. running: control.checked ? false : true
  37. duration: 200
  38. }
  39. }
  40. }
  41. //要显示的文本
  42. contentItem: Text {
  43. //text: control.checked.toString()
  44. text: control.text
  45. font.pixelSize: 13
  46. font.bold: true
  47. //鼠标按下时 control.down
  48. color: control.checked ? "green" : "gray"
  49. verticalAlignment: Text.AlignVCenter
  50. anchors.left: indicator.right
  51. }
  52. }