[Qt-interest] Loading QPixmap from file with QtConcurrent causing crash

Cole, Derek dcole at integrity-apps.com
Mon Jun 13 19:01:40 CEST 2011


So, I am trying to rework a new class that does not use QPixmap. Can someone take a look at this code? This seems to crash after the first  thread enters the updateSceneSlot() function. I am using a QImage and the drawImage painter function

#include "tile.h"

Tile::Tile(QGraphicsItem *parent) : QGraphicsItem(parent)
{
}

Tile::~Tile()
{
}
Tile::Tile(QGraphicsItem *parent)
    : QGraphicsItem(parent)
{
    qDebug("Tile constructor called");
    /*Set a bunch of parameters related to reading the file from disk, image size, etc
    . 
    .
    .
    */
    this->image = NULL;

    this->future = new QFuture<void>;
    this->watcher = new QFutureWatcher<void>;
    connect(watcher,SIGNAL(finished()),this,SLOT(updateSceneSlot()));
}
QRectF Tile::boundingRect() const
{
    if(image == NULL)
        return(QRectF(0,0,0,0));

    return(QRectF(0,0,image->width(),image->height()));
}

void Tile::updateSceneSlot()
{
    qDebug("updateSceneSlot Thread id %i", QThread::currentThread());
    this->paint(TilePainter, TileOption, TileWidget);
}

void Tile::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget)
{
    if(image==NULL)
    {
        TilePainter=painter;
        TileOption=option;
        TileWidget=widget;
        qDebug()<<"Paint Thread id "<< QThread::currentThread();
        *future=QtConcurrent::run(this, &Tile::LoadTilePixmap);
        watcher->setFuture(*future);

    }
    QPointF *p = new QPointF(0.0,0.0);
    if(watcher->isFinished())
        painter->drawImage(*p, *image);
}

void Tile::LoadTilePixmap()
{
    qDebug("Loading Pixmap");
    /*Load file from disk, populate the floatData array.....*/

    image = new QImage(nXSize, nYSize, QImage::Format_RGB32);
        for (int i = 0 ; i < nYSize ; i++)
        {
             for (int j = 0 ; j < nXSize ; j++)
             {
                    qi->setPixel(j,i,qRgb((unsigned char)floatData[i*nXSize+j],(unsigned char)floatData[i*nXSize+j],(unsigned char)floatData[i*nXSize+j]));
             }
         }
}


________________________________________
From: qt-interest-bounces+dcole=integrity-apps.com at qt.nokia.com [qt-interest-bounces+dcole=integrity-apps.com at qt.nokia.com] on behalf of Thiago Macieira [thiago at kde.org]
Sent: Monday, June 13, 2011 11:57 AM
To: qt-interest at qt.nokia.com
Subject: Re: [Qt-interest] Loading QPixmap from file with       QtConcurrent    causing crash

Em Monday, 13 de June de 2011, às 11:38:12, Atlant Schmidt escreveu:
> Thiago:
>
>   Didn't you say it exactly the other way around? That
>   QPixmaps can *ONLY* be created and used in the GUI
>   thread? Perhaps the confusion arises from the use
>   of the phrase "main GUI thread"?

I did.

Sorry, I misread what Derek wrote. QPixmap CANNOT be used in a thread other
than the GUI thread (the main thread). It can ONLY be used in the GUI thread.

--
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
      PGP/GPG: 0x6EF45358; fingerprint:
      E067 918B B660 DBD1 105C  966C 33F5 F005 6EF4 5358



More information about the Qt-interest-old mailing list