以下代码应用自定义,但对于保持灰色的下拉菜单未定义修改:
ComboBox { currentIndex: 2 activeFocusOnPress: true style: ComboBoxStyle { ID: comboBox background: Rectangle { ID: rectcategory radius: 5 border.wIDth: 2 color: "#fff" Image { source: "pics/corner.png" anchors.bottom: parent.bottom anchors.right: parent.right anchors.bottommargin: 5 anchors.rightmargin: 5 } } label: Text { verticalAlignment: Text.AlignVCenter horizontalAlignment: Text.AlignHCenter Font.pointSize: 15 Font.family: "CourIEr" Font.cAPItalization: Font.SmallCaps color: "black" text: control.currentText } } model: ListModel { ID: cbItems ListElement { text: "Banana" } ListElement { text: "Apple" } ListElement { text: "Coconut" } } wIDth: 200}解决方法 目前的公共API不允许自定义下拉菜单,如 here.Qt 5.4,即Styles 1.3,刚刚介绍了一些属性来自定义字体和文本(docs here),但仍然没有公共访问下拉式定制.
此外,链接中提供的示例不适用于较新版本的Qt.这是一个使用Qt 5.3,Qt 5.4和Qt 5.5进行测试的修改版本(请记住将QtQuick.Controls.Private 1.0导入导入):
ComboBox { ID: Box currentIndex: 2 activeFocusOnPress: true style: ComboBoxStyle { ID: comboBox background: Rectangle { ID: rectcategory radius: 5 border.wIDth: 2 color: "#fff" } label: Text { verticalAlignment: Text.AlignVCenter horizontalAlignment: Text.AlignHCenter Font.pointSize: 15 Font.family: "CourIEr" Font.cAPItalization: Font.SmallCaps color: "black" text: control.currentText } // drop-down customization here property Component __dropDownStyle: MenuStyle { __maxPopupHeight: 600 __menuItemType: "comboBoxitem" frame: Rectangle { // background color: "#fff" border.wIDth: 2 radius: 5 } itemDelegate.label: // an item text Text { verticalAlignment: Text.AlignVCenter horizontalAlignment: Text.AlignHCenter Font.pointSize: 15 Font.family: "CourIEr" Font.cAPItalization: Font.SmallCaps color: styleData.selected ? "white" : "black" text: styleData.text } itemDelegate.background: Rectangle { // selection of an item radius: 2 color: styleData.selected ? "darkGray" : "transparent" } __scrollerStyle: ScrollVIEwStyle { } } property Component __popupStyle: Style { property int __maxPopupHeight: 400 property int submenuOverlap: 0 property Component frame: Rectangle { wIDth: (parent ? parent.contentWIDth : 0) height: (parent ? parent.contentHeight : 0) + 2 border.color: "black" property real maxHeight: 500 property int margin: 1 } property Component menuItemPanel: Text { text: "NOT IMPLEMENTED" color: "red" Font { pixelSize: 14 bold: true } } property Component __scrollerStyle: null } } model: ListModel { ID: cbItems ListElement { text: "Banana" } ListElement { text: "Apple" } ListElement { text: "Coconut" } } wIDth: 200}
这里__dropDownStyle被分配了MenuStyle
类型.这种类型的某些属性被定制以获得期望的样式,特别是itemDelegate(其定义了组合框内的项目的外观)和框架(整体背景).有关详细信息,请参阅链接的MenuStyle API.总体结果:
请注意,这种方法在windows和AndroID上完美工作,而在OSX上,代码完全被忽略.可以检查Qt安装中的qml样式文件(搜索像qml / QtQuick / Controls / Styles / Desktop这样的子路径)来查看w.r.t.的更改. windows并尝试适应提供的解决方案.这部分留给读者.
总结以上是内存溢出为你收集整理的c – QML ComboBox项目DropDownMenu样式全部内容,希望文章能够帮你解决c – QML ComboBox项目DropDownMenu样式所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)