Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide QComboBox items instead of clearing them out

Tags:

python

pyqt

I can't find a way to hide QComboBox items. So far the only way to filter its items out is to delete the existing ones (with .clear() method). And then to rebuild the entire QComboBox again using its .addItem() method.

I would rather temporary hide the items. And when they are needed to unhide them back. Is hide/unhide on QCombobox items could be accomplished?

like image 430
alphanumeric Avatar asked Dec 01 '22 01:12

alphanumeric


1 Answers

In case someone still looking for an answer:

By default, QComboBox uses QListView to display the popup list and QListView has the setRowHidden() method:

qobject_cast<QListView *>(comboBox->view())->setRowHidden(0, true);

Edit: fix code according to @Tobias Leupold's comment.
Edit: Python version:

# hide row
view = comboBox.view()
view.setRowHidden(row, True)

# disable item
model = comboBox.model()
item = model.item(row)
item.setFlags(item.flags() & ~Qt.ItemIsEnabled)

# enable item
view.setRowHidden(row, false)
item.setFlags(item.flags() | Qt.ItemIsEnabled)
like image 69
Kef Avatar answered Dec 05 '22 12:12

Kef