3

I'm currently in the process of porting a large application from Py2/PySide 1.2.4 to Py3/PySide2 5.13.0 and I'm discovering a DeprecationWarning related to the use of QPixmapCache.find(key, pixmap).

c:\path\to\module.py:220: DeprecationWarning: QPixmapCache.find(const QString & key, QPixmap & pixmap) is deprecated
  if (QPixmapCache.find("image_key", pixmap) is False):

I would like to fix this deprecation warning, but the documentation isn't very helpful as it:

  • Actually at one place explicitly recommends the use of the deprecated function. (PySide2.QtGui.QPixmapCache.find(key))
  • Has two entries for static PySide2.QtGui.QPixmapCache.find(key, pixmap)
    • One is listed as deprecated.
    • The other is not.
  • Seems to have no advice on what the modern usage would be. (Or I didn't find it).

So, what is the recommended fix for the deprecated usage of PySide2.QtGui.QPixmapCache.find(key, pixmap)?

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
timmwagener
  • 2,368
  • 2
  • 19
  • 27

3 Answers3

1

The recommended fix is to avoid passing a pixmap as the second argument to find, since it is not really needed (see below). So your code should simply change to:

pixmap = QPixmapCache.find("image_key")
if pixmap is None:
    ...

The inclusion of the methods which take a second argument seems to be a bug (or misfeature) in PySide2. It probably should have only ever implemented these two overloads:

  • find(str) -> QPixmap

  • find(QPixmapCache.Key) -> QPixmap

The other methods are more specific to C++, where they are currently defined like this:

  • find(const QString &key, QPixmap *pixmap) -> bool

  • find(const QPixmapCache::Key &key, QPixmap *pixmap) -> bool

The second argument here is a pointer which Qt will set to the found pixmap. It has to be done this way in C++, because there is no way to return a tuple of (bool, QPixmap), as might have been done in Python. And by the same token, it makes little sense to implement such methods in PySide, because there are no pointers in Python. (I would guess that the deprecated method uses something like QPixmap.swap on the passed in argument to get similar behaviour).

The confusion in the current API/documentation should be reported on the PySide bug tracker. As a point of reference, PyQt5 only implements the first two methods shown above, which seems the most pythonic way of doing things. It's hard to see a good reason why any other overloads should be included (other than for backwards compatibility).

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
1

As @ekhumoro points out it looks like a bug, but the following method currently works using the QPixmapCache::Key:

from PySide2 import QtGui

if __name__ == '__main__':
    import sys

    app = QtGui.QGuiApplication(sys.argv)

    filename = "test.png"

    key = QtGui.QPixmapCache.Key()
    pm  = QtGui.QPixmap()

    for i in range(100):
        pix = QtGui.QPixmapCache.find(key)
        if pix is None:
            pm.load(filename)
            key = QtGui.QPixmapCache.insert(pm)
            print("load from filename")
        else:
            pm = pix

Output:

load from filename
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
0

So actually the one-argument version PySide2.QtGui.QPixmapCache.find(key) also raises a DeprecationWarning. In the end it had to be fixed with in the way @eyllanesc proposed, which was slightly inconvenient in my case, as I generated the keys beforehand from hashed data. Upvoted both answers and accepted the one from @eyllanesc. Thanks!

timmwagener
  • 2,368
  • 2
  • 19
  • 27