extended_combo_box.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # Copyright (c) 2011, Dorian Scholz, TU Darmstadt
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 #
10 # * Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # * Redistributions in binary form must reproduce the above
13 # copyright notice, this list of conditions and the following
14 # disclaimer in the documentation and/or other materials provided
15 # with the distribution.
16 # * Neither the name of the TU Darmstadt nor the names of its
17 # contributors may be used to endorse or promote products derived
18 # from this software without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 # POSSIBILITY OF SUCH DAMAGE.
32 
33 try:
34  from python_qt_binding.QtCore import QSortFilterProxyModel # Qt 5
35 except ImportError:
36  from python_qt_binding.QtGui import QSortFilterProxyModel # Qt 4
37 from python_qt_binding.QtCore import Qt, Signal, Slot
38 from python_qt_binding.QtWidgets import QComboBox, QCompleter
39 
40 
41 class ExtendedComboBox(QComboBox):
42  setItems = Signal(list)
43 
44  def __init__(self, parent=None):
45  super(ExtendedComboBox, self).__init__(parent)
46 
47  self.setFocusPolicy(Qt.StrongFocus)
48  self.setEditable(True)
49 
50  # add a filter model to filter matching items
51  self.filter_model = QSortFilterProxyModel(self)
52  self.filter_model.setFilterCaseSensitivity(Qt.CaseInsensitive)
53  self.filter_model.setSourceModel(self.model())
54 
55  # add a completer, which uses the filter model
56  self.completer = QCompleter(self.filter_model, self)
57  # always show all (filtered) completions
58  self.completer.setCompletionMode(QCompleter.UnfilteredPopupCompletion)
59  self.setCompleter(self.completer)
60 
61  # connect signals
62  self.lineEdit().textEdited[str].connect(self.filter_model.setFilterFixedString)
63  self.completer.activated.connect(self.on_completer_activated)
64  self.setItems.connect(self.onSetItems)
65 
66  # on selection of an item from the completer, select the corresponding item from combobox
67  def on_completer_activated(self, text):
68  if text:
69  index = self.findText(text)
70  self.setCurrentIndex(index)
71 
72  # on model change, update the models of the filter and completer as well
73  def setModel(self, model):
74  super(ExtendedComboBox, self).setModel(model)
75  self.filter_model.setSourceModel(model)
77 
78  # on model column change, update the model column of the filter and completer as well
79  def setModelColumn(self, column):
80  self.completer.setCompletionColumn(column)
81  self.filter_model.setFilterKeyColumn(column)
82  super(ExtendedComboBox, self).setModelColumn(column)
83 
84  @Slot(list)
85  def onSetItems(self, items):
86  self.clear()
87  self.addItems(items)
88 
89 
90 if __name__ == "__main__":
91  import sys
92  from python_qt_binding.QtGui import QApplication
93 
94  app = QApplication(sys.argv)
95 
96  string_list = ['hola muchachos', 'adios amigos', 'hello world', 'good bye']
97 
98  combo = ExtendedComboBox()
99 
100  # either fill the standard model of the combobox
101  combo.addItems(string_list)
102 
103  # or use another model
104  # combo.setModel(QStringListModel(string_list))
105 
106  combo.resize(300, 40)
107  combo.show()
108 
109  sys.exit(app.exec_())


rqt_py_common
Author(s): Dorian Scholz , Isaac Saito, Dirk Thomas
autogenerated on Mon May 16 2022 02:49:40