[Qt-interest] Loading QPixmap from file with QtConcurrent causing crash
Gopalakrishna Bhat
gopalakbhat at gmail.com
Mon Jun 13 17:35:25 CEST 2011
On Mon, Jun 13, 2011 at 8:52 PM, Cole, Derek <dcole at integrity-apps.com>wrote:
> I think i read somewhere that you can only use QPixmap in a thread other
> than the main GUI thread. is this correct?
>
>
Actually it is the other way round. You can use QPixmap only in the GUI
thread.
> 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.
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at qt.nokia.com
> http://lists.qt.nokia.com/mailman/listinfo/qt-interest
>
>
--
My blog http://gkbhat.blogspot.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110613/4cc63d7c/attachment.html
More information about the Qt-interest-old
mailing list