I am using the QImageReader to read in all of the images in a directory. However it is sometimes unable to read certain images, even if it has read them successfully in the past. The error message is "Unable to read image data". Once there is an error on one file in a directory there tends to be an error on all subsequent files. Can someone explain what is going on? Here is my code:

Qt Code:
  1. QStringList filters;
  2. foreach (QByteArray format, QImageReader::supportedImageFormats())
  3. filters += "*." + format;
  4. dir.setSorting(QDir::Time | QDir::Reversed);
  5.  
  6. foreach (QFileInfo fileInfo, dir.entryInfoList(filters, QDir::Files))
  7. {
  8. QString filename = fileInfo.fileName();
  9. QString pathname = fileInfo.absoluteFilePath();
  10.  
  11. if (!imageReader)
  12. {
  13. imageReader = new QImageReader(pathname);
  14. }
  15. else
  16. {
  17. imageReader->setFileName(pathname);
  18. }
  19. QImage image = imageReader->read();
  20. if (!image.isNull())
  21. {
  22. Image* myImage = new Image(image);
  23. myImage->s_pathname(pathname);
  24. myImage->s_timestamp (fileInfo.lastModified());
  25. QList<Image *> *imglist = g_imageList();
  26. imglist->append(myImage);
  27. }
  28. else
  29. {
  30. /*
  31. ** Sometimes get the error "Unable to read image data" which is the
  32. ** "InvalidDataError". But it is inconsistent. The same file is
  33. ** successfully read on other occasions.
  34. ** When it fails with one file tends to fail with all other files from that
  35. ** directory. (filename is always ok).
  36. */
  37. QString errstr = imageReader->errorString();
  38. QString path = imageReader->fileName();
  39. }
  40. }
To copy to clipboard, switch view to plain text mode