[Qt-interest] How can I read a large image fastly?

Dan Milburn danmilburn at clara.co.uk
Thu Feb 4 16:08:19 CET 2010


Kermit Mei wrote:
> Hello community!
> 
>     I run Qt on a slow embedded device, now we want to show some large 
> image on it, but the speed of loading is too slowly. For example, if we 
> decode a 6M jpg picture, we always need 100s to show it.
> 
>    Does Qt4 can support that  read  parts of  the  image to make it 
> shows quickly. For example, the picture is 4000x4000,  in fact we just 
> need to show 320x240. Can I just show a part of the picture to make it 
> faster?
> 
> I use QImageReader like this:
> 
> void Browser::paintEvent(QPaintEvent *event) {
>    imgReader->setFileName("./test.jpg");
>    imgReader->setClipRect(QRect(0,0,320,240));
>    imgReader->setScaledSize(QSize(3200,2400));
>    qDebug("canRead: %d",imgReader->canRead());
>    QPainter p(this);
>    p.drawImage(QRect(0,0,320,240),imgReader->read());
> }*
> **
> *But it shows all the picture which is reduced to 320x240, so the speed 
> is also slow.
> 
> Is there something wrong of the code?

Ok, I am not familiar with QImageReader, but a quick look at the docs 
suggests this is what your code is doing:

- Using setClipRect() to only read part of the image.
- Scaling that part of the image up to 3200x2400.
- Drawing the image scaled back down to 320x240.

It's hardly surprising if this works slowly, even without the fact that 
as others have told you, paintEvent() is the wrong place to be doing this.

It's unclear what you're actually trying to achieve.  If you want to 
draw the entire image scaled down there is no way to avoid loading the 
full image.  You should remove the setClipRect() call and
setScaledSize( 320, 240 ).  This is unlikely to be any faster than using 
QImage::scale() directly.

There really isn't a way to load large images any faster.  You can make 
it *seem* faster if you can load them ahead of time, possibly in another 
thread.

See for example 
http://labs.trolltech.com/blogs/2010/01/21/qt-graphics-and-performance-generating-content-in-threads/

Dan



More information about the Qt-interest-old mailing list