我尝试使用combobox.contentitemimplicitwidth和combobox.WidestTextWhencthenced已完成,并且两者都无法正常工作。在这两种情况下,尺寸仅适用于第一个列表元素。
Consider覆盖ItemDelegate.ContentItem,并考虑使用Rowlayout而不是行。
ComboBox{
id: control
//implicitContentWidthPolicy: ComboBox.ContentItemImplicitWidth
//implicitContentWidthPolicy: ComboBox.WidestTextWhenCompleted + 24
implicitContentWidthPolicy: ComboBox.WidestTextWhenCompleted
model: ListModel {
id: listModel
ListElement {language: "English" ; flag: "https://upload.wikimedia.org/wikipedia/commons/thumb/8/83/Flag_of_the_United_Kingdom_%283-5%29.svg/320px-Flag_of_the_United_Kingdom_%283-5%29.svg.png"}
ListElement {language: "German" ; flag: "https://upload.wikimedia.org/wikipedia/en/b/ba/Flag_of_Germany.svg"}
ListElement {language: "Italian" ; flag: "https://upload.wikimedia.org/wikipedia/en/0/03/Flag_of_Italy.svg"}
}
delegate: ItemDelegate {
width: control.width
contentItem: RowLayout {
width: control.width
Image {
Layout.preferredWidth: 24
Layout.preferredHeight: 16
source: listModel.get(index).flag
}
Text {
text: listModel.get(index).language
font: control.font
elide: Text.ElideRight
verticalAlignment: Text.AlignVCenter
}
}
highlighted: control.highlightedIndex === index
}
contentItem: RowLayout {
Image {
Layout.leftMargin: 10
Layout.preferredWidth: 24
Layout.preferredHeight: 16
source: listModel.get(control.currentIndex).flag
}
Text {
text: listModel.get(control.currentIndex).language
font: control.font
elide: Text.ElideRight
verticalAlignment: Text.AlignVCenter
}
}
}
您可以在网上浏览它!