Search code examples
pyqt4visibleqlistwidget

PyQt4: How can I get a list of the visible items of a QlistWidget?


I'm trying to dynamically load (in other thread so it's not blocking) different icons for the items in a QlistWidget. But the list is huge and so I'm only interested about loading the icons for the items shown at that precise time. Is there a way to get a list of the visible items of a QlistWidget?

Thanks


Solution

  • Get the indexes at the top and the bottom of the viewable area, and then iterate over the range of indexes they encompass:

    def visibleItems(listwidget):
        rect = listwidget.viewport().contentsRect()
        top = listwidget.indexAt(rect.topLeft())
        if top.isValid():
            bottom = listwidget.indexAt(rect.bottomLeft())
            if not bottom.isValid():
                bottom = listwidget.model().index(listwidget.count() - 1)
            for index in range(top.row(), bottom.row() + 1):
                yield listwidget.item(index)