python - QComboBox with autocompletion works in PyQt4 but not in PySide -
i've got combo box custom completer worked fine in pyqt4, isn't working in pyside.
i have verified new completer replacing qcombobox's built in completer because inline completion no longer occurring. when run pyside, completer doesn't popup filtered list of options.
i've tried ensuring text str
or unicode
avoid differences between pyqt api 1 qstrings , pyside's use of python unicode types. changing text types has had no effect on either pyqt or pyside's behavior (pyqt keeps working, pyside doesn't work).
here code:
from pyside import qtcore 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 filter model filter matching items self.pfiltermodel = qtgui.qsortfilterproxymodel(self) self.pfiltermodel.setfiltercasesensitivity(qtcore.qt.caseinsensitive) self.pfiltermodel.setsourcemodel(self.model()) # add completer, uses filter model self.completer = qtgui.qcompleter(self.pfiltermodel, self) # show (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 item completer, select corresponding item 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 models of filter , completer def setmodel(self, model): super(advcombobox, self).setmodel(model) self.pfiltermodel.setsourcemodel(model) self.completer.setmodel(self.pfiltermodel) # on model column change, update model column of filter , completer 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 standard model of combobox combo.additems(names) combo.setmodelcolumn(0) combo.resize(300, 40) combo.show() sys.exit(app.exec_())
i figured out while writing question...
it appears while pyside qcompleter documentation lists option initialize qcompleter model , parent, isn't working.
the solution set model of completer after initialized.
here working code:
from pyside import qtcore 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 filter model filter matching items self.pfiltermodel = qtgui.qsortfilterproxymodel(self) self.pfiltermodel.setfiltercasesensitivity(qtcore.qt.caseinsensitive) self.pfiltermodel.setsourcemodel(self.model()) # add completer self.completer = qtgui.qcompleter(self) #set model qcompleter uses # - in pyside doing separate step worked better self.completer.setmodel(self.pfiltermodel) # show (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 item completer, select corresponding item 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 models of filter , completer def setmodel(self, model): super(advcombobox, self).setmodel(model) self.pfiltermodel.setsourcemodel(model) self.completer.setmodel(self.pfiltermodel) # on model column change, update model column of filter , completer 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 standard model of combobox combo.additems(names) combo.setmodelcolumn(0) combo.resize(300, 40) combo.show() sys.exit(app.exec_())
Comments
Post a Comment