Button 按钮

属性

  1. action : Action
  2. autoExclusive : bool
  3. autoRepeat : bool
  4. autoRepeatDelay : int
  5. autoRepeatInterval : int
  6. checkable : bool
  7. checked : bool
  8. display : enumeration
  9. down : bool //按键是否按下,onDownChanged 当按键按下与弹开变化时可触发onDownChanged, 按下同时会触发pressed,松开会触发released
  10. icon
  11. //This property specifies whether the icon should be cached.
  12. 此属性指定是否应缓存图标。
  13. icon.cache : bool
  14. icon.color : color
  15. icon.height : int
  16. icon.name : string
  17. icon.source : url
  18. icon.width : int
  19. implicitIndicatorHeight : real
  20. implicitIndicatorWidth : real
  21. indicator : Item
  22. pressX : real
  23. pressY : real
  24. pressed : bool
  25. text : string

信号

  1. canceled()
  2. clicked()
  3. doubleClicked()
  4. pressAndHold()
  5. pressed()
  6. released()
  7. toggled()

实例-按钮背景、图标、长按

  1. Button{
  2. id: btn
  3. width: 50
  4. height: 40
  5. // checkable: true //按键是否可勾选(状态),按键会有按下与释放的不同状态展示
  6. checkable: true
  7. autoExclusive: true //自动排它,同时只能一个按键按下,一个时刻只能check一个控件
  8. autoRepeat: true //按下后不释放是否会重复触发按下信号
  9. autoRepeatDelay: 1000 //按下时触发到稍后第一次触发时间间隔
  10. autoRepeatInterval: 300 //按下后第一次触发后时间间隔
  11. onAutoRepeatChanged: {
  12. console.log("onAutoRepeatChanged down: ", down,"pressed: ", pressed)
  13. }
  14. onAutoRepeatIntervalChanged: {
  15. console.log("onAutoRepeatIntervalChangeddown: ", down,"pressed: ", pressed)
  16. }
  17. onCheckableChanged: {
  18. console.log("onCheckableChanged: ", checkable)
  19. }
  20. onDownChanged: {
  21. console.log("onDownChanged: ", down)
  22. }
  23. onPressed: {
  24. console.log("onPressed: ", pressed)
  25. }
  26. }
  1. Button{
  2. id: btn
  3. width: 150
  4. height: 140
  5. // text: qsTr("Button")
  6. background: Rectangle {
  7. anchors.fill: btn
  8. opacity: enabled ? 1 : 0.3
  9. color: btn.down ? "blue" : "red"
  10. border.width: 3
  11. border.color: {
  12. if(btn.pressed){
  13. return "green"
  14. }else{
  15. return "blue"
  16. }
  17. }
  18. }
  19. // icon.source: "/2.jpg" 不能正常加载图片
  20. indicator:Image {
  21. id: icon
  22. source: "/2.jpg" //鼠标点击有效区域只是btn定义区域,不是icon区域
  23. width: btn.width
  24. height: btn.height
  25. }
  26. onClicked: console.log ("onclicked")
  27. }

button 自定义

  1. Button{
  2. id: control
  3. width: 200
  4. height: 60
  5. padding: 0
  6. text: "button"
  7. // background:Rectangle{
  8. // }
  9. contentItem: Rectangle{ //对内容进行重绘
  10. //默认背景色白色
  11. // color: "transparent" //透明
  12. color: "red"
  13. opacity: 0.8
  14. Text{
  15. id: txt
  16. text: control.text
  17. font.pointSize: 20
  18. color: "green"
  19. }
  20. Image {
  21. id: img
  22. source: "/2.jpg"
  23. width: 50
  24. height: 50
  25. anchors.right: parent.right
  26. }
  27. }
  28. }

image.png

CheckBox 勾选框

属性

  1. checkState : enumeration
  2. nextCheckState : function //修改点击按钮 按钮勾选的状态顺序,参数为一个回调函数
  3. tristate : bool //三态使能 当开启的时候勾选框才会出现第三种状态
  4. checkState : enumeration
  5. Qt.Unchecked //The checkbox is unchecked.
  6. Qt.PartiallyChecked //The checkbox is partially checked. This state is only used when tristate is enabled.
  7. Qt.Checked //The checkbox is checked.
  1. Column {
  2. ButtonGroup {
  3. id: childGroup
  4. exclusive: false //排他
  5. checkState: parentBox.checkState //按钮组的勾选状况和parentBox的勾选状态绑定
  6. }
  7. CheckBox {
  8. id: parentBox
  9. text: qsTr("Parent")
  10. checkState: childGroup.checkState //与按钮组勾选状态绑定
  11. }
  12. CheckBox {
  13. checked: true
  14. text: qsTr("Child 1")
  15. leftPadding: indicator.width
  16. ButtonGroup.group: childGroup //指定所属组
  17. }
  18. CheckBox {
  19. text: qsTr("Child 2")
  20. leftPadding: indicator.width
  21. ButtonGroup.group: childGroup
  22. }
  23. }

image.png

  1. CheckBox {
  2. tristate: true
  3. // checkState: allChildrenChecked ? Qt.Checked :
  4. // anyChildChecked ? Qt.PartiallyChecked : Qt.Unchecked
  5. nextCheckState: function() {
  6. if (checkState === Qt.Unchecked)
  7. return Qt.Checked
  8. else if (checkState === Qt.Checked)
  9. return Qt.PartiallyChecked
  10. else
  11. return Qt.Unchecked
  12. }
  13. }

ButtonGroup

属性

  1. buttons : list<AbstractButton> //管理的按钮
  2. checkedButton : AbstractButton //当组合是排他的且当前有选中按钮的时候返回当前选中的按钮,否则返回null
  3. exclusive : bool //排他
  4. checkState : enumeration
  5. Qt.Unchecked //None of the buttons are checked.
  6. Qt.PartiallyChecked //Some of the buttons are checked.
  7. Qt.Checked //All of the buttons are checked.