Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing QtConcurrent to load a Pixmap and paint it
    primarykey
    data
    text
    <p>I'm trying to create a Tile rendering program. Heres some basic code.</p> <p>Header</p> <pre><code>class Tile: public QGraphicsItem { public: Tile(void); ~Tile(void); QGraphicsPixmapItem *tileItem; void update(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget); protected: QRectF boundingRect() const; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget); }; </code></pre> <p>CPP:</p> <pre><code>.Constructor etc . . void Tile::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget) { if(tileItem==NULL) { qDebug()&lt;&lt;"Loading Pixmap"; QPixmap p("c:\\qt\\tile\\tile0-0.png"); tileItem=new QGraphicsPixmapItem; tileItem-&gt;setPixmap(p); } tileItem-&gt;paint(painter,option,widget); } </code></pre> <p>I'm trying to make a application that will paste tiles of a big image onto a QGraphicsScene. But loading all the tiles at once is time consuming and takes up a lot of memory. So I'm subclassing QGraphicsItem and overriding paint. The paint method in the QGraphicsItem class is only called when a it comes into view inside the QGraphicsView. So by loading up my tile inside paint, I can basically create an application that loads tiles only when they come into view. This much is working so far.</p> <p>To make the user experience better I'm use QtConcurrent to try an load the tiles up in a seperate thread. SO here's the changes I've made.</p> <p>CPP</p> <pre><code>connect(&amp;watcher,SIGNAL(finished()),this,SLOT(updateSceneSlot())); void Tile::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget) { if(tileItem==NULL) { TilePainter=painter; TileOption=option; TileWidget=widget; qDebug()&lt;&lt;"Paint Thread id "&lt;&lt; QThread::currentThread(); future=QtConcurrent::run(LoadTilePixmap,this); watcher.setFuture(future); } else tileItem-&gt;paint(painter, option, widget); } </code></pre> <p>LoadTilePixmap function:</p> <pre><code>void LoadTilePixmap(Tile *temp,QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget) { qDebug()&lt;&lt;"Loading Pixmap"; QPixmap p("c:\\qt\\tile\\tile0-0.png"); temp-&gt;tileItem=new QGraphicsPixmapItem; temp-&gt;tileItem-&gt;setPixmap(p); qDebug()&lt;&lt;"Loaded Pixmap"; } void Tile::updateSceneSlot() { qDebug()&lt;&lt;"updateSceneSlot Thread id "&lt;&lt; QThread::currentThread(); tileItem-&gt;paint(TilePainter, TileOption, TileWidget); } </code></pre> <p>This code should work but it keeps crashing at runtime as soon as paint gets called. After adding breakpoints I narrowed the problem down to <code>temp-&gt;tileItem-&gt;paint(painter,option,widget);</code> which causes the crash.</p> <p>The output that I get is</p> <pre><code>Loading Pixmap Almost Loaded Pixmap First-chance exception at 0x6526174a (QtGuid4.dll) in Visualizer.exe: 0xC0000005: Access violation reading location 0xc88bffe1. Unhandled exception at 0x6526174a (QtGuid4.dll) in Visualizer.exe: 0xC0000005: Access violation reading location 0xc88bffe1. </code></pre> <p>Could anyone help me and let me know why the lastline/paint method crashes. How can I fix it?</p> <p>EDITED CODE TO UPDATE CHANGES</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload