Using PyQt5, How do I make a QComboBox searchable?
Asked Answered
B

2

7

I am using PyQt5 to make a GUI. On it, I have a QComboBox that has a dropdown list that has over 400 items. I was wondering if there is any way in which I can type into the QComboBox to search for a matching case?

Bora answered 29/4, 2020 at 17:46 Comment(0)
W
13

You could use a QCompleter for this. For an editable QComboBox a QCompleter is created automatically. This completer performs case insensitive inline completion but you can adjust that if needed, for example

from PyQt5 import QtWidgets
from itertools import product

app = QtWidgets.QApplication([])

# wordlist for testing
wordlist = [''.join(combo) for combo in product('abc', repeat = 4)]

combo = QtWidgets.QComboBox()
combo.addItems(wordlist)

# completers only work for editable combo boxes. QComboBox.NoInsert prevents insertion of the search text
combo.setEditable(True)
combo.setInsertPolicy(QtWidgets.QComboBox.NoInsert)

# change completion mode of the default completer from InlineCompletion to PopupCompletion
combo.completer().setCompletionMode(QtWidgets.QCompleter.PopupCompletion)

combo.show()
app.exec()
Wilkens answered 29/4, 2020 at 20:3 Comment(1)
Wow! That was so simple to add to my pre-existing combo box!Bora
E
3

If anyone is still looking for a solution. The accepted one works, but it has a major flaw: you have to search from the beginning of the string (cannot find substrings).

This implementation allows to search substrings:

class SearchableComboBox(QComboBox):
def __init__(self, parent=None):
    super(SearchableComboBox, self).__init__(parent)

    self.setFocusPolicy(Qt.ClickFocus)
    self.setEditable(True)

    # prevent insertions into combobox
    self.setInsertPolicy(QComboBox.NoInsert)

    # filter model for matching items
    self.pFilterModel = QSortFilterProxyModel(self)
    self.pFilterModel.setFilterCaseSensitivity(Qt.CaseInsensitive)
    self.pFilterModel.setSourceModel(self.model())

    # completer that uses filter model
    self.completer = QCompleter(self.pFilterModel, self)
    self.completer.setCompletionMode(QCompleter.UnfilteredPopupCompletion)
    self.setCompleter(self.completer)

    # connect signals
    self.lineEdit().textEdited[str].connect(self.pFilterModel.setFilterFixedString)
    self.completer.activated.connect(self.on_completer_activated)

def on_completer_activated(self, text):
    if text:
        index = self.findText(text)
        self.setCurrentIndex(index)
        self.activated[str].emit(self.itemText(index))

def setModel(self, model):
    super(SearchableComboBox, self).setModel(model)
    self.pFilterModel.setSourceModel(model)
    self.completer.setModel(self.pFilterModel)

def setModelColumn(self, column):
    self.completer.setCompletionColumn(column)
    self.pFilterModel.setFilterKeyColumn(column)
    super(SearchableComboBox, self).setModelColumn(column)
Ejective answered 24/9 at 16:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.