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 from python_qt_binding.QtCore import Qt, Signal, Slot
00034 from python_qt_binding.QtGui import QComboBox, QCompleter, QSortFilterProxyModel
00035
00036
00037 class ExtendedComboBox(QComboBox):
00038 setItems = Signal(list)
00039
00040 def __init__(self, parent=None):
00041 super(ExtendedComboBox, self).__init__(parent)
00042
00043 self.setFocusPolicy(Qt.StrongFocus)
00044 self.setEditable(True)
00045
00046
00047 self.filter_model = QSortFilterProxyModel(self)
00048 self.filter_model.setFilterCaseSensitivity(Qt.CaseInsensitive)
00049 self.filter_model.setSourceModel(self.model())
00050
00051
00052 self.completer = QCompleter(self.filter_model, self)
00053
00054 self.completer.setCompletionMode(QCompleter.UnfilteredPopupCompletion)
00055 self.setCompleter(self.completer)
00056
00057
00058 self.lineEdit().textEdited[unicode].connect(self.filter_model.setFilterFixedString)
00059 self.completer.activated.connect(self.on_completer_activated)
00060 self.setItems.connect(self.onSetItems)
00061
00062
00063 def on_completer_activated(self, text):
00064 if text:
00065 index = self.findText(text)
00066 self.setCurrentIndex(index)
00067
00068
00069 def setModel(self, model):
00070 super(ExtendedComboBox, self).setModel(model)
00071 self.filter_model.setSourceModel(model)
00072 self.completer.setModel(self.filter_model)
00073
00074
00075 def setModelColumn(self, column):
00076 self.completer.setCompletionColumn(column)
00077 self.filter_model.setFilterKeyColumn(column)
00078 super(ExtendedComboBox, self).setModelColumn(column)
00079
00080 @Slot(list)
00081 def onSetItems(self, items):
00082 self.clear()
00083 self.addItems(items)
00084
00085
00086 if __name__ == "__main__":
00087 import sys
00088 from python_qt_binding.QtGui import QApplication
00089
00090 app = QApplication(sys.argv)
00091
00092 string_list = ['hola muchachos', 'adios amigos', 'hello world', 'good bye']
00093
00094 combo = ExtendedComboBox()
00095
00096
00097 combo.addItems(string_list)
00098
00099
00100
00101
00102 combo.resize(300, 40)
00103 combo.show()
00104
00105 sys.exit(app.exec_())