Nominate our 2022 Qt Champions!

QImage::loadFromData failing

  • I'm building an image viewer app for raw files (extracting the embedded jpeg). I am sending a list of all the image absolute paths to a function that:

    • extracts the embedded jpeg into a QByteArray

    • creates a QImage via loadFromData

    • creates a Pixmap via from Image

    • adds the Pixmap to QPixmapCache

      void ImageView::loadImageCache(QFileInfoList &imageList)
          QPixmapCache::setCacheLimit(10240000);   //10GB
          for (int i=0; i < imageList.size(); ++i) {
              QFileInfo fileInfo =;
              QString imageFullPath = fileInfo.absoluteFilePath();
              QPixmap pm;
              loadPixmap(imageFullPath, pm);
              QPixmapCache::insert(imageFullPath, pm);
      void ImageView::loadPixmap(QString &imageFullPath, QPixmap &pm)
          QImage image;
          QFileInfo fileInfo(imageFullPath);
          QFile imFile(imageFullPath);
          QByteArray buf =>getLengthFullJPG(imageFullPath));
          bool imLoaded = image.loadFromData(buf, "JPEG");  // errors out after multiple calls
          pm = QPixmap::fromImage(image);

    When working on a folder with 36MP NEF files bool imLoaded becomes false and stays that way for all repeated executions for the remaining files in the folder after 5-6 files have been processed. When I send a folder of smaller jpegs I may get 30 files before imLoaded becomes false. The accumulated memory for the Pixmaps is usually around 1.2 - 1.4 GB when the image.loadFromData statement returns false. The memory estimate is based on (width * height * depth)/8 bytes. The QByteArray buf continues to work, no matter how many files I process.

    I'm hoping I am doing something obviously wrong. If not, I'm hoping someone can give me some advice on how to proceed to solve this.

    Running Qt 5.5 Win 10

    Thanks in advance

  • From QPixmap Documentation:
    Note that the pixel data in a pixmap is internal and is managed by the underlying window system.

    So, for Windows:

    • Pixmaps are stored as GDI-Objects
    • Memory for GDI-Objects is taken from windows paged kernel memory
    • paged kernel memory is limited by windows (on my 12GB win7 its around 1GB)

    If you really need to have 10GB of Images in memory you need

    • use QImage for storing data in memory
    • write your own Cache handling those Images
    • use Pixmap::fromImage(...) to display the Images

    Hope that helps

  • @Gerd

    Thanks Gerd. I'll start researching how to do that. Very much appreciated!


Log in to reply