问题描述
我有一个 QComboBox,它列出了所有 Windows 的驱动器号,并让用户从中进行选择.在执行过程中,我们需要启用或禁用某些字母(不删除它们).
I have a QComboBox that list all Windows' drive letters and let the user choose among them.During the execution, we need to enable or disable some of the letters (without removing them).
这是基本代码:
all_letters = ["{}:".format(chr(i)) for i in range(90, 64, -1)] # Z: -> A:
all_letters.insert(0, "")
cb_letter = QtGui.QComboBox()
for l in all_letters:
cb_letter.addItem(l)
cb_letter.setCurrentIndex(0)
我可以找到一种解决方案(听起来真的很复杂)来禁用此处的条目,但没有办法重新启用它.
I could find a kind of solution (which sounds really complicated) for just disabling an entry here but no way to enable it back.
启用和禁用 QComboBox 任何条目的最佳方法是什么?
What would be the best way to enable and disable any entry of a QComboBox?
推荐答案
默认情况下,QComboBox
使用 QStandardItemModel
,所以 QStandardItem
可供您使用:
By default, QComboBox
uses a QStandardItemModel
, so all of the convenience methods of QStandardItem
are available to you:
cb_letter.model().item(2).setEnabled(False)
这篇关于PyQt 启用/禁用 QComboBox 中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!