c – QML ComboBox项目DropDownMenu样式

前端之家收集整理的这篇文章主要介绍了c – QML ComboBox项目DropDownMenu样式前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在我的项目中使用ComboBox类型.是否可以改变下拉菜单的外观(颜色,形状,文字样式),还是需要使用矩形,ListView和其他类型的组合?

以下代码应用自定义,但对于保持灰色的下拉菜单未定义修改

  1. ComboBox {
  2. currentIndex: 2
  3. activeFocusOnPress: true
  4. style: ComboBoxStyle {
  5. id: comboBox
  6. background: Rectangle {
  7. id: rectCategory
  8. radius: 5
  9. border.width: 2
  10. color: "#fff"
  11.  
  12. Image {
  13. source: "pics/corner.png"
  14. anchors.bottom: parent.bottom
  15. anchors.right: parent.right
  16. anchors.bottomMargin: 5
  17. anchors.rightMargin: 5
  18. }
  19. }
  20. label: Text {
  21. verticalAlignment: Text.AlignVCenter
  22. horizontalAlignment: Text.AlignHCenter
  23. font.pointSize: 15
  24. font.family: "Courier"
  25. font.capitalization: Font.SmallCaps
  26. color: "black"
  27. text: control.currentText
  28. }
  29. }
  30.  
  31. model: ListModel {
  32. id: cbItems
  33. ListElement { text: "Banana" }
  34. ListElement { text: "Apple" }
  35. ListElement { text: "Coconut" }
  36. }
  37. width: 200
  38. }

解决方法

目前的公共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导入导入):

  1. ComboBox {
  2. id: Box
  3. currentIndex: 2
  4. activeFocusOnPress: true
  5. style: ComboBoxStyle {
  6. id: comboBox
  7. background: Rectangle {
  8. id: rectCategory
  9. radius: 5
  10. border.width: 2
  11. color: "#fff"
  12. }
  13. label: Text {
  14. verticalAlignment: Text.AlignVCenter
  15. horizontalAlignment: Text.AlignHCenter
  16. font.pointSize: 15
  17. font.family: "Courier"
  18. font.capitalization: Font.SmallCaps
  19. color: "black"
  20. text: control.currentText
  21. }
  22.  
  23. // drop-down customization here
  24. property Component __dropDownStyle: MenuStyle {
  25. __maxPopupHeight: 600
  26. __menuItemType: "comboBoxitem"
  27.  
  28. frame: Rectangle { // background
  29. color: "#fff"
  30. border.width: 2
  31. radius: 5
  32. }
  33.  
  34. itemDelegate.label: // an item text
  35. Text {
  36. verticalAlignment: Text.AlignVCenter
  37. horizontalAlignment: Text.AlignHCenter
  38. font.pointSize: 15
  39. font.family: "Courier"
  40. font.capitalization: Font.SmallCaps
  41. color: styleData.selected ? "white" : "black"
  42. text: styleData.text
  43. }
  44.  
  45. itemDelegate.background: Rectangle { // selection of an item
  46. radius: 2
  47. color: styleData.selected ? "darkGray" : "transparent"
  48. }
  49.  
  50. __scrollerStyle: ScrollViewStyle { }
  51. }
  52.  
  53. property Component __popupStyle: Style {
  54. property int __maxPopupHeight: 400
  55. property int submenuOverlap: 0
  56.  
  57. property Component frame: Rectangle {
  58. width: (parent ? parent.contentWidth : 0)
  59. height: (parent ? parent.contentHeight : 0) + 2
  60. border.color: "black"
  61. property real maxHeight: 500
  62. property int margin: 1
  63. }
  64.  
  65. property Component menuItemPanel: Text {
  66. text: "NOT IMPLEMENTED"
  67. color: "red"
  68. font {
  69. pixelSize: 14
  70. bold: true
  71. }
  72. }
  73.  
  74. property Component __scrollerStyle: null
  75. }
  76. }
  77.  
  78. model: ListModel {
  79. id: cbItems
  80. ListElement { text: "Banana" }
  81. ListElement { text: "Apple" }
  82. ListElement { text: "Coconut" }
  83. }
  84. width: 200
  85. }

这里__dropDownStyle被分配了MenuStyle类型.这种类型的某些属性被定制以获得期望的样式,特别是itemDelegate(其定义了组合框内的项目的外观)和框架(整体背景).有关详细信息,请参阅链接的MenuStyle API.总体结果:

请注意,这种方法在Windows和Android上完美工作,而在OSX上,代码完全被忽略.可以检查Qt安装中的qml样式文件(搜索像qml / QtQuick / Controls / Styles / Desktop这样的子路径)来查看w.r.t.的更改. Windows并尝试适应提供的解决方案.这部分留给读者.

猜你在找的C&C++相关文章