使用 PyQt5,如何使 QComboBox 可搜索?

2024-02-21

我正在使用 PyQt5 制作 GUI。在它上面,我有一个 QComboBox,其中有一个包含 400 多个项目的下拉列表。我想知道是否有什么方法可以在 QComboBox 中输入来搜索匹配的案例?


你可以使用QCompleter为了这。对于可编辑的QComboBox a QCompleter是自动创建的。此完成器执行不区分大小写的内联完成,但您可以根据需要进行调整,例如

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()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 PyQt5,如何使 QComboBox 可搜索? 的相关文章

随机推荐