title: QQuickImageProvider loads only around 1% of raw images
body: <p>I started to learn Qt and I wanted to make simple list of images in folder with thumbnail. I managed to get it kind of working, but it seems to randomly load only about 1 image in 500 and 1 image in 50 is black (screenshot below). Once scrolled away the thumbnail will probably not load again and some other might load this time. For the raw images I use libraw. </p>
  
  <p><strong><em>Few observations:</em></strong></p>
  
  <p><em>7.7.2017 - I tried to check if issue wasn't caused by broken files. Although libraw says thumbnails are jpg, they don't seem to be, because normal browsers are unable to browser neither success nor failed attemps, but better editors can open both the same. So it seems issue isn't with corrupted data being given to QImage</em></p>
  
  <p><em>When I hardcoded few photos to load, when there were fewer photos they almost always loaded properly. With increasing amounts they suddenly could all go black or not load at all. Maybe a multithreading issue?</em></p>
  
  <p><em>Libraw seems to always load files properly. I tested it by printing enough bytes in the middle of the file when it loaded and when it didn't</em></p>
  
  <p><em>When image is black, it thinks everything is fine, when image doesn't load at all it is because QImage has null content.</em></p>
  
  <p><em>When I add one image many times, all copies share the same fate (They all load or they all fail)</em></p>
  
  <pre><code>QImage RawImageProvider::requestImage(const QString &amp;id, QSize *size, const QSize &amp;requestedSize) {
  LibRaw rawProc;
  auto state = rawProc.open_file(id.toLatin1().data());
  QImage thumbnail;
  if(LIBRAW_SUCCESS == state) {
      if(LIBRAW_SUCCESS == rawProc.unpack_thumb()) {
          if (LIBRAW_THUMBNAIL_JPEG == rawProc.imgdata.thumbnail.tformat) {
              thumbnail.loadFromData((unsigned char*)rawProc.imgdata.thumbnail.thumb, rawProc.imgdata.thumbnail.tlength, "JPEG");
          }
      }
      rawProc.recycle();
  }
  return thumbnail;
  }
  </code></pre>
  
  <p>for displaying the images I use gridview and model-delegate</p>
  
  <pre><code>GridView {
      id: gridView
      anchors.rightMargin: 6
      anchors.leftMargin: 6
      anchors.bottomMargin: 6
      anchors.topMargin: 46
      anchors.fill: parent
      cellHeight: 200
      cellWidth: 185
      model: imageModel
      delegate: PhotoDelegate {}
  }
  </code></pre>
  
  <p>raw image provider and model are registered like this in main function</p>
  
  <pre><code>int main(int argc, char *argv[])
  {
  ...
  QQmlApplicationEngine engine;
  engine.addImageProvider(QLatin1String("rawProvider"), new RawImageProvider);
  
  AppData::setContext(engine.rootContext());
  AppData::notifyContext();
  ...
  }
  </code></pre>
  
  <p>AppData is my "static" class which holds list from which the model is created and pointer to context so I can easily access and update these values from other class which is called from qml (File Browser). The updating of model is done as followed every time data are changed.</p>
  
  <pre><code>void AppData::notifyContext() {
       context-&gt;setContextProperty("imageModel", QVariant::fromValue(dataList));
  }
  </code></pre>
  
  <p>I have really ran out of ideas how I could solve this, any help would be... well... helpful</p>
  
  <p><a href="https://i.stack.imgur.com/dMCrv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dMCrv.png" alt="this is how it looks"></a></p>