'QComboBox , QCompleter how to preselect the first item

I have this code for my QComboBox with QCompleter:

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


        self.filter_model = QtCore.QSortFilterProxyModel(self)
        self.filter_model.setFilterCaseSensitivity(QtCore.Qt.CaseInsensitive)
        self.filter_model.setSourceModel(self.model())

        self.completer = QtWidgets.QCompleter(self.filter_model, self)
        self.completer.setCompletionMode(QtWidgets.QCompleter.UnfilteredPopupCompletion)
        self.setCompleter(self.completer)
        self.lineEdit().textEdited.connect(self.filter_model.setFilterFixedString)

Item list is just random, in this case ["asd", "kjh", "ddfg"]

If I enter text and the beginning matches, the filter works as expected and the first item is preselected.

Pre-Selected

If I now press enter, the preselected item appears in the line.

But if I enter text and the beginning does not match, the filter works as expected again, but the first element is not preselected.

Not-Pre-Selected

If I now press enter, the line remains jh.

How to make that the first element is always preselected, regardless of whether the beginning matches and how many elements are left after the filter?

Thanks in advance!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source