Is it possible to set QComboBox to an item knowing only an item's text value? I am trying to avoid looping through for i in range(myCombobox.count())
just to find an item's index so it could be used to set the current index.
Asked
Active
Viewed 7.3k times
38

ekhumoro
- 115,249
- 20
- 229
- 336

alphanumeric
- 17,967
- 64
- 244
- 392
1 Answers
75
Yes, there is QComboBox.findText, which will return the index of the matched item (or -1
, if there isn't one). By default, the search does exact, case-sensitive matching, but you can tweak the behaviour by passing some match-flags as the second argument. For example, to do case-insensitive matching:
index = combo.findText(text, QtCore.Qt.MatchFixedString)
if index >= 0:
combo.setCurrentIndex(index)
There is also an equivalent findData method that matches by the item's data.

ekhumoro
- 115,249
- 20
- 229
- 336
-
The case-sensitive is actually what I need. But that's good to know a search settings are tweakable. Thanks again! – alphanumeric Apr 02 '14 at 05:32
-
how about substring? should I post another question? – greendino Jul 24 '22 at 13:57
-
1@greendino Please consult the links in my answer, which will show you all the options. It sounds like you just need to use `Qt.MatchContains`. – ekhumoro Jul 24 '22 at 14:11