ReuseComboBox.qml 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import QtQuick
  2. import QtQuick.Controls
  3. import QtQuick.Layouts
  4. RowLayout {
  5. id: reusecombobox
  6. anchors.fill: parent
  7. spacing: 10
  8. property string strTitle: ""
  9. property string strValue: ""
  10. property var objInfos
  11. property var objChild
  12. ListModel
  13. {
  14. id: reusecombobox_listmodel
  15. }
  16. Rectangle {
  17. width: parent.width * 0.225
  18. height: parent.height
  19. color: "#00ffffff"
  20. Text {
  21. id: reusecombobox_text_title
  22. anchors.fill: parent
  23. font.pixelSize: 25
  24. color: "#ffffff"
  25. horizontalAlignment: Text.AlignRight
  26. verticalAlignment: Text.AlignVCenter
  27. }
  28. }
  29. Rectangle {
  30. x: parent.width * 0.225
  31. width: parent.width * 0.675
  32. height: parent.height * 0.725
  33. color: "#ffffff"
  34. radius: 5
  35. ComboBox {
  36. id: reusecombobox_text_value
  37. anchors.fill: parent
  38. focusPolicy: Qt.NoFocus
  39. anchors.margins: 2
  40. font.pointSize: 15
  41. focus: false
  42. background: Rectangle {
  43. color: "transparent"
  44. }
  45. font.pixelSize: 20
  46. padding:5
  47. font.styleName: "Regular"
  48. model: reusecombobox_listmodel;
  49. // 当前选中项改变时触发
  50. onCurrentIndexChanged: {
  51. objChild = objInfos[textAt(currentIndex)];
  52. strValue = textAt(currentIndex);
  53. }
  54. }
  55. }
  56. Connections {
  57. target: reusecombobox
  58. onStrTitleChanged: {
  59. reusecombobox_text_title.text = strTitle;
  60. }
  61. }
  62. Component.onCompleted: {
  63. reusecombobox_text_title.text = strTitle;
  64. }
  65. function setModel(value)
  66. {
  67. objInfos = {};
  68. var types = [];
  69. for (var i = 0; i < value.length; ++i)
  70. {
  71. objInfos[value[i].display] = value[i];
  72. types.push(value[i].display);
  73. }
  74. reusecombobox_text_value.model = types;
  75. }
  76. function setValue(value)
  77. {
  78. for (var i = 0; i < reusecombobox_text_value.model.length; ++i)
  79. {
  80. if (reusecombobox_text_value.model[i] === value)
  81. {
  82. reusecombobox_text_value.currentIndex = i;
  83. }
  84. }
  85. strValue = value;
  86. objChild = objInfos[reusecombobox_text_value.textAt(reusecombobox_text_value.currentIndex)];
  87. }
  88. }