Autocomplete QComboBox works in PyQt4, but not in PySide

I have a combo box with a special add-on that works fine in PyQt4 but doesn't work in PySide.

I checked that the new instance replaces the QComboBox built-in bundle because the built-in termination no longer occurs. However, when you start with PySide, the add-on does not produce a filtered list of parameters.

I also tried to make sure that all text is all str or all unicode in order to avoid differences between PyQt API 1 using QStrings and PySide for Python Unicode types. Changing the text types did not affect the behavior of PyQt or PySide (PyQt continues to work, PySide does not work).

Here is my code:

 from PySide import QtCore from PySide import QtGui #from PyQt4 import QtCore #from PyQt4 import QtGui class AdvComboBox(QtGui.QComboBox): def __init__(self, parent=None): super(AdvComboBox, self).__init__(parent) self.setFocusPolicy(QtCore.Qt.StrongFocus) self.setEditable(True) # add a filter model to filter matching items self.pFilterModel = QtGui.QSortFilterProxyModel(self) self.pFilterModel.setFilterCaseSensitivity(QtCore.Qt.CaseInsensitive) self.pFilterModel.setSourceModel(self.model()) # add a completer, which uses the filter model self.completer = QtGui.QCompleter(self.pFilterModel, self) # always show all (filtered) completions self.completer.setCompletionMode(QtGui.QCompleter.UnfilteredPopupCompletion) self.setCompleter(self.completer) # connect signals def filter(text): print "Edited: ", text, "type: ", type(text) self.pFilterModel.setFilterFixedString(str(text)) self.lineEdit().textEdited[unicode].connect(filter) 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): print "activated" if text: print "text: ", text index = self.findText(str(text)) print "index: ", index self.setCurrentIndex(index) # on model change, update the models of the filter and completer as well def setModel(self, model): super(AdvComboBox, 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(AdvComboBox, self).setModelColumn(column) if __name__ == "__main__": import sys app = QtGui.QApplication(sys.argv) combo = AdvComboBox() names = ['bob', 'fred', 'bobby', 'frederick', 'charles', 'charlie', 'rob'] # fill the standard model of the combobox combo.addItems(names) combo.setModelColumn(0) combo.resize(300, 40) combo.show() sys.exit(app.exec_()) 
+1
source share
1 answer

I figured this out when writing a question ...

It seems that the PySide QCompleter documentation provides an option to initialize QCompleter with the model and parent, it actually does not work.

The solution is to establish a complement model after initialization.

Here is the working code:

 from PySide import QtCore from PySide import QtGui class AdvComboBox(QtGui.QComboBox): def __init__(self, parent=None): super(AdvComboBox, self).__init__(parent) self.setFocusPolicy(QtCore.Qt.StrongFocus) self.setEditable(True) # add a filter model to filter matching items self.pFilterModel = QtGui.QSortFilterProxyModel(self) self.pFilterModel.setFilterCaseSensitivity(QtCore.Qt.CaseInsensitive) self.pFilterModel.setSourceModel(self.model()) # add a completer self.completer = QtGui.QCompleter(self) #Set the model that the QCompleter uses # - in PySide doing this as a separate step worked better self.completer.setModel(self.pFilterModel) # always show all (filtered) completions self.completer.setCompletionMode(QtGui.QCompleter.UnfilteredPopupCompletion) self.setCompleter(self.completer) # connect signals def filter(text): print "Edited: ", text, "type: ", type(text) self.pFilterModel.setFilterFixedString(str(text)) self.lineEdit().textEdited[unicode].connect(filter) 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): print "activated" if text: print "text: ", text index = self.findText(str(text)) print "index: ", index self.setCurrentIndex(index) # on model change, update the models of the filter and completer as well def setModel(self, model): super(AdvComboBox, 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(AdvComboBox, self).setModelColumn(column) if __name__ == "__main__": import sys app = QtGui.QApplication(sys.argv) combo = AdvComboBox() names = ['bob', 'fred', 'bobby', 'frederick', 'charles', 'charlie', 'rob'] # fill the standard model of the combobox combo.addItems(names) combo.setModelColumn(0) combo.resize(300, 40) combo.show() sys.exit(app.exec_()) 
+2
source

Source: https://habr.com/ru/post/1337453/


All Articles