Hello,

I am trying to create a QGraphicsItem that pulls data from the disk in a threaded manner. In my case, the images are very large, so I am creating Tiles that will each pull a section from the same data source.

Here is the code from my Tile.cpp class that is subclassing QGraphicsitem

Qt Code:
  1. #include "tile.h"
  2.  
  3. Tile::Tile(QGraphicsItem *parent) : QGraphicsItem(parent)
  4. {
  5. }
  6.  
  7. Tile::~Tile()
  8. {
  9. }
  10. Tile::Tile(QGraphicsItem *parent)
  11. : QGraphicsItem(parent)
  12. {
  13.  
  14.  
  15. this->image = NULL;
  16.  
  17. this->future = new QFuture<void>;
  18. this->watcher = new QFutureWatcher<void>;
  19. connect(watcher,SIGNAL(finished()),this,SLOT(updateSceneSlot()));
  20. }
  21. QRectF Tile::boundingRect() const
  22. {
  23. if(image == NULL)
  24. return(QRectF(0,0,0,0));
  25.  
  26. return(QRectF(image->rect()));
  27. }
  28.  
  29. void Tile::updateSceneSlot()
  30. {
  31. qDebug("updateSceneSlot Thread id %i", QThread::currentThread());
  32. this->paint(TilePainter, TileOption, TileWidget);
  33. }
  34.  
  35. void Tile::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget)
  36. {
  37.  
  38.  
  39. if(image==NULL)
  40. {
  41. qDebug("Image is null");
  42. TilePainter=painter;
  43. TileOption=option;
  44. TileWidget=widget;
  45. qDebug()<<"Paint Thread id "<< QThread::currentThread();
  46. *future=QtConcurrent::run(this, &Tile::LoadTilePixmap);
  47. watcher->setFuture(*future);
  48.  
  49. }else
  50. {
  51. qDebug("Image is not null");
  52. QPointF *p = new QPointF(0.0,0.0);
  53. painter->drawImage(*p, *image);
  54.  
  55. }
  56. }
  57.  
  58. void Tile::LoadTilePixmap()
  59. {
  60. qDebug("Loading Pixmap");
  61. /*...go out to disk here, and populate floatData...*/
  62.  
  63. image = new QImage(nXSize, nYSize, QImage::Format_RGB32);
  64. for (int i = 0 ; i < nYSize ; i++)
  65. {
  66. for (int j = 0 ; j < nXSize ; j++)
  67. {
  68. image->setPixel(j,i,qRgb((unsigned char)floatData[i*nXSize+j],(unsigned char)floatData[i*nXSize+j],(unsigned char)floatData[i*nXSize+j]));
  69. }
  70. }
  71.  
  72. qDebug("Image complete loading");
  73. if(image != NULL)
  74. qDebug("Image not null");
  75.  
  76. }
To copy to clipboard, switch view to plain text mode 
Here is the header file:

Qt Code:
  1. #ifndef TILE_H
  2. #define TILE_H
  3.  
  4. #include <QGraphicsItem>
  5. #include <QThread>
  6. #include <QFutureWatcher>
  7. #include <QtConcurrentRun>
  8. #include "gdal_priv.h"
  9. #include <QPainter>
  10.  
  11. class Tile : public QObject, public QGraphicsItem
  12. {
  13. Q_OBJECT
  14. Q_INTERFACES(QGraphicsItem)
  15.  
  16. public:
  17. Tile(QGraphicsItem *parent = 0);
  18. ~Tile();
  19. void LoadTilePixmap();
  20.  
  21. protected:
  22. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget);
  23. QRectF boundingRect() const;
  24.  
  25. private:
  26. QFutureWatcher<void> *watcher;
  27. QFuture<void> *future;
  28. QImage *image;
  29. const QStyleOptionGraphicsItem *TileOption;
  30. QPainter *TilePainter;
  31. QWidget *TileWidget;
  32.  
  33.  
  34. signals:
  35.  
  36. public slots:
  37. void updateSceneSlot();
  38.  
  39. };
  40.  
  41. #endif // TILE_H
To copy to clipboard, switch view to plain text mode 

i have tried to just simply load 4 tiles from the disk in my main method, and my code is crashing with the following output:

Image is null
Paint Thread id QThread(0x6288ac0)
Image is null
Paint Thread id QThread(0x6288ac0)
Loading Pixmap
Image is null
Loading Pixmap
Paint Thread id QThread(0x6288ac0)
Image is null
Paint Thread id QThread(0x6288ac0)
Loading Pixmap
Loading Pixmap

X Error: BadAlloc (insufficient resources for operation) 11
Major opcode: 53 (X_CreatePixmap)
Resource id: 0x1e00293
X Error: BadDrawable (invalid Pixmap or Window parameter) 9
Extension: 156 (RENDER)
Minor opcode: 4 (RenderCreatePicture)
Resource id: 0x1e00293
X Error: BadDrawable (invalid Pixmap or Window parameter) 9
Major opcode: 55 (X_CreateGC)
Resource id: 0x1e00293
X Error: BadGC (invalid GC parameter) 13
Major opcode: 56 (X_ChangeGC)
Resource id: 0x1e00295
X Error: BadDrawable (invalid Pixmap or Window parameter) 9
Major opcode: 70 (X_PolyFillRectangle)
Resource id: 0x1e00293
X Error: BadGC (invalid GC parameter) 13
Major opcode: 60 (X_FreeGC)
Resource id: 0x1e00295
X Error: RenderBadPicture (invalid Picture parameter) 181
Extension: 156 (RENDER)
Minor opcode: 7 (RenderFreePicture)
Resource id: 0x1e00294
X Error: BadPixmap (invalid Pixmap parameter) 4
Major opcode: 54 (X_FreePixmap)
Resource id: 0x1e00293
X Error: BadAlloc (insufficient resources for operation) 11
Major opcode: 53 (X_CreatePixmap)
Resource id: 0x1e00296
X Error: BadDrawable (invalid Pixmap or Window parameter) 9
Extension: 156 (RENDER)
Minor opcode: 4 (RenderCreatePicture)
Resource id: 0x1e00296
X Error: BadDrawable (invalid Pixmap or Window parameter) 9
Major opcode: 55 (X_CreateGC)
Resource id: 0x1e00296
X Error: BadGC (invalid GC parameter) 13
Major opcode: 56 (X_ChangeGC)
Resource id: 0x1e00298
X Error: BadDrawable (invalid Pixmap or Window parameter) 9
Major opcode: 70 (X_PolyFillRectangle)
Resource id: 0x1e00296
X Error: BadGC (invalid GC parameter) 13
Major opcode: 60 (X_FreeGC)
Resource id: 0x1e00298
X Error: RenderBadPicture (invalid Picture parameter) 181
Extension: 156 (RENDER)
Minor opcode: 7 (RenderFreePicture)
Resource id: 0x1e00297
X Error: BadPixmap (invalid Pixmap parameter) 4
Major opcode: 54 (X_FreePixmap)
Resource id: 0x1e00296
X Error: BadAlloc (insufficient resources for operation) 11
Major opcode: 53 (X_CreatePixmap)
Resource id: 0x1e00299
X Error: BadDrawable (invalid Pixmap or Window parameter) 9
Extension: 156 (RENDER)
Minor opcode: 4 (RenderCreatePicture)
Resource id: 0x1e00299
X Error: BadDrawable (invalid Pixmap or Window parameter) 9
Major opcode: 55 (X_CreateGC)
Resource id: 0x1e00299
X Error: BadGC (invalid GC parameter) 13
Major opcode: 56 (X_ChangeGC)
Resource id: 0x1e0029b
X Error: BadDrawable (invalid Pixmap or Window parameter) 9
Major opcode: 70 (X_PolyFillRectangle)
Resource id: 0x1e00299
X Error: BadGC (invalid GC parameter) 13
Major opcode: 60 (X_FreeGC)
Resource id: 0x1e0029b
X Error: RenderBadPicture (invalid Picture parameter) 181
Extension: 156 (RENDER)
Minor opcode: 7 (RenderFreePicture)
Resource id: 0x1e0029a
X Error: BadPixmap (invalid Pixmap parameter) 4
Major opcode: 54 (X_FreePixmap)
Resource id: 0x1e00299
X Error: BadAlloc (insufficient resources for operation) 11
Major opcode: 53 (X_CreatePixmap)
Resource id: 0x1e0029c
X Error: BadDrawable (invalid Pixmap or Window parameter) 9
Extension: 156 (RENDER)
Minor opcode: 4 (RenderCreatePicture)
Resource id: 0x1e0029c
X Error: BadDrawable (invalid Pixmap or Window parameter) 9
Major opcode: 55 (X_CreateGC)
Resource id: 0x1e0029c
X Error: BadGC (invalid GC parameter) 13
Major opcode: 56 (X_ChangeGC)
Resource id: 0x1e0029e
X Error: BadDrawable (invalid Pixmap or Window parameter) 9
Major opcode: 70 (X_PolyFillRectangle)
Resource id: 0x1e0029c
X Error: BadGC (invalid GC parameter) 13
Major opcode: 60 (X_FreeGC)
Resource id: 0x1e0029e
X Error: RenderBadPicture (invalid Picture parameter) 181
Extension: 156 (RENDER)
Minor opcode: 7 (RenderFreePicture)
Resource id: 0x1e0029d
X Error: BadPixmap (invalid Pixmap parameter) 4
Major opcode: 54 (X_FreePixmap)
Resource id: 0x1e0029c
Image complete loading
Image not null
updateSceneSlot Thread id 103320256
Image is not null
The program has unexpectedly finished.


What do these errors mean? What am I doing wrong here?

Thanks