gpt4 book ai didi

qt - QtQuick.Controls 2.2 中的 QML 组合框样式问题

转载 作者:行者123 更新时间:2023-12-04 13:28:41 25 4
gpt4 key购买 nike

我是初学者。我正在尝试使用组合框来填充元素列表,但是当我尝试设置样式时,在显示文本时出现了一些问题。
这是代码:

import QtQuick 2.7
import QtQuick.Controls 2.2

Item {
property string btntext : "First"
signal dropDownIndexChanged(int index)

id: mainDropDown
ListModel{
id: modelList
ListElement{ text: "First" }
ListElement{ text: "Second" }
ListElement{ text: "Third" }
}

ComboBox {
id: comboButton
width: parent.width
height: parent.height
model:modelList
currentIndex: 0
editText : btntext

Image {
id: imageMainButton
x: 119
anchors.top: parent.verticalCenter
anchors.right: parent.right
anchors.rightMargin: 9
anchors.topMargin: -7
fillMode: Image.Tile
sourceSize.height: 25
sourceSize.width: 25
source: "<some image>"
}
delegate: ItemDelegate {
id:itemDelegate
width: comboButton.width

background:Rectangle{
gradient: Gradient {
GradientStop {
position: 0.0
color: itemDelegate.down ? "white" : "blue"
}
GradientStop {
position: 1.0
color: itemDelegate.down ? "yellow" : "orange"
}
}
}

contentItem: Text {
text: modelData
elide: Text.ElideRight

horizontalAlignment: Text.AlignLeft
verticalAlignment: Text.AlignVCenter
font.pointSize: 11
font.family: "Arial"
color: itemDelegate.down ? "black" : "white"
}
highlighted: comboButton.highlightedIndex === index

}

indicator: Canvas {
}



//When this is added combo box text disapears or will be empty until something else is selected from the dropdown.
contentItem: Text {
text: comboButton.displayText
anchors.centerIn: parent

//font: comboButton.font
horizontalAlignment: Text.AlignLeft
verticalAlignment: Text.AlignVCenter
elide: Text.ElideRight

renderType: Text.NativeRendering
anchors.left : parent.left
anchors.leftMargin: 10
font.family: "Verdena"
font.pointSize: 12
font.bold: true
color: "white"
}


background: Rectangle {
implicitWidth: 120
implicitHeight: 40
radius: 2

color : "white"
//height:100
smooth: true
//border.width: 1
border.color: "white"


}

popup: Popup {
y: comboButton.height
width: comboButton.width -5
//implicitHeight: contentItem.implicitHeight -1
padding: 1

background: Rectangle {
border.color: "black"
radius: 2
color : "white"
}

contentItem: ListView {
//clip: true
implicitHeight: contentHeight
model: comboButton.popup.visible ? comboButton.delegateModel : null
currentIndex: comboButton.highlightedIndex
interactive: false

}
}

onCurrentIndexChanged:
{
btntext = mainDropDown.get(currentIndex).text
dropDownIndexChanged(currentIndex)
console.log(btntext ,currentIndex)
}
}
}

1) 如上所述,为什么在我从下拉列表中选择一个项目之前,组合框文本不会显示?

2) 所选的索引/项目根本没有突出显示。

最佳答案

1) As mentioned above why does combobox text is not displayed until I select an item from the drop down?



这是因为您的背景矩形颜色是“白色”,与您的文本颜色相同(“白色”是默认颜色)。

2) The selected index/item is not highlighted at all.



这是因为里面 代表 (id: itemDelegate),您正在根据 更改颜色itemDelegate.down 健康)状况。将此更改为 itemDelegate.highlighted .

关于qt - QtQuick.Controls 2.2 中的 QML 组合框样式问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45229221/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com