下拉列表控件 QComboBox
1、如何将列表添加到QComboBox控件中
2、如何获取选中的列表项
3.扩展基本的ComboBox插件,增加模糊搜索功能,过滤出正确的Items
'''
下拉列表控件 QComboBox
1、如何将列表添加到QComboBox控件中
2、如何获取选中的列表项
3.扩展基本的ComboBox插件,增加模糊搜索功能,过滤出正确的Items
'''
import sys
from PyQt5.QtCore import QSortFilterProxyModel, Qt
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QComboBox, QCompleter
# 扩展ComboBox插件 增加Items模糊搜索功能
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.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)
self.activated[str].emit(self.itemText(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)
class QComboBoxDemo(QWidget):
def __init__(self):
super(QComboBoxDemo, self).__init__()
self.initUI()
def initUI(self):
# 设置窗体的标题
self.setWindowTitle("下拉列表空间的演示")
# 调整窗体大小
self.resize(300, 100)
# 使用垂直布局
layout = QVBoxLayout()
# 创建一个标签控件
self.label = QLabel('请选编程的语言')
# 创建下拉列表控件
self.cb = ExtendedComboBox()
# 添加可选内容
self.cb.addItem('C')
self.cb.addItem('C++')
self.cb.addItem('Python')
# 以列表形式添加内容
self.cb.addItems(['Java', 'C#', 'Ruby'])
self.cb.addItems(['汉语', '华语', '中文', '日本语', '韩语', '英文'])
self.cb.currentIndexChanged.connect(self.selectionChange)
layout.addWidget(self.label)
layout.addWidget(self.cb)
self.setLayout(layout)
def selectionChange(self,i):
self.label.setText(self.cb.currentText())
self.label.adjustSize()
for count in range(self.cb.count()):
print('选项' + str(count) + '=' + self.cb.itemText(count))
print('当前索引', i, '选项被改变', self.cb.currentText())
if __name__ == '__main__':
# 通过类创建一个应用
app = QApplication(sys.argv)
# 创建一个窗口
w = QComboBoxDemo()
# 这只窗口大小
# w.resize(600,400)
# 给窗口一个定位
# w.move(200,100)
# 设置窗口的标题
# w.setWindowTitle('引入 QTDesigner 设计的 UI')
# 实例化UI类
# ui = mwUI.mwUI()
# 初始化UI
# ui.setupUi(w)
# 让窗口显示
w.show()
sys.exit(app.exec_())