@@ -21,6 +21,23 @@ ComboBox {
2121 // Whether the current input value is within the source model.
2222 readonly property bool validValue: sourceModel .includes (inputValue)
2323
24+
25+ QtObject {
26+ id: m
27+ readonly property int delegateModelCount: root .delegateModel .count
28+
29+ // Ensure the highlighted index is always within the range of delegates whenever the
30+ // combobox model changes, for combobox validation to always considers a valid item.
31+ onDelegateModelCountChanged: {
32+ if (delegateModelCount > 0 && root .highlightedIndex >= delegateModelCount) {
33+ while (root .highlightedIndex > 0 && root .highlightedIndex >= delegateModelCount) {
34+ // highlightIndex is read-only, this method has to be used to change it programmatically.
35+ root .decrementCurrentIndex ();
36+ }
37+ }
38+ }
39+ }
40+
2441 signal editingFinished (var value)
2542
2643 function clearFilter () {
@@ -42,14 +59,15 @@ ComboBox {
4259 return sourceModel .filter (item => {
4360 return item .toString ().toLowerCase ().includes (filterText .toLowerCase ());
4461 });
62+ }
63+
64+ popup .onOpened : {
65+ filterTextArea .forceActiveFocus ();
4566 }
4667
4768 popup .onClosed : clearFilter ()
4869
49- // Allows typing into the filter text area while the combobox has focus.
50- Keys .forwardTo : [filterTextArea]
51-
52- onActivated : index => {
70+ onActivated : (index ) => {
5371 const isValidEntry = model .length > 0 ;
5472 if (! isValidEntry) {
5573 return ;
@@ -63,10 +81,12 @@ ComboBox {
6381 states: [
6482 State {
6583 name: " Invalid"
66- when: root . delegateModel . count === 0
84+ when: m . delegateModelCount === 0
6785 PropertyChanges {
6886 target: filterTextArea
6987 color: Colors .orange
88+ // Prevent ComboBox validation when there are no entries in the model.
89+ Keys .forwardTo : []
7090 }
7191 }
7292 ]
@@ -85,10 +105,8 @@ ComboBox {
85105 placeholderText: " Type to filter..."
86106 Layout .fillWidth : true
87107 leftPadding: 18
88- MouseArea {
89- // Prevent textfield from stealing combobox's active focus, without disabling it.
90- anchors .fill : parent
91- }
108+ Keys .forwardTo : [root]
109+
92110 background: Item {
93111 MaterialLabel {
94112 anchors .verticalCenter : parent .verticalCenter
0 commit comments