PyQt5 Combobox 自动补完 筛选 弹窗样式设置

在选项数量较少的时候,PyQt5 Combobox自身的下拉菜单形式非常方便,但是在处理大量选项的时候,下拉菜单中反复滚动查找显然不是很好的选择。

1.PyQt5 的Combobox默认支持 头部字符匹配检索模式。只要激活Combobox的editable功能之后,在输入框中输入任意字符,便会默认开始做头部匹配检索,并自动带出第一匹配项。

2.自动补完功能,completer函数会激活一个弹窗,将所有数据呈现到前台供选择点击。

3.筛选功能,默认提供连续字符的模糊检索,支持大小写识别,可以将符合要求的数据筛选出来,结合completer函数,可以将筛选过的数据弹出供参考点击

4.completer自带的弹窗样式字体比较小,可以通过setStylesheet函数进行字号设置,包括首页展示的数量上限,背景颜色,

PyQt5 筛选文本:https://www.cnpython.com/qa/70319
PyQt5 popup.setStylesheet函数:http://www.voidcn.com/article/p-helksvhl-buq.html
Qt5 setStylesheet相关的关键字段参考:https://blog.csdn.net/icebin008/article/details/75088159

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt, QSortFilterProxyModel
from PyQt5.QtWidgets import QCompleter, QComboBox,QStyleOptionViewItem

class ExtendedComboBox(QComboBox):
    def __init__(self, parent=None):
        super(ExtendedComboBox, self).__init__(parent)
      
        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)
        self.completer.popup().setStyleSheet("font:30px ;")#通过pop.setstylesheet()函数设置弹窗样式,字体、背景色等等。
        # 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)    


if __name__ == "__main__":
    import sys
    from PyQt5.QtWidgets import QApplication
    from PyQt5.QtCore import QStringListModel

    app = QApplication(sys.argv)

    string_list = ['morning', 'afternoon', 'hello world', 'good bye','nighty night']

    combo = ExtendedComboBox()

    # either fill the standard model of the combobox
    combo.addItems(string_list)

    combo.resize(300, 40)
    combo.show()
    

    sys.exit(app.exec_())

你可能感兴趣的:(PyQt5 Combobox 自动补完 筛选 弹窗样式设置)