Repaint single item in QListView
-
I have subclassed QListView and QAbstractItemModel to display a list of files in IconMode. I want to use a default icon when I create and populate the view. After the view is displayed, I want to use a worker thread to create an icon from each file and then update the icon in the list view. I can not figure out how to do this. I have got everything working except that the icons in the list view do not update until I swipe the mouse across or resize the view or otherwise trigger a repaint. Instead I want them to update as they are loaded.
Here's the domain class.
class ImageFile: def __init__(self, path: str): self._path = path self._icon = None def getIcon(self) -> QIcon: return self._icon if self._icon is not None else QIcon(r'resources\icons\ImageFileIcon.png') def loadIcon(self) -> None: self._icon = QIcon(self._path)The model and view classes are boilerplate but I can post code if necessary.
Here's where I update the icons.
class MainWindow(QMainWindow): ... def loadThumbnails(self): worker = Worker(self._imageFileListModel._data.loadIcons) worker.signals.progress.connect(self.workerProgress) worker.signals.finished.connect(self.workerFinished) self._threadpool.start(worker) def workerProgress(self, i): print('Loaded icon #%d' % i) def workerFinished(self): print('Finished loading icons')I need some way to connect the ImageFile class and the representation in the list view, then I could perform the update in MainWindow.workerProgress method or maybe by emitting a signal in the ImageFile.loadIcon method. Is this is a job that could be performed by a delegate?
I'm stumped. Any suggestions gratefully received.
-
Store the icons in the model as a DecorationRole. When the custom icon is ready, call setData(), and the view will update if that index is visible.
-
Store the icons in the model as a DecorationRole. When the custom icon is ready, call setData(), and the view will update if that index is visible.