-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtestUI.py
103 lines (74 loc) · 3.04 KB
/
testUI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QComboBox, QPushButton
class Example(QMainWindow):
def __init__(self):
super().__init__()
combo = QComboBox(self)
combo.addItem("Apple")
combo.addItem("Pear")
combo.addItem("Lemon")
combo.setEditable(True)
combo.move(50, 50)
self.qlabel = QLabel(self)
self.qlabel.move(50, 16)
combo.activated[str].connect(self.onChanged)
self.setGeometry(50, 50, 320, 200)
self.setWindowTitle("QLineEdit Example")
self.show()
def onChanged(self, text):
self.qlabel.setText(text)
self.qlabel.adjustSize()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
'''
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from PyQt5.QtCore import Qt, QSortFilterProxyModel
from PyQt5.QtWidgets import QComboBox, QCompleter, QApplication
from numpy import unicode
class ExtendedComboBox(QComboBox):
def __init__(self, parent=None):
super(ExtendedComboBox, self).__init__(parent)
self.setFocusPolicy(Qt.StrongFocus)
self.setEditable(True)
# add a filter model to filter matching items
self.pFilterModel = QSortFilterProxyModel(self)
self.pFilterModel.setFilterCaseSensitivity(Qt.CaseInsensitive)
self.pFilterModel.setSourceModel(self.model())
# add a completer, which uses the filter model
self.completer = QCompleter(self.pFilterModel, self)
# always show all (filtered) completions
self.completer.setCompletionMode(QCompleter.UnfilteredPopupCompletion)
self.setCompleter(self.completer)
# connect signals
self.lineEdit().textEdited[unicode].connect(self.pFilterModel.setFilterFixedString)
self.completer.activated.connect(self.on_completer_activated)
# on selection of an item from the completer, select the corresponding item from combobox
def on_completer_activated(self, text):
if text:
index = self.findText(text)
self.setCurrentIndex(index)
# on model change, update the models of the filter and completer as well
def setModel(self, model):
super(ExtendedComboBox, self).setModel(model)
self.pFilterModel.setSourceModel(model)
self.completer.setModel(self.pFilterModel)
# on model column change, update the model column of the filter and completer as well
def setModelColumn(self, column):
self.completer.setCompletionColumn(column)
self.pFilterModel.setFilterKeyColumn(column)
super(ExtendedComboBox, self).setModelColumn(column)
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
string_list = ['hola muchachos', 'adios amigos', 'hello world', 'good bye']
combo = ExtendedComboBox()
# either fill the standard model of the combobox
combo.addItems(string_list)
# or use another model
# combo.setModel(QStringListModel(string_list))
combo.resize(300, 40)
combo.show()
'''