1. 效果
2. 代码
import QtQuick 2.15
import QtQuick.Controls 2.15
ComboBox {
id: control
property real popHeight: 0
indicator: Canvas {
id: canvas
x: control.width - width - control.rightPadding;
y: control.topPadding + (control.availableHeight - height) / 2;
width: 20;
height: 12;
contextType: "2d";
onPaint: {
var ctx = getContext("2d")
ctx.reset();
ctx.moveTo(0, 0);
ctx.lineWidth = 3;
ctx.lineTo(width / 2, height*0.8);
ctx.lineTo(width, 0);
ctx.strokeStyle = control.enabled ? "black" : "grey"
ctx.stroke();
}
}
contentItem: Text {
leftPadding: 20
text: control.displayText
font: control.font
color: control.enabled ? "black" : "grey"
verticalAlignment: Text.AlignVCenter
}
background: Rectangle {
implicitWidth: 120
implicitHeight: 40
property string sColor: control.enabled ? "white" : "lightgrey"
color: control.pressed ? Qt.darker(sColor, 1.2) : sColor
border.width: 1
border.color: "lightgrey"
radius: 0
}
popup: Popup {
y: control.height
width: control.width
implicitHeight: control.popHeight === 0 ? contentItem.implicitHeight : control.popHeight
padding: 1
contentItem: ListView {
clip: true
implicitHeight: contentHeight
model: control.popup.visible ? control.delegateModel : null
}
background: Rectangle {
border.color: "black"
radius: 2
}
onAboutToHide: {
canvas.rotation = 0
}
onAboutToShow: {
canvas.rotation = 180
}
}
delegate: ItemDelegate {
width: control.width
contentItem: Text {
text: modelData
color: "black"
font: control.font
verticalAlignment: Text.AlignVCenter
}
}
}