PyQt 启用/禁用 QComboBox 中的元素 [英] PyQt Enable/Disable elements in a QComboBox

查看:85
本文介绍了PyQt 启用/禁用 QComboBox 中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 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 中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆