属性
acceptableInput : bool
count : int
currentIndex : int
currentText : string
currentValue : string
delegate : Component
displayText : string
down : bool
editText : string
editable : bool
flat : bool
highlightedIndex : int
implicitIndicatorHeight : real
implicitIndicatorWidth : real
indicator : Item
inputMethodComposing : bool
inputMethodHints : flags
model : model
popup : Popup //弹窗 下拉弹窗是popup
pressed : bool
selectTextByMouse : bool
textRole : string
validator : Validator
valueRole : string
//筛选editText输入格式
validator : Validator
//整数IntValidator:
bottom : int
locale : string
top : int
//浮点数
DoubleValidator:
bottom : real
最小值
decimals : int
This property holds the validator's maximum number of digits after the decimal point. By default, this property contains a value of 1000.
此属性保存验证器小数点后的最大位数。默认情况下,此属性包含1000的值。
locale : string
notation : enumeration
top : real
最大值
//正则
RegExpValidator:
regExp : regExp //正则表达式使用/括起来
信号
void accepted() //编辑框回车触发
void activated(int index) //选中item就将这个index激活了并触发该信号
void highlighted(int index) //鼠标浮动到index时item高亮并触发信号
方法
void decrementCurrentIndex()
int find(string text, enumeration flags)
void incrementCurrentIndex()
int indexOfValue(object value)
void selectAll()
string textAt(int index)
属性使用
ComboBox{
// model: 3
textRole: "text"
valueRole: "value"
width: 300
// editable: true
model: [
{value:100, text: "Banana", color: "Yellow" },
{value:200, text: "Apple", color: "Green" },
{value:300, text: "Coconut", color: "Brown" }
]
//使用displayText 时不可编辑,否则displayText报错,猜测是currentText获取是正在修改
displayText: currentText + " "+currentValue
onAccepted: {
//eidtText 此属性指定用户为可编辑组合框操作的文本。
if (find(editText) === -1) { //当不能找到当前显示框中的文本就追加listmodel中
model.append({text: editText})
currentIndex = find(editText)
}
}
onCurrentIndexChanged: {
console.log("index: ", currentIndex)
}
onCurrentTextChanged: {
console.log("text: ", currentText)
}
onCurrentValueChanged: {
console.log("value: ", currentValue)
}
// onHighlighted: {
// console.log("onHighlighted: ", index)
// }
// onActivated: {
// console.log("onActivated: ", index)
// }
// onAccepted:{
// console.log("onAccepted: ")
// }
}
indicator
默认indicator:
indicator:Image{
id:img
width: 50
height: 50
anchors.right: parent.right
anchors.verticalCenter: parent.verticalCenter
source:"/more3.png"
}
background
background: Rectangle {
implicitWidth: 120
implicitHeight: 40
border.color: control.pressed ? "#17a81a" : "#21be2b"
border.width: control.visualFocus ? 2 : 1
radius: 2
}
ComboBox的background属性仅仅只设置显示下卡弹框这个可编辑框的显示样式,下拉弹出来的部分是需要单独设置的
contentItem
//控制当前控件的显示内容如何绘制
contentItem: Text {
leftPadding: 0
rightPadding: control.indicator.width + control.spacing
text: control.displayText
font: control.font
// color: control.pressed ? "#17a81a" : "#21be2b"
color: control.pressed ? "red" : "blue"
verticalAlignment: Text.AlignVCenter //纵向居中
elide: Text.ElideRight //右侧省略
}
delegate
//针对model中每一项的具体绘制
delegate: ItemDelegate {
width: control.width
contentItem: Text {
text: modelData
// color: "#21be2b"
color: index % 2 ? "orange" : "blue"
font: control.font
elide: Text.ElideRight
verticalAlignment: Text.AlignVCenter
}
highlighted: control.highlightedIndex === index
}
输入筛选器validator
ComboBox{
// model: 3
// textRole: "text"
// valueRole: "value"
width: 300
editable: true
// validator: IntValidator{
// top: 10
// bottom: 1
// }
validator: RegExpValidator{
// regExp: /^[0-9A-F]/
regExp: /[0-9A-F]+[.][0-9]*/
// regExp: /^\d{n}$/ //无效
}
// validator: DoubleValidator{
// top:1000
// bottom: 0
// decimals: 5
// }
onAcceptableInputChanged: { //当前是否匹配validator验证器
console.log(acceptableInput)
}
}
自定义
ComboBox {
id: control
property bool bIsPopupShowDown: true
model: ["First", "Second", "Third","First", "Second", "Third","First", "Second", "Third"]
background: Rectangle {
implicitWidth: 120
implicitHeight: 40
border.color: control.pressed ? "#17a81a" : "#21be2b"
border.width: control.visualFocus ? 2 : 1
radius: 2
}
//针对model中每一项的具体绘制
delegate: ItemDelegate {
width: control.width
contentItem: Text {
text: modelData
// color: "#21be2b"
color: index % 2 ? "orange" : "blue"
font: control.font
elide: Text.ElideRight
verticalAlignment: Text.AlignVCenter
}
highlighted: control.highlightedIndex === index
}
indicator:Image{
id:img
width: 50
height: 50
anchors.right: parent.right
anchors.verticalCenter: parent.verticalCenter
source:"/more3.png"
}
// indicator: Canvas {
// id: canvas
// x: control.width - width - control.rightPadding
// y: control.topPadding + (control.availableHeight - height) / 2
// width: 12
// height: 8
// contextType: "2d"
// Connections {
// target: control
// function onPressedChanged() { canvas.requestPaint(); }
// }
// onPaint: {
// context.reset();
// context.moveTo(0, 0);
// context.lineTo(width, 0);
// context.lineTo(width / 2, height);
// context.closePath();
// context.fillStyle = control.pressed ? "#17a81a" : "#21be2b";
// context.fill();
// }
// }
//控制当前控件的显示内容如何绘制
contentItem: Text {
leftPadding: 0
rightPadding: control.indicator.width + control.spacing
text: control.displayText
font: control.font
// color: control.pressed ? "#17a81a" : "#21be2b"
color: control.pressed ? "red" : "blue"
verticalAlignment: Text.AlignVCenter //纵向居中
elide: Text.ElideRight //右侧省略
}
popup: Popup {//delegate绘制单个项 而popup绘制整个下来窗体
// y: control.height - 1 //下拉窗体的位置
y: bIsPopupShowDown ? control.height +10 : -control.height -80 //控制下拉窗体是向上或者向下
width: control.width
implicitHeight: contentItem.implicitHeight
padding: 1
contentItem: ListView {
clip: true
// implicitHeight: contentHeight
implicitHeight:150
interactive: true //设置拖动可回弹
boundsBehavior: Flickable.StopAtBounds //拖动不会回弹脱离边界
model: control.popup.visible ? control.delegateModel : null
currentIndex: control.highlightedIndex
//下拉窗体中滚动时显示一个滚动条
// ScrollIndicator.vertical: ScrollIndicator { }
ScrollBar.vertical: ScrollBar{
policy: ScrollBar.AlwaysOn
}
}
background: Rectangle {
border.color: "#21be2b"
radius: 2
// height: 100
//阴影设置
layer.enabled: true
layer.effect: DropShadow{
horizontalOffset: 2 //向右偏移两个单位
verticalOffset: 3 //向下便宜两个单位的阴影
radius: 8.0
samples: 17
color: "#80000000"
}
}
}
}