Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Repaint single item in QListView
Qt 6.12 LTS is out! See what's new in the release blog

Repaint single item in QListView

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 428 Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nhunter
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • jeremy_kJ Offline
      jeremy_kJ Offline
      jeremy_k
      wrote on last edited by
      #2

      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.

      Asking a question about code? http://eel.is/iso-c++/testcase/

      N 1 Reply Last reply
      4
      • jeremy_kJ jeremy_k

        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.

        N Offline
        N Offline
        nhunter
        wrote on last edited by
        #3

        @jeremy_k Thanks that worked great! I appreciate the helping hand.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt
        • Unsolved