Button.qml 1011 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import QtQuick 2.0
  2. Rectangle {
  3. id: button
  4. property bool checkable: false
  5. property bool checked: false
  6. property bool checkBox: false
  7. property string title
  8. property alias horizontalAlignment: label.horizontalAlignment
  9. signal clicked()
  10. implicitHeight: block
  11. implicitWidth: block * 3
  12. color: checked ? "gray" : "lightgray"
  13. border.width: 1
  14. border.color: "black"
  15. radius: space / 4
  16. opacity: enabled ? (mouseArea.pressed ? 0.6 : 0.9) : 0.4
  17. function toggle() {
  18. checked = !checked
  19. }
  20. Text {
  21. id: label
  22. text: (checkBox ? (checked ? "☑ " : "☐ ") : "") + title
  23. horizontalAlignment: Text.AlignHCenter
  24. verticalAlignment: Text.AlignVCenter
  25. anchors.fill: parent
  26. anchors.margins: space
  27. }
  28. MouseArea {
  29. id: mouseArea
  30. anchors.fill: parent
  31. onClicked: {
  32. if (checkable) {
  33. toggle()
  34. }
  35. parent.clicked()
  36. }
  37. }
  38. }