I get exampel from this url: How do I Filter the PyQt QCombobox Items based on the text input? An example is what I need, but I have a problem with running this in created ui file. In ui file I have combobox.
When I use this:
self.ui.comboBox = ExtendedComboBox()
self.ui.comboBox.addItems(['test','test1','test3','test4'])
comboBox is empty and non activ.
When I use this:
self.ui.comboBox = ExtendedComboBox()
self.ui.comboBox.addItems(['test','test1','test3','test4'])
self.ui.comboBox.show()
comboBox from ui file is still empty,but appears in a separate window properly runnin ExtendedComboBox.
How to use ExtendedComboBox to work in ui file? Probably doing some stupid mistake
class ExtendedComboBox(QComboBox):
def __init__(self, parent=None):
super(ExtendedComboBox, self).__init__(parent)
self.setFocusPolicy(Qt.StrongFocus)
self.setEditable(True)
# add a filter model to filter matching items
self.pFilterModel = QSortFilterProxyModel(self)
self.pFilterModel.setFilterCaseSensitivity(Qt.CaseInsensitive)
self.pFilterModel.setSourceModel(self.model())
# add a completer, which uses the filter model
self.completer = QCompleter(self.pFilterModel, self)
# always show all (filtered) completions
self.completer.setCompletionMode(QCompleter.UnfilteredPopupCompletion)
self.setCompleter(self.completer)
# connect signals
self.lineEdit().textEdited[unicode].connect(self.pFilterModel.setFilterFixedString)
self.completer.activated.connect(self.on_completer_activated)
# 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)
# on model change, update the models of the filter and completer as well
def setModel(self, model):
super(ExtendedComboBox, 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(ExtendedComboBox, self).setModelColumn(column)
class CaDialogal(QDialog):
def __init__(self):
QDialog.__init__(self)
# Set up the user interface from Designer.
self.ui = Ui_CaDialog()
self.ui.setupUi(self)
**self.ui.comboBox = ExtendedComboBox()
self.ui.comboBox.addItems(['test','test1','test3','test4'])
#self.ui.comboBox.show()
##self.ui.comboBox.addItems(['test','test1','test3','test4'])**