checkable combobox with autocomplete
Unsolved
General and Desktop
-
Hello everyone!
I'm trying to implement a combobox with checkable items, so the user can multi-select, and search.Here is the code:
from PyQt5.QtWidgets import QApplication, QComboBox, QCompleter, QLineEdit from PyQt5.QtCore import Qt class CheckableComboBox(QComboBox): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.setEditable(True) self.setInsertPolicy(QComboBox.NoInsert) # Make Completer for Search Box self.completer = QCompleter() self.completer.setFilterMode(Qt.MatchContains) self.completer.setCaseSensitivity(Qt.CaseInsensitive) # Make Search Box for Combo Box self.search_box = QLineEdit() self.search_box.setCompleter(self.completer) self.search_box.setFocus() self.search_box.selectAll() self.setLineEdit(self.search_box) self.completer.setModel(self.model()) if __name__ == '__main__': app = QApplication([]) region = ['South', 'North', 'East', 'West'] combo = CheckableComboBox() combo.addItems(region) combo.show() model = combo.model() app.exec()
The checkboxes are being displayed, but the auto-compete does not work.
Any advice would be much appreciated!