属性

  1. acceptableInput : bool
  2. count : int
  3. currentIndex : int
  4. currentText : string
  5. currentValue : string
  6. delegate : Component
  7. displayText : string
  8. down : bool
  9. editText : string
  10. editable : bool
  11. flat : bool
  12. highlightedIndex : int
  13. implicitIndicatorHeight : real
  14. implicitIndicatorWidth : real
  15. indicator : Item
  16. inputMethodComposing : bool
  17. inputMethodHints : flags
  18. model : model
  19. popup : Popup //弹窗 下拉弹窗是popup
  20. pressed : bool
  21. selectTextByMouse : bool
  22. textRole : string
  23. validator : Validator
  24. valueRole : string
  25. //筛选editText输入格式
  26. validator : Validator
  27. //整数IntValidator:
  28. bottom : int
  29. locale : string
  30. top : int
  31. //浮点数
  32. DoubleValidator:
  33. bottom : real
  34. 最小值
  35. decimals : int
  36. This property holds the validator's maximum number of digits after the decimal point. By default, this property contains a value of 1000.
  37. 此属性保存验证器小数点后的最大位数。默认情况下,此属性包含1000的值。
  38. locale : string
  39. notation : enumeration
  40. top : real
  41. 最大值
  42. //正则
  43. RegExpValidator:
  44. regExp : regExp //正则表达式使用/括起来

信号

  1. void accepted() //编辑框回车触发
  2. void activated(int index) //选中item就将这个index激活了并触发该信号
  3. void highlighted(int index) //鼠标浮动到index时item高亮并触发信号

方法

  1. void decrementCurrentIndex()
  2. int find(string text, enumeration flags)
  3. void incrementCurrentIndex()
  4. int indexOfValue(object value)
  5. void selectAll()
  6. string textAt(int index)

属性使用

  1. ComboBox{
  2. // model: 3
  3. textRole: "text"
  4. valueRole: "value"
  5. width: 300
  6. // editable: true
  7. model: [
  8. {value:100, text: "Banana", color: "Yellow" },
  9. {value:200, text: "Apple", color: "Green" },
  10. {value:300, text: "Coconut", color: "Brown" }
  11. ]
  12. //使用displayText 时不可编辑,否则displayText报错,猜测是currentText获取是正在修改
  13. displayText: currentText + " "+currentValue
  14. onAccepted: {
  15. //eidtText 此属性指定用户为可编辑组合框操作的文本。
  16. if (find(editText) === -1) { //当不能找到当前显示框中的文本就追加listmodel中
  17. model.append({text: editText})
  18. currentIndex = find(editText)
  19. }
  20. }
  21. onCurrentIndexChanged: {
  22. console.log("index: ", currentIndex)
  23. }
  24. onCurrentTextChanged: {
  25. console.log("text: ", currentText)
  26. }
  27. onCurrentValueChanged: {
  28. console.log("value: ", currentValue)
  29. }
  30. // onHighlighted: {
  31. // console.log("onHighlighted: ", index)
  32. // }
  33. // onActivated: {
  34. // console.log("onActivated: ", index)
  35. // }
  36. // onAccepted:{
  37. // console.log("onAccepted: ")
  38. // }
  39. }

image.png

indicator

默认indicator:image.png

  1. indicator:Image{
  2. id:img
  3. width: 50
  4. height: 50
  5. anchors.right: parent.right
  6. anchors.verticalCenter: parent.verticalCenter
  7. source:"/more3.png"
  8. }

image.png

background

  1. background: Rectangle {
  2. implicitWidth: 120
  3. implicitHeight: 40
  4. border.color: control.pressed ? "#17a81a" : "#21be2b"
  5. border.width: control.visualFocus ? 2 : 1
  6. radius: 2
  7. }

ComboBox的background属性仅仅只设置显示下卡弹框这个可编辑框的显示样式,下拉弹出来的部分是需要单独设置的
image.png

contentItem

  1. //控制当前控件的显示内容如何绘制
  2. contentItem: Text {
  3. leftPadding: 0
  4. rightPadding: control.indicator.width + control.spacing
  5. text: control.displayText
  6. font: control.font
  7. // color: control.pressed ? "#17a81a" : "#21be2b"
  8. color: control.pressed ? "red" : "blue"
  9. verticalAlignment: Text.AlignVCenter //纵向居中
  10. elide: Text.ElideRight //右侧省略
  11. }

image.png 按下鼠标显示红色

delegate

  1. //针对model中每一项的具体绘制
  2. delegate: ItemDelegate {
  3. width: control.width
  4. contentItem: Text {
  5. text: modelData
  6. // color: "#21be2b"
  7. color: index % 2 ? "orange" : "blue"
  8. font: control.font
  9. elide: Text.ElideRight
  10. verticalAlignment: Text.AlignVCenter
  11. }
  12. highlighted: control.highlightedIndex === index
  13. }

image.png

输入筛选器validator

  1. ComboBox{
  2. // model: 3
  3. // textRole: "text"
  4. // valueRole: "value"
  5. width: 300
  6. editable: true
  7. // validator: IntValidator{
  8. // top: 10
  9. // bottom: 1
  10. // }
  11. validator: RegExpValidator{
  12. // regExp: /^[0-9A-F]/
  13. regExp: /[0-9A-F]+[.][0-9]*/
  14. // regExp: /^\d{n}$/ //无效
  15. }
  16. // validator: DoubleValidator{
  17. // top:1000
  18. // bottom: 0
  19. // decimals: 5
  20. // }
  21. onAcceptableInputChanged: { //当前是否匹配validator验证器
  22. console.log(acceptableInput)
  23. }
  24. }

image.pngimage.png

自定义

  1. ComboBox {
  2. id: control
  3. property bool bIsPopupShowDown: true
  4. model: ["First", "Second", "Third","First", "Second", "Third","First", "Second", "Third"]
  5. background: Rectangle {
  6. implicitWidth: 120
  7. implicitHeight: 40
  8. border.color: control.pressed ? "#17a81a" : "#21be2b"
  9. border.width: control.visualFocus ? 2 : 1
  10. radius: 2
  11. }
  12. //针对model中每一项的具体绘制
  13. delegate: ItemDelegate {
  14. width: control.width
  15. contentItem: Text {
  16. text: modelData
  17. // color: "#21be2b"
  18. color: index % 2 ? "orange" : "blue"
  19. font: control.font
  20. elide: Text.ElideRight
  21. verticalAlignment: Text.AlignVCenter
  22. }
  23. highlighted: control.highlightedIndex === index
  24. }
  25. indicator:Image{
  26. id:img
  27. width: 50
  28. height: 50
  29. anchors.right: parent.right
  30. anchors.verticalCenter: parent.verticalCenter
  31. source:"/more3.png"
  32. }
  33. // indicator: Canvas {
  34. // id: canvas
  35. // x: control.width - width - control.rightPadding
  36. // y: control.topPadding + (control.availableHeight - height) / 2
  37. // width: 12
  38. // height: 8
  39. // contextType: "2d"
  40. // Connections {
  41. // target: control
  42. // function onPressedChanged() { canvas.requestPaint(); }
  43. // }
  44. // onPaint: {
  45. // context.reset();
  46. // context.moveTo(0, 0);
  47. // context.lineTo(width, 0);
  48. // context.lineTo(width / 2, height);
  49. // context.closePath();
  50. // context.fillStyle = control.pressed ? "#17a81a" : "#21be2b";
  51. // context.fill();
  52. // }
  53. // }
  54. //控制当前控件的显示内容如何绘制
  55. contentItem: Text {
  56. leftPadding: 0
  57. rightPadding: control.indicator.width + control.spacing
  58. text: control.displayText
  59. font: control.font
  60. // color: control.pressed ? "#17a81a" : "#21be2b"
  61. color: control.pressed ? "red" : "blue"
  62. verticalAlignment: Text.AlignVCenter //纵向居中
  63. elide: Text.ElideRight //右侧省略
  64. }
  65. popup: Popup {//delegate绘制单个项 而popup绘制整个下来窗体
  66. // y: control.height - 1 //下拉窗体的位置
  67. y: bIsPopupShowDown ? control.height +10 : -control.height -80 //控制下拉窗体是向上或者向下
  68. width: control.width
  69. implicitHeight: contentItem.implicitHeight
  70. padding: 1
  71. contentItem: ListView {
  72. clip: true
  73. // implicitHeight: contentHeight
  74. implicitHeight:150
  75. interactive: true //设置拖动可回弹
  76. boundsBehavior: Flickable.StopAtBounds //拖动不会回弹脱离边界
  77. model: control.popup.visible ? control.delegateModel : null
  78. currentIndex: control.highlightedIndex
  79. //下拉窗体中滚动时显示一个滚动条
  80. // ScrollIndicator.vertical: ScrollIndicator { }
  81. ScrollBar.vertical: ScrollBar{
  82. policy: ScrollBar.AlwaysOn
  83. }
  84. }
  85. background: Rectangle {
  86. border.color: "#21be2b"
  87. radius: 2
  88. // height: 100
  89. //阴影设置
  90. layer.enabled: true
  91. layer.effect: DropShadow{
  92. horizontalOffset: 2 //向右偏移两个单位
  93. verticalOffset: 3 //向下便宜两个单位的阴影
  94. radius: 8.0
  95. samples: 17
  96. color: "#80000000"
  97. }
  98. }
  99. }
  100. }

image.png