Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 try:
00034 from python_qt_binding.QtCore import QSortFilterProxyModel
00035 except ImportError:
00036 from python_qt_binding.QtGui import QSortFilterProxyModel
00037 from python_qt_binding.QtCore import Qt, Signal, Slot
00038 from python_qt_binding.QtWidgets import QComboBox, QCompleter
00039
00040
00041 class ExtendedComboBox(QComboBox):
00042 setItems = Signal(list)
00043
00044 def __init__(self, parent=None):
00045 super(ExtendedComboBox, self).__init__(parent)
00046
00047 self.setFocusPolicy(Qt.StrongFocus)
00048 self.setEditable(True)
00049
00050
00051 self.filter_model = QSortFilterProxyModel(self)
00052 self.filter_model.setFilterCaseSensitivity(Qt.CaseInsensitive)
00053 self.filter_model.setSourceModel(self.model())
00054
00055
00056 self.completer = QCompleter(self.filter_model, self)
00057
00058 self.completer.setCompletionMode(QCompleter.UnfilteredPopupCompletion)
00059 self.setCompleter(self.completer)
00060
00061
00062 self.lineEdit().textEdited[str].connect(self.filter_model.setFilterFixedString)
00063 self.completer.activated.connect(self.on_completer_activated)
00064 self.setItems.connect(self.onSetItems)
00065
00066
00067 def on_completer_activated(self, text):
00068 if text:
00069 index = self.findText(text)
00070 self.setCurrentIndex(index)
00071
00072
00073 def setModel(self, model):
00074 super(ExtendedComboBox, self).setModel(model)
00075 self.filter_model.setSourceModel(model)
00076 self.completer.setModel(self.filter_model)
00077
00078
00079 def setModelColumn(self, column):
00080 self.completer.setCompletionColumn(column)
00081 self.filter_model.setFilterKeyColumn(column)
00082 super(ExtendedComboBox, self).setModelColumn(column)
00083
00084 @Slot(list)
00085 def onSetItems(self, items):
00086 self.clear()
00087 self.addItems(items)
00088
00089
00090 if __name__ == "__main__":
00091 import sys
00092 from python_qt_binding.QtGui import QApplication
00093
00094 app = QApplication(sys.argv)
00095
00096 string_list = ['hola muchachos', 'adios amigos', 'hello world', 'good bye']
00097
00098 combo = ExtendedComboBox()
00099
00100
00101 combo.addItems(string_list)
00102
00103
00104
00105
00106 combo.resize(300, 40)
00107 combo.show()
00108
00109 sys.exit(app.exec_())