python-3.x - 使用 PyQt5,如何使 QComboBox 可搜索?
问题描述
我正在使用 PyQt5 制作 GUI。在它上面,我有一个 QComboBox,它有一个包含 400 多个项目的下拉列表。我想知道是否有任何方法可以输入 QComboBox 来搜索匹配的案例?
解决方案
您可以QCompleter
为此使用 a 。对于可编辑QComboBox
的 aQCompleter
是自动创建的。此完成程序执行不区分大小写的内联完成,但您可以根据需要进行调整,例如
from PyQt5 import QtWidgets
from itertools import product
app = QtWidgets.QApplication([])
# wordlist for testing
wordlist = [''.join(combo) for combo in product('abc', repeat = 4)]
combo = QtWidgets.QComboBox()
combo.addItems(wordlist)
# completers only work for editable combo boxes. QComboBox.NoInsert prevents insertion of the search text
combo.setEditable(True)
combo.setInsertPolicy(QtWidgets.QComboBox.NoInsert)
# change completion mode of the default completer from InlineCompletion to PopupCompletion
combo.completer().setCompletionMode(QtWidgets.QCompleter.PopupCompletion)
combo.show()
app.exec()
推荐阅读
- linux - 使用 awk,减去所有列中的前一行并打印结果
- python - 如何使用 tensorflow-gpu 获得子任务的多处理?
- swift - 我如何在导数中将“h”参数逼近为零?
- database - 简单的静态网站可以与数据库交互吗?
- python - 如何将两个过滤器同时应用于 Tweepy Streaming API 过滤器功能?
- flutter - 找不到名为“devtools-server-address”的选项。尝试从 AndroidStudio 运行颤振应用程序时
- arrays - 如何在 TRIE 结构中存储一个键的多个值?
- c++ - 这些 getter 和 setter 是纯粹的风格偏好还是它们在某些方面有所不同?哪一个是最好的”?
- reactjs - React Native - 从平面列表传递数据模型
- asp.net - 使用 ASP.NET Core Web Api 验证 3rd 方 Cookie