如何根据文本输入过滤PyQt QCombobox项目?

如何根据文本输入过滤PyQt QCombobox项目?,第1张

如何根据文本输入过滤PyQt QCombobox项目

试试这个代码,这是我在我的项目中使用的东西

import sysfrom PyQt4.QtGui import QComboBox, QApplication, QCompleter, QSortFilterProxyModel, QStandardItemModel, QStandardItemfrom PyQt4.QtCore import Qtclass ExtendedCombo( QComboBox ):    def __init__( self,  parent = None):        super( ExtendedCombo, self ).__init__( parent )        self.setFocusPolicy( Qt.StrongFocus )        self.setEditable( True )        self.completer = QCompleter( self )        # always show all completions        self.completer.setCompletionMode( QCompleter.UnfilteredPopupCompletion )        self.pFilterModel = QSortFilterProxyModel( self )        self.pFilterModel.setFilterCaseSensitivity( Qt.CaseInsensitive )        self.completer.setPopup( self.view() )        self.setCompleter( self.completer )        self.lineEdit().textEdited[unipre].connect( self.pFilterModel.setFilterFixedString )        self.completer.activated.connect(self.setTextIfCompleterIsClicked)    def setModel( self, model ):        super(ExtendedCombo, self).setModel( model )        self.pFilterModel.setSourceModel( model )        self.completer.setModel(self.pFilterModel)    def setModelColumn( self, column ):        self.completer.setCompletionColumn( column )        self.pFilterModel.setFilterKeyColumn( column )        super(ExtendedCombo, self).setModelColumn( column )    def view( self ):        return self.completer.popup()    def index( self ):        return self.currentIndex()    def setTextIfCompleterIsClicked(self, text):      if text:        index = self.findText(text)        self.setCurrentIndex(index)if __name__ == "__main__":    app = QApplication(sys.argv)    model = QStandardItemModel()    for i,word in enumerate( ['hola', 'adios', 'hello', 'good bye'] ):        item = QStandardItem(word)        model.setItem(i, 0, item)    combo = ExtendedCombo()    combo.setModel(model)    combo.setModelColumn(0)    combo.show()    sys.exit(app.exec_())


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5647307.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存