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. Why is there no overload for QPixmap::load(QIODevice *device, ...) ?
Forum Updated to NodeBB v4.3 + New Features

Why is there no overload for QPixmap::load(QIODevice *device, ...) ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 2 Posters 453 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.
  • DiracsbracketD Offline
    DiracsbracketD Offline
    Diracsbracket
    wrote on last edited by Diracsbracket
    #1

    Hi.
    I'm working with QNetworkDiskCache's data() method which returns a pointer to a QIODevice.

    It can be used directly with QImage::load(QIODevice *device...) to retrieve cached QImages.

    However, since I am using QPixmapCache, I want to directly store and retrieve QPixmap images from the QNetworkDiskCache as well.

    To store it in the cache, I can directly do:

        pixmap.save(cache, "XPM");
        icon_cache_->insert(cache);
    

    where cache is a QIODevice* and icon_cache_ is a QNetworkDiskCache*.

    However, to retrieve it, I need to use a QImage first, then convert it to QPixmap e.g. to insert it in the QPixmapCache:

    QImage cached_image;
    if (cached_image.load(cache.get(), "XPM")) {
        const QPixmap cached_pixmap = QPixmap::fromImage(cached_image);
        QPixmapCache::insert(cache_key, cached_pixmap);
    }
    

    From the source code in QPixmap, it is not directly clear what the actual implementation of the ::fromImage() is, but it does not seem to be a trivial operation?

    So why is there no QPixmap::load(QIODevice *device, ...) overload as is the case for QImage?. This is strange, since QPixmap does have a corresponding QPixmap::save(QIODevice *device, ...)version.

    Or is there another way to retrieve a QPixmap directly from the QNetworkCache without the need for a temporaryQImage?

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Diracsbracket said in Why is there no overload for QPixmap::load(QIODevice *device, ...) ?:

      QPixmap::save(QIODevice *device, ...)

      Which also simply converts the QPixmap to a QImage. You're most likely looking for QPixmap::fromImageReader()

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      DiracsbracketD 1 Reply Last reply
      2
      • Christian EhrlicherC Christian Ehrlicher

        @Diracsbracket said in Why is there no overload for QPixmap::load(QIODevice *device, ...) ?:

        QPixmap::save(QIODevice *device, ...)

        Which also simply converts the QPixmap to a QImage. You're most likely looking for QPixmap::fromImageReader()

        DiracsbracketD Offline
        DiracsbracketD Offline
        Diracsbracket
        wrote on last edited by Diracsbracket
        #3

        @Christian-Ehrlicher: Thanks!

        QPixmap::save(QIODevice *device, ...)

        Which also simply converts the QPixmap to a QImage.

        Indeed... However, it turns out that

        QPixmap::fromImageReader()
        

        also uses an intermediate QImage. It internally uses:

        void QPlatformPixmap::fromImageReader(QImageReader *imageReader,
                                          Qt::ImageConversionFlags flags)
        {
            const QImage image = imageReader->read();
            fromImage(image, flags);
        }
        

        The documentation for QPixmap::fromImageReader() states:

        On some systems, reading an image directly to QPixmap can use less memory than reading a QImage to convert it to QPixmap.
        

        Not sure what that means, as it always internally seems to use a QImage first. I'm not so much worried as much about memory in my case but more about doing unnecessary conversions.

        So sadly, Qt provides no way around always using a QImage in the end.

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Diracsbracket said in Why is there no overload for QPixmap::load(QIODevice *device, ...) ?:

          So sadly, Qt provides no way around always using a QImage in the end.

          QPixmap can not directly be modified (i.e. pixels) so it can also not be used to directly load pixel data from files. That's what QImage is for.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          DiracsbracketD 1 Reply Last reply
          1
          • Christian EhrlicherC Christian Ehrlicher

            @Diracsbracket said in Why is there no overload for QPixmap::load(QIODevice *device, ...) ?:

            So sadly, Qt provides no way around always using a QImage in the end.

            QPixmap can not directly be modified (i.e. pixels) so it can also not be used to directly load pixel data from files. That's what QImage is for.

            DiracsbracketD Offline
            DiracsbracketD Offline
            Diracsbracket
            wrote on last edited by
            #5

            Thanks @Christian-Ehrlicher.

            QPixmap can not directly be modified (i.e. pixels) so it can also not be used to directly load pixel data from files. That's what QImage is for.

            I have no idea about the underlying differences between QImage and QPixmap, so forgive my ignorance.
            Do you have any idea about how expensive the conversion from/to QPixmap
            actually is?

            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              It depends on the platform. On windows it's just a memcpy when the format of the image is Format_ARGB32_Premultiplied.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              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