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

Bo Thorsen bo at fioniasoftware.dk
Tue Jun 14 08:19:28 CEST 2011


Den 13-06-2011 17:22, Cole, Derek skrev:
> 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.

I know this thread has moved on, but I'll still answer your original 
question. Maybe it doesn't help this time, but it could be helpful in 
another occasion.

One of my standard tricks is to avoid threading by putting things in a 
queue and let the event loop do the "scheduling". The trick is to do 
something like this:

// somewhere: Start the code
QTimer::singleShot(0, this, SLOT(doNextBits()));

void doNextBits() {
     // Read some of your pixmaps here

     if (!done)
         QTimer::singleShot(0, this, SLOT(doNextBits()));
}

How many of your pixmaps can be loaded on each iteration is something 
you have to test.

The observant reader will notice that this is actually the same idea as 
QtConcurrent, just implemented using the event loop instead of with threads.

Unfortunately this doesn't work well on something like phones, but on 
desktop it's perfect for a lot of problems.

Bo Thorsen,
Fionia Software.

-- 

Expert Qt and C++ developer for hire
Contact me if you need expert Qt help
http://www.fioniasoftware.dk



More information about the Qt-interest-old mailing list