Hello,

I am trying to create image tiles that load asynchronously as they are displaying in a QGraphicsView. I have subclassed QGraphicsItem, and overloaded the pain method with the following:

Qt Code:
  1. void RasterTile::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget)
  2. {
  3. if(image.isNull())
  4. {
  5. TilePainter=painter;
  6. TileOption=option;
  7. TileWidget=widget;
  8. future = QtConcurrent::run(this, &RasterTile::LoadTilePixmap);
  9. watcher.setFuture(future);
  10.  
  11. }else
  12. {
  13. QRectF imageRect = image.rect();
  14. painter->drawImage(imageRect, image);
  15. }
  16.  
  17. }
To copy to clipboard, switch view to plain text mode 

Which calls the following in its own thread:

Qt Code:
  1. QImage RasterTile::LoadTilePixmap()
  2. {
  3. QMutexLocker locker(&mutex);
  4.  
  5. QImage img(nBlockXSize, nBlockYSize, QImage::Format_RGB32);
  6.  
  7. QRect rect(tilePosX*nBlockXSize, tilePosY*nBlockYSize, nBlockXSize, nBlockYSize);
  8.  
  9. reader->setClipRect(rect);
  10. reader->read(&img);
  11. if(reader->error())
  12. {
  13. qDebug("Not null error");
  14. qDebug()<<"Error string is: "<<reader->errorString();
  15. }
  16. return img;
  17.  
  18. }
To copy to clipboard, switch view to plain text mode 

mutex is a static member of my class which is defined in the .h, and in the cpp

QMutex RasterTile::mutex;

This code seems to malfunction. If i have like 5 tiles of an image, they all seem to get some data in them, but it is often scrambled and such. Occaisionally one will load all of the image data correctly. This leads me to believe I have something going wrong with the threading. Each tile has its own instance of a QImageReader, all pointing to the same file.

What am I doing wrong here?