Button.qml 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 bool menu: false
  8. property string title
  9. property alias horizontalAlignment: label.horizontalAlignment
  10. signal clicked()
  11. implicitHeight: block
  12. implicitWidth: block * 3
  13. color: menu ? (mouseArea.containsMouse ? "#88ffffff" : "transparent")
  14. : (checked ? "gray" : "lightgray")
  15. border.width: menu ? 0 : 1
  16. border.color: "black"
  17. radius: space / 4
  18. opacity: enabled ? (mouseArea.pressed ? 0.6 : 0.9) : 0.4
  19. function toggle() {
  20. checked = !checked
  21. }
  22. Text {
  23. id: label
  24. text: (checkBox ? (checked ? "☑ " : "☐ ") : "") + title
  25. horizontalAlignment: Text.AlignHCenter
  26. verticalAlignment: Text.AlignVCenter
  27. anchors.fill: parent
  28. anchors.margins: space
  29. }
  30. MouseArea {
  31. id: mouseArea
  32. anchors.fill: parent
  33. hoverEnabled: menu
  34. onClicked: {
  35. if (checkable) {
  36. toggle()
  37. }
  38. parent.clicked()
  39. }
  40. }
  41. }