How to correctly display pictures on the phone--qimage and Qpixmap fully resolved

Source: Internet
Author: User

Introduction

Using the QT program to display a picture on the phone is a fundamental thing for programmers. So let's first look at two pieces of code:

Dangerous should not being used, cannot display earth.png,  qpixmap pixmap; pixmap.  Load); label--setpixmap);       

And

//dangerous should not being used, cannot display earth.png, //but If we change earth.png to a smaller image e.g. Apple.png, apple.png can be displayed qpixmap< /span> pixmap Loadqpainter  painter (thisdrawpixmap (0,0         

What do you think is wrong with these two pieces of code?
Looks like there's no problem. Yes, there is no problem on the Windows operating system. The problem is, we're doing QT for symbian!. The resources on the phone are inherently scarce, so we need to pay more attention when we use them. Qt provides us with four classes for processing images: Qimage,qpixmap,qbitmap and qpicture. The first two are the most commonly used.


This article through an example, step by step for you to explain the use of qimage and Qpixmap mysteries, in the process for everyone to reveal the above code defects. The example program used in this article can be downloaded using the link attached to the end of this article.

Qpixmap relies on hardware

The first thing to know is that the specific implementation of QPIXMAP is dependent on the system. On Symbian Systems Qpixmap is stored on the server side.

The current QT will store the Qpixmap in the graphics memory, which is obviously dependent on the hardware. So we need to pay extra attention to the use of qpixmap. This is also the root of the problem of the above two pieces of code.
So why would QT do this? Very simple, the design of the first qpixmap is used to speed up the display, for example, when we paint the use of qpixmap will be better than other classes of the effect of much better.

Now back to our original question, what's wrong with the above code? We can first use the example procedure provided in this article to do an experiment. When using the above code to display smaller images (such as background.png and Apple.png in the example program), there is no problem, the images can be displayed correctly on the phone.
But when we change the picture to a larger picture 287kb,1058 x 1058 "Earth.png" when there is a problem, the picture can not be displayed, the interface of the program is a blank.

It is estimated that "earth.png" is completely decoded and stored in the graphics memory will occupy about 4.3MB of space. If there are other loaded windows and Qpixmap at this point, there is likely to be no space.

convert to Qpixmap display after loading with qimage

So what should be the safe and the right way? The answer is we need to do a pre-treatment with Qimage:

Correct and recommendedQimageImage; Image.Load(":/pics/earth.png" qpainter  painter (thisqpixmap  pixmaptoshow = qpixmap  ::fromimage (Image. Scaled (qt  ::keepaspectratio)   ; Painter. drawpixmap (0,0         

Unlike Qpixmap, Qimage is hardware-independent and can be accessed by another thread at the same time. Qimage is stored on the client, and the use of qimage is very convenient and secure. And since Qimage is also a kind of qpaintdevice, we can draw it in another thread without having to do it in a GUI thread, which can greatly improve the responsiveness of the UI.
So when the picture is large, we can first load the picture through qimage, then zoom the image into the desired size, and then convert it to qpixmap for display. is the display effect (the picture is displayed according to the Earth.png's original size scale):

It should be noted that the use of Qt::keepaspectratio, the default parameter is Qt::ignoreaspectratio, if we write in the program:

Qpixmap  qpixmap::fromimage(image.  Scaled(Size()Qt::ignoreaspectratio);      

The effect is as follows, and the earth.png is stretched to fill the screen:

Direct use of qimage display

We can also directly use the Qimage to do the display, and not convert to Qpixmap, which depends on the specific needs of our application to decide, if necessary, we can write:

qimage image; image.  Load)qpainter painter(this); painter.  DrawImage(0,0, image);          

Here's how the display works (and of course we can zoom in and then show it)
As you can see from the picture, it is shown in the original size earth.png:

Test Equipment

This code has passed the test on N97 and N8.


This article was reproduced from: http://developer.nokia.com/community/wiki/%E5%A6%82%E4%BD%95%E6%AD%A3%E7%A1%AE%E5%9C%B0%E5%9C%A8%E6%89% 8b%e6%9c%ba%e4%b8%8a%e6%98%be%e7%a4%ba%e5%9b%be%e7%89%87%e2%80%94%e2%80%94qimage%e4%b8%8eqpixmap%e5%ae%8c%e5% 85%a8%e8%a7%a3%e6%9e%90


In addition, for QT mapping, you can also use this article: http://www.360doc.com/content/12/0717/14/1291795_224728691.shtml

http://blog.csdn.net/jan5_reyn/article/details/38959519

How to correctly display pictures on the phone--qimage and Qpixmap fully resolved

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.