Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Cache QImage
Forum Updated to NodeBB v4.3 + New Features

Cache QImage

Scheduled Pinned Locked Moved Unsolved Qt for Python
4 Posts 2 Posters 1.4k Views 1 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.
  • krzysieklfcK Offline
    krzysieklfcK Offline
    krzysieklfc
    wrote on last edited by
    #1

    I have an application which lists 32bit .exr image files and creates a thumbnail once each item from the list widget is selected. It would be useful to cache each thumbnail in memory in case an item is selected more than once. I have read that the memory managment beetween python and qt is tricky [1][2].

    I tried collecting the QImages in a dictionary to simply keep the references but it is not working properly (most times I get image artifacts, sometimes the thumbnail is looking fine but it's from another file). I'm sure there's better approach than this

    class Image(object):
    	WIDTH = 260
    	HEIGHT = 130
    	thumbnails = dict()
    
    	@staticmethod
    	def createThumbnail(name, buffer):
    		image = QtGui.QImage(buffer, Image.WIDTH, Image.HEIGHT, QtGui.QImage.Format_RGB888)
    		Image.collectThumbnail(name, image)
    		return image
    
    	@staticmethod
    	def collectThumbnail(name, image):
    		if name not in Image.thumbnails:
    			Image.thumbnails[name] = image
    
    #[...]
    
    if selected_file in Image.thumbnails:
    	image = Image.thumbnails[selected_file]
        self.setThumbnail(image)
    
    

    I noticed QCache is missing in PySide2 bindings.

    I'm using Pyside 2 (Qt 5.6.3)

    1 Reply Last reply
    0
    • Kent-DorfmanK Offline
      Kent-DorfmanK Offline
      Kent-Dorfman
      wrote on last edited by
      #2

      The only thing that comes to mind is whether your buffer is in the format that you think it is. Also, I don't generally use the dict() constructor, but prefer dict = {}. finally, I usually rely upon the KeyError exception to see if an item already exists in a dictionary.

      Nothing really jumps out as being obviously wrong with the above, so my suspicion is the format of buffer.

      krzysieklfcK 1 Reply Last reply
      1
      • Kent-DorfmanK Kent-Dorfman

        The only thing that comes to mind is whether your buffer is in the format that you think it is. Also, I don't generally use the dict() constructor, but prefer dict = {}. finally, I usually rely upon the KeyError exception to see if an item already exists in a dictionary.

        Nothing really jumps out as being obviously wrong with the above, so my suspicion is the format of buffer.

        krzysieklfcK Offline
        krzysieklfcK Offline
        krzysieklfc
        wrote on last edited by
        #3

        @Kent-Dorfman I totally missed the concept that QImage references the buffer, not copies it. I included it in my dictionary and now it works. Thank you.

        Kent-DorfmanK 1 Reply Last reply
        1
        • krzysieklfcK krzysieklfc

          @Kent-Dorfman I totally missed the concept that QImage references the buffer, not copies it. I included it in my dictionary and now it works. Thank you.

          Kent-DorfmanK Offline
          Kent-DorfmanK Offline
          Kent-Dorfman
          wrote on last edited by
          #4

          @krzysieklfc said in Cache QImage:

          @Kent-Dorfman I totally missed the concept that QImage references the buffer, not copies it. I included it in my dictionary and now it works. Thank you.

          Well yeah, reuse of the buffer ist verboten. Gotta make a copy. Glad you figured it out

          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 Extensions
          • Unsolved