Results 1 to 3 of 3

Thread: Loading new QGraphicsItems with QtConcurrent

  1. #1
    Join Date
    Jan 2011
    Posts
    22
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Loading new QGraphicsItems with QtConcurrent

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Loading new QGraphicsItems with QtConcurrent

    Your updateSceneSlot() should be:
    Qt Code:
    1. void Tile::updateSceneSlot()
    2. {
    3. image = future.result();
    4. prepareGeometryChange();
    5. }
    To copy to clipboard, switch view to plain text mode 

    and the concurrent call should be:
    Qt Code:
    1. future = QtConcurrent::run(this, &Tile::LoadTilePixmap);
    To copy to clipboard, switch view to plain text mode 
    with LoadTilePixmap being:
    Qt Code:
    1. QImage Tile::LoadTilePixmap() {
    2. QImage img = ...;
    3. for(...) { img.setPixel(...); }
    4. return img;
    5. }
    To copy to clipboard, switch view to plain text mode 

    otherwise you're modifying the image member variable from another thread at the same time using this variable from within the main thread. The concurrent call should return a ready image and only then you can assign it to the member variable and inform graphics view it might use it (with prepareGeometryChange() as your boundingRect changes).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2011
    Posts
    22
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Loading new QGraphicsItems with QtConcurrent

    Excellent. This did seem to work. It is my first time trying to use QtConcurrent, so its a little bewildering to me still yet.

Similar Threads

  1. Replies: 2
    Last Post: 8th December 2010, 11:51
  2. QtConcurrent, i need advice
    By SABROG in forum Qt Programming
    Replies: 10
    Last Post: 29th December 2009, 20:53
  3. qtconcurrent
    By knishaq in forum Qt Programming
    Replies: 4
    Last Post: 15th December 2009, 09:41
  4. QtConcurrent and QPainter
    By AwDogsgo2Heaven in forum Qt Programming
    Replies: 3
    Last Post: 14th July 2009, 09:35
  5. QtConcurrent
    By Brandybuck in forum An Introduction to QThreads
    Replies: 2
    Last Post: 9th May 2008, 15:10

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.