#!/usr/bin/env python

# -*- coding: utf-8 -*-

from PyQt4.QtCore import Qt

from PyQt4.QtGui import QCompleter, QComboBox, QSortFilterProxyModel

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

from PyQt4.QtGui import QStringListModel, QApplication

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()

sys.exit(app.exec_())

最新文章

  1. C++ STL 学习 :for_each与仿函数(functor)
  2. Web前端新人笔记之CSS值和单位
  3. CentOS7安装vim7.4
  4. WebApi学习总结系列第三篇(Http)此篇持续更新...
  5. [转] linux下的僵尸进程处理SIGCHLD信号
  6. js 中的 exec( )方法
  7. WPF之路四:窗体自适应
  8. aria2 加速百度网盘下载
  9. Python内置函数(36)——reversed
  10. git配置公钥
  11. ScheduledThreadPoolExecutor线程池scheduleAtFixedRate和scheduleWithFixedDelay的区别
  12. CountDownLatch与join的区别和联系
  13. capwap学习笔记——初识capwap(二)
  14. 第十八篇:融汇贯通--谈USB Video Class驱动
  15. spark 多语言编程
  16. mysql高可用架构 -> MHA配置binlog-server-06
  17. Delphi-Cross-Socket
  18. 理解PV操作和信号量
  19. Html遮罩层的显示(主要在于样式设置)
  20. $.getJSON()函数内的数据不能传到全局变量是怎么回事?

热门文章

  1. 读《MacTalk·人生元编程》及Mac经常使用软件
  2. QML设计登陆界面
  3. cocos2d-x CCAction(转载)
  4. [编译原理代码][NFA转DFA并最小化DFA并使用DFA进行词法分析]
  5. 【转】UIKit性能调优实战讲解
  6. 打开新窗口(window.open)
  7. javascript 实现jsonp
  8. Python 类 --基础与要点
  9. 浅述Oracle分布式事务概念
  10. NHibernate——基本映射(5)