alphanumeric alphanumeric - 1 year ago 178
Python Question

PyQt: How to set Combobox to Item knowing Item's text (a title)

Is it possible to set Combobox to an item knowing 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 a combobox to that item's index.

Answer Source

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:

There is also an equivalent findData method that matches by the item's data.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download