Search code examples
python-3.xpyqt5

Filter a supplier_combo_box as user types, if the typed text matches anything in the combo box values. I use PyQt5 and python 3.11


Sorry for the formatting, I am new in Python, but I know Cobol and Pascal. So some bad habits I picked up from there.

class FilterComboBox(QtWidgets.QComboBox):
    def __init__(self, parent=None):
        super(FilterComboBox, self).__init__(parent)
        self.setFocusPolicy(QtCore.Qt.StrongFocus)
        self.setEditable(True)

        # add a filter model to filter matching items
        self.pFilterModel = QtCore.QSortFilterProxyModel(self)
        self.pFilterModel.setFilterCaseSensitivity(QtCore.Qt.CaseInsensitive)

        # add a completer, which uses the filter model
        self.completer = QtWidgets.QCompleter(self.pFilterModel, self)
        # filter the completions
        self.completer.setCompletionMode(QtWidgets.QCompleter.PopupCompletion)
        self.setCompleter(self.completer)

        # connect signals
        self.lineEdit().textEdited.connect(self.on_lineEdit_textEdited)
        self.completer.activated.connect(self.on_completer_activated)

    def on_lineEdit_textEdited(self, text):
        regex = QtCore.QRegExp(".*" + text + ".*", QtCore.Qt.CaseInsensitive, QtCore.QRegExp.RegExp)
        self.pFilterModel.setFilterRegExp(regex)

    # on selection of an item from the completer, select the corresponding item from combobox 
    def on_completer_activated(self, text):
        if text:
            index = self.findText(text)
            self.setCurrentIndex(index)
            self.activated[str].emit(self.itemText(index))

    # on model change, update the models of the filter and the completer as well 
    def setModel(self, model):
        super(FilterComboBox, self).setModel(model)
        self.pFilterModel.setSourceModel(model)
        self.completer.setModel(self.pFilterModel)

    # on model column change, update the model column of the filter and completer as well
    def setModelColumn(self, column):
        self.completer.setCompletionColumn(column)
        self.pFilterModel.setFilterKeyColumn(column)
        super(FilterComboBox, self).setModelColumn(column)

Works if the typed text matches the beginning of the values in the list of combo box items. But doesn't filter if it matches anything inside the string.


Solution

  • This is how I modified my class to work with the combo box

    class FilterComboBox(QtWidgets.QComboBox):
        def __init__(self, parent=None):
            super(FilterComboBox, self).__init__(parent)
            self.setEditable(True)
    
            # add a filter model to filter matching items
            self.pFilterModel = QtCore.QSortFilterProxyModel(self)
            self.pFilterModel.setSourceModel(self.model())
            self.pFilterModel.setFilterCaseSensitivity(QtCore.Qt.CaseInsensitive)
    
            # add a completer, which uses the filter model
            self.completer = QtWidgets.QCompleter(self.pFilterModel, self)
            # always show all (filtered) completions
            self.completer.setCompletionMode(QtWidgets.QCompleter.UnfilteredPopupCompletion)
            self.setCompleter(self.completer)
    
            # connect signals
            self.lineEdit().textEdited.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))
    

    I create my combo box like this

            # Create a label and a combo box
            supplier_label = QtWidgets.QLabel("Supplier:")
            supplier_combo_box = FilterComboBox()
            supplier_combo_box.addItems(['John Doe', 'Jane Doe', 'John Smith', 'Jane Smith'])