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 try:
00034     from python_qt_binding.QtCore import QSortFilterProxyModel  # Qt 5
00035 except ImportError:
00036     from python_qt_binding.QtGui import QSortFilterProxyModel  # Qt 4
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         # add a filter model to filter matching items
00051         self.filter_model = QSortFilterProxyModel(self)
00052         self.filter_model.setFilterCaseSensitivity(Qt.CaseInsensitive)
00053         self.filter_model.setSourceModel(self.model())
00054 
00055         # add a completer, which uses the filter model
00056         self.completer = QCompleter(self.filter_model, self)
00057         # always show all (filtered) completions
00058         self.completer.setCompletionMode(QCompleter.UnfilteredPopupCompletion)
00059         self.setCompleter(self.completer)
00060 
00061         # connect signals
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     # on selection of an item from the completer, select the corresponding item from combobox
00067     def on_completer_activated(self, text):
00068         if text:
00069             index = self.findText(text)
00070             self.setCurrentIndex(index)
00071 
00072     # on model change, update the models of the filter and completer as well
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     # on model column change, update the model column of the filter and completer as well
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     # either fill the standard model of the combobox
00101     combo.addItems(string_list)
00102 
00103     # or use another model
00104     #combo.setModel(QStringListModel(string_list))
00105 
00106     combo.resize(300, 40)
00107     combo.show()
00108 
00109     sys.exit(app.exec_())


rqt_py_common
Author(s): Dorian Scholz, Isaac Saito
autogenerated on Wed May 3 2017 02:24:14