Question

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

Was it helpful?

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)

OTHER TIPS

While I get a better approach, I found an ugly way to do it:

rectangle = parent.geometry()    
midx = rectangle.left() + (( rectangle.right() - rectangle.left()) / 2)    
y=rectangle.top()
itemlist = []
while y < rectangle.bottom():
    y +=  10 # random value just to not check every pixel.
    item = parent.itemAt(midx, y)
    if item not in itemlist and item is not None:                
        itemlist.append(item)  

Better solutions?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top