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

Cole, Derek dcole at integrity-apps.com
Mon Jun 13 17:22:25 CEST 2011


I think i read somewhere that you can only use QPixmap in a thread other than the main GUI thread. is this correct?

I have some code that is trying to create a new QGraphicsPixmapItem from a file. I have to do this for many items, all coming from the same file. I am trying to make this work threaded, so the GUI doesnt freeze while all these tile items are loading.

Here are some relevant code snippets:

ImagePixmapItem::ImagePixmapItem(const QPixmap &pixmap, QGraphicsItem *parentItem)
    : QGraphicsPixmapItem(pixmap, parentItem)
{

    this->tileItem = NULL;
    this->future = new QFuture<void>;
    this->watcher = new QFutureWatcher<void>;
    connect(watcher,SIGNAL(finished()),this,SLOT(updateSceneSlot()));

}

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



void ImagePixmapItem::LoadTilePixmap(ImagePixmapItem *temp)
{
    qDebug("Loading Pixmap");
    /*...code to generate a QImage named qi, reading from a file */

    QPixmap p(QPixmap::fromImage(*qi,Qt::AutoColor));

    temp->tileItem = new QGraphicsPixmapItem;
    temp->tileItem->setPixmap(p);

    temp->update(0,0,tileItem->boundingRect().width(), tileItem->boundingRect().height());
}

void ImagePixmapItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget)
{

    if(tileItem==NULL)
    {
        TilePainter=painter;
        TileOption=option;
        TileWidget=widget;
        qDebug()<<"Paint Thread id "<< QThread::currentThread();

        *future=QtConcurrent::run(this, &ImagePixmapItem::LoadTilePixmap, this);
        watcher->setFuture(*future);

    }
    tileItem->paint(painter,option,widget);
}

When I run this, I know that  paint is getting called, because that Paint Thread Id prints out, however, "Loading Pixmap" does not print out.

When I run this in debug mode, it stops in some assembler code, and I cant tell exactly where the problem is.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110613/2ce0ab49/attachment.html 


More information about the Qt-interest-old mailing list