Unsolved Cache QImage
-
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)
-
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.
-
@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.
-
@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