user7172 user7172 - 1 month ago 13
Python Question

How to use ExtendedComboBox to work in ui file?

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'])**

Answer

You cannot replace a widget from the ui file by simply assigning directly to it.

There are several ways of working around this, but probably the easiest for your case would be to use widget promotion. To do this, go back to Qt Designer and right-click on the combo-box you want to replace, and select "Promote to...". In the dialog, set "Promoted class name" to "ExtendedComboBox", and set "Header file" to the python import path for the module that contains this class (e.g. myapp.mainwindow). Then click "Add", and "Promote", and you will see the class change from "QComboBox" to "ExtendedComboBox" in the Object Inspector pane.

Now when you re-generate your ui module with pyuic, you should see that it uses your ExtendedComboBox class and there will be an extra line at the bottom of the file like this:

    from myapp.mainwindow import ExtendedComboBox

With that in place, all you need to do is:

    self.ui.comboBox.addItems(['test','test1','test3','test4'])

and hopefully everything should work as you intended.

Comments