samb samb - 16 days ago 4
Python Question

PyQt Enable/Disable elements in a QComboBox

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

Here is the basic code :

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.

What would be the best way to enable and disable any entry of a QComboBox?

Answer

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)