PyQT5 (七十九)扩展QComboBox下拉列表控件,通过插件增加Items模糊搜索功能

下拉列表控件 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_())

 

  • 5
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个Python GUIPyQt5QComboBox控件的使用方法和实例: QComboBoxPyQt5中的一个下拉列表控件,可以方便地在图形界面中实现下拉列表功能。以下是QComboBox控件的详细使用方法和实例: 1. 在PyQt5中导入QComboBox控件: ``` python from PyQt5.QtWidgets import QComboBox ``` 2. 创建一个QComboBox控件对象: ``` python combo_box = QComboBox() ``` 3. 添加下拉列表框选项: 可以通过addItem()方法添加下拉列表框选项,也可以在创建QComboBox控件时直接传入选项列表: ``` python combo_box.addItem('选项1') combo_box.addItem('选项2') combo_box.addItem('选项3') # 或者 options = ['选项1', '选项2', '选项3'] combo_box = QComboBox(self) combo_box.addItems(options) ``` 4. 获取当前选中的选项: 可以通过currentIndex()方法获取当前选中的选项的索引值,也可以通过currentText()方法获取当前选中的选项的文本内容: ``` python index = combo_box.currentIndex() text = combo_box.currentText() ``` 5. 为下拉列表框添加事件: 可以为下拉列表框添加事件,例如选项改变时触发事件。可以通过currentIndexChanged()方法来实现: ``` python combo_box.currentIndexChanged.connect(self.on_combo_box_changed) def on_combo_box_changed(self, index): # index为当前选中的选项的索引值 print(index) ``` 下面是一个完整的例子: ``` python from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QComboBox, QVBoxLayout import sys class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setWindowTitle('QComboBox控件使用例子') self.setGeometry(300, 300, 300, 200) label = QLabel('请选择一项:', self) combo_box = QComboBox(self) combo_box.addItem('选项1') combo_box.addItem('选项2') combo_box.addItem('选项3') combo_box.currentIndexChanged.connect(self.on_combo_box_changed) layout = QVBoxLayout() layout.addWidget(label) layout.addWidget(combo_box) self.setLayout(layout) self.show() def on_combo_box_changed(self, index): print(index) if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_()) ``` 该例子创建了一个窗口,包含一个QLabel控件和一个QComboBox控件,当选项改变时会触发on_combo_box_changed()方法。当选项改变时,会在控制台输出当前选中的选项的索引值。 希望这个例子能够帮助你理解使用PyQt5中的QComboBox控件

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值