using QPixmapCache to speed up the performance

1.7k views Asked by At

I am trying to using QPixmapCache in my PyQt4 app, however it still seems to take time, Can anyone please have a look at my code below:

def data(self, index, role):

        if index.isValid() and role == QtCore.Qt.DecorationRole:
            row = index.row()
            column = index.column()
            value = self._listdata[row][column]
            key = "image:%s"% value
            pixmap = QtGui.QPixmap()
            # pixmap.load(value)
            if not QtGui.QPixmapCache.find(key, pixmap):
                pixmap=self.generatePixmap(value)
                QtGui.QPixmapCache.insert(key, pixmap)
            # pixmap.scaled(400,300, QtCore.Qt.KeepAspectRatio)
            return QtGui.QImage(pixmap)

        if index.isValid() and role == QtCore.Qt.DisplayRole:
            row = index.row()
            column = index.column()
            value = self._listdata[row][column]
            fileName = os.path.split(value)[-1]
            return os.path.splitext(fileName)[0]

    def generatePixmap(self, value):
        pixmap=QtGui.QPixmap()
        pixmap.load(value)
        pixmap.scaled(100, 120, QtCore.Qt.KeepAspectRatio)
        return pixmap
1

There are 1 answers

0
ekhumoro On

It looks like you are trying create a cache of images from a list of filenames.

When loading the images for the first time, a cache will only improve performance if there are lots of images that are the same. Each image has to be created at least once, so if the images are all (or mostly) different, they won't load any quicker. In fact, the overall performance could possibly be slightly worse, due to the overhead of the caching itself.

A cache will only provide a performance gain if the application needs to constantly refresh the list of images (in which case, you'd need to check that the files on disk haven't changed in the meantime).

You should also note that QPixmapCache has a cache limit which, by default, is 10 MiB for desktop systems. This can be set to whatever value you like, though.