extended_combo_box.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 # Copyright (c) 2011, Dorian Scholz, TU Darmstadt
00004 # All rights reserved.
00005 #
00006 # Redistribution and use in source and binary forms, with or without
00007 # modification, are permitted provided that the following conditions
00008 # are met:
00009 #
00010 #   * Redistributions of source code must retain the above copyright
00011 #     notice, this list of conditions and the following disclaimer.
00012 #   * Redistributions in binary form must reproduce the above
00013 #     copyright notice, this list of conditions and the following
00014 #     disclaimer in the documentation and/or other materials provided
00015 #     with the distribution.
00016 #   * Neither the name of the TU Darmstadt nor the names of its
00017 #     contributors may be used to endorse or promote products derived
00018 #     from this software without specific prior written permission.
00019 #
00020 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024 # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00028 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00030 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031 # POSSIBILITY OF SUCH DAMAGE.
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         # add a filter model to filter matching items
00047         self.filter_model = QSortFilterProxyModel(self)
00048         self.filter_model.setFilterCaseSensitivity(Qt.CaseInsensitive)
00049         self.filter_model.setSourceModel(self.model())
00050 
00051         # add a completer, which uses the filter model
00052         self.completer = QCompleter(self.filter_model, self)
00053         # always show all (filtered) completions
00054         self.completer.setCompletionMode(QCompleter.UnfilteredPopupCompletion)
00055         self.setCompleter(self.completer)
00056 
00057         # connect signals
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     # on selection of an item from the completer, select the corresponding item from combobox
00063     def on_completer_activated(self, text):
00064         if text:
00065             index = self.findText(text)
00066             self.setCurrentIndex(index)
00067 
00068     # on model change, update the models of the filter and completer as well
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     # on model column change, update the model column of the filter and completer as well
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     # either fill the standard model of the combobox
00097     combo.addItems(string_list)
00098 
00099     # or use another model
00100     #combo.setModel(QStringListModel(string_list))
00101 
00102     combo.resize(300, 40)
00103     combo.show()
00104 
00105     sys.exit(app.exec_())


rqt_py_common
Author(s): Dorian Scholz
autogenerated on Fri Jan 3 2014 11:54:45