Retina (@2x) icons are not being loaded from resource (.qrc)



  • Hi, I have placed both the standard icon (app.png) and the retina one (app@2x.png) into a resource (.qrc).
    In designer I've chosen the app.png one, but it is not being changed automatically in retina display systems as expected.
    It have been working whereas I used images from folders, but now that I am trying to make my app more "portable" it stop working!.

    Any Idea??



  • My guess is that the automagic @2x kludge implemented by Apple is done when the OS X API functions (perhaps NSImage) see a matching "@2x" file in the same place as the file you actually asked for. Since the Qt resources are handled internally these virtual file accesses are not passed off to the OS X API which, understandably, cannot perform the magic of returning something other than what you asked for.



  • For using the "@2x" images with Qt I use a trick described in this topic:
    http://qt-project.org/forums/viewthread/44805/



  • It seems to be "automagic" isn't implemented on QPixmap.
    As you can see in the source code:
    https://github.com/Vitallium/qt5/blob/master/qtbase/src/gui/image/qicon.cpp
    QIcon makes the "automagic" for @2x files in the QIcon::addFile method, but QPixmap doesn't.



  • Well, I just did the same fix and overrided the involved methods
    It works at expected. My program is written in python. So I share the code in that language, but it can help someone else!.
    @
    def at2x_fix(fileName):
    if qApp.devicePixelRatio() >= 2:
    subfile, fext = os.path.splitext(fileName)
    fileRetina = subfile + "@2x" + fext

        if QFile(fileRetina).exists():
            return fileRetina
    return fileName
    

    def QPixmap_init(self, fileName, *args, **kwargs):
    fileName = at2x_fix(fileName)
    QPixmap_init_old(self, fileName, *args, **kwargs)
    QPixmap_init_old = QtGui.QPixmap.init
    QtGui.QPixmap.init = QPixmap_init

    def QPixmap_load(self, fileName, *args, **kwargs):
    fileName = at2x_fix(fileName)
    QPixmap_load_old(self, fileName, *args, **kwargs)
    QPixmap_load_old = QtGui.QPixmap.load
    QtGui.QPixmap.load = QPixmap_load
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.