Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. What exactly is QIcon::cacheKey based on?
QtWS25 Last Chance

What exactly is QIcon::cacheKey based on?

Scheduled Pinned Locked Moved General and Desktop
8 Posts 2 Posters 2.5k Views
  • 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.
  • V Offline
    V Offline
    Violet Giraffe
    wrote on last edited by Violet Giraffe
    #1

    I have a cache of icons in my application. Naturally, I use QIcon::cacheKey. And I'm starting to suspect it doesn't do what I think it should. There are like 20-50 different icons, and yet my map<QIcon::cacheKey, QIcon> has 3000 entries. It's as if the hash (cacheKey) is based on the Pixmap object (which I've got a new one for every icon), rather than the actual image data.
    Consequently, what is the fastest way to obtain an image-data-based hash for caching QIcons? This part of the application is already underperforming, so hashing must be fast.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      It's not based on either, You should take a look at QIcon's implementation for the current algorithm used.

      How are you adding these icons to your cache ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      V 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        It's not based on either, You should take a look at QIcon's implementation for the current algorithm used.

        How are you adding these icons to your cache ?

        V Offline
        V Offline
        Violet Giraffe
        wrote on last edited by
        #3

        Yep, it definitely doesn't do what I thought it would.
        I'm requesting the icons using QFileIconProvider, and simply add them to the map:

        QIcon icon = _provider.icon(filePath);
        _cahce[icon.cacheKey()] = icon;

        If 2 different files have the same icon, which is very common, the icons I get will have the same image, but still different key - because it's not based on the contents.

        1 Reply Last reply
        0
        • V Offline
          V Offline
          Violet Giraffe
          wrote on last edited by Violet Giraffe
          #4

          This works, but I wonder if it's the fastest way.

          QCryptographicHash qCryptoHash(QCryptographicHash::Md5);
          const auto qimage = icon.pixmap(icon.availableSizes().front()).toImage();
          qCryptoHash.addData((const char*)qimage.constBits(), qimage.bytesPerLine() * qimage.height());
          const auto result = qCryptoHash.result();
          const qulonglong iconHash = *(qulonglong*)(result.data()) ^ *(qulonglong*)(result.data()+8);

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            No it's not, but since it should only be done once that might be enough

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • V Offline
              V Offline
              Violet Giraffe
              wrote on last edited by Violet Giraffe
              #6

              No, it's not done once. I take hash of a QIcon every time I need to look it up in the cache. Basically, I'm calculating hashes all the time.
              How can I optimize the code?

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Why not keep it with the icon after calculating at construction time ?

                How are you using that cache ? It sounds strange that you get 3000 different entries.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • V Offline
                  V Offline
                  Violet Giraffe
                  wrote on last edited by
                  #8

                  I no longer have 3000 entries after introducing the hash function that accounts for the image content.
                  I can't calculate the hash any less often because I'm getting new and new icons from QFileIconProvider, and I need to hash their contents to find out if I already have this icon in the cache.

                  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