Search code examples
pythonqlistwidgetqtpy

How to create a list from visible items in QListWidget


I have a qlistwidget in which most items are hidden.

The amount of items shown in the widget are determined by the users input.

I would like to be able to take the shown items in the qlistwidget and turn them into a list.

Sometimes there will be 3/4 items shown.

How can I make a list of the 3 items shown in the qlistwidget?

Problem illustration:

Qlistwidget window: 
------------------
| item 1         |
| item 2         |
| item 3(hidden) |
| item 4         |
|                |
|                |
|                |
------------------

pseudocode:

list_of_visible_items = []

for item in Qlistwidget window:
    if item not hidden:
        list_of_visible_items.append(item)
    
print(list_of_visible_items)

[item 1, item 2, item 4]

Solution

  • If you want to get the texts of the visible items then you just have to iterate over the items, check their visibility and get the text:

    results = []
    for row in range(listwidget.count()):
        item = listwidget.item(row)
        if not item.isHidden():
            text = item.text()
            results.append(text)
    print(results)