PDA

View Full Version : Loading an image into memory



Luc4
9th April 2010, 08:16
Hi! I noticed this situation a couple of times: I have a method which does something and then loads an image into memory, something like:


//do something like a QGraphicsView::show();
QPixmap pixmap("/path/to/file");

I get that the instructions before loading the image show results only after the pixmap is loaded. For instance, the QGraphicsView is shown only after the image is loaded (some seconds in slow devices and very large images). Is it possible somehow to change this behavior? I would prefer avoiding having to use threads if possible.
Thanks for any information!

wysota
9th April 2010, 08:38
You may only defer loading the pixmap to after you are sure the widget will already be visible. The latter happens when show() is called and processing returns to the event loop where appropriate events for showing the widget are processed. You can schedule your own event (or make a deferred slot call via QMetaObject::invokeMethod()) to load the image asynchronously. Of course you'll have to move all your code that uses the pixmap from the original function to somewhere after the pixmap is actually loaded.

Luc4
9th April 2010, 11:03
Thanks for the information! And is it possible to terminate the loading of an image? I noticed it is not possible to terminate a thread while it is loading an image (this time I'm using the read method of QImageReader). Even if I call the terminate, the image is loaded completely anyway.

wysota
9th April 2010, 21:10
No, you can't interrupt the call as it's synchronous and performed mostly by 3rd party libraries that tend to behave differently.

Luc4
10th April 2010, 13:45
I'm trying to load the image from a separate thread with low priority. In this case, can I call the terminate method of the thread to interrupt the loading?
Thanks again for your help!

wysota
12th April 2010, 08:03
You can call it but it's unlikely it will actually terminate it inside the call :)

Luc4
12th April 2010, 08:44
OK, so no solution... :-( If I try to read an image which takes 8 seconds to load, I have to load it all, no matter what I do... Thanks anyway for your help!