Differences between qpixmap and qimage

Source: Internet
Author: User

(1) differences between QPixmap and QImage

Http://www.thisisqt.com/forum/viewthread.php? Tid = 267

QPixmap is generated specially for plotting. You need to use QPixmap to draw an image. QImage is I/O, designed for Pixel access and modification. If you want to access the image pixels or modify the image pixels, you need to use QImage or use QPainter to operate the pixels. In addition, unlike QImage, QPixmap is related to hardware, such as X11, Mac, and Symbian platforms. QPixmap is stored on the server, while QImage is stored on the client, on Windows, both QPixmap and QImage are stored on the client and do not use any GDI resources.

I believe everyone is more concerned about who is faster. Haha, let's summarize:
On X11, Mac, and Symbian platforms, QImage: because it is stored on the client, drawing to QImage is faster, but displaying it is slower. QPixmap: because it is stored on the server side, drawing to QPixmap is slow, but displaying it is faster. But it is the same on Windows platform, because they are all stored on the client.

QPixmap and QImage are the most widely used in Qt processing. However, since we talk about images, let's also talk about several other image processing classes:
QBitmap is just a simple class that inherits from QPixmap. It ensures that the image depth is 1.
QBitmap is a subclass of QPixmap. It provides a monochrome image and can be used to create a cursor (QCursor) or a brush (QBrush ).

QPicture is a painting device class that records and can repeat QPainter commands. You can use the begin () method of QPainter to specify the drawing on QPicture, use the end () method to end the drawing, and use the save () method of QPicture () method: Save the drawing commands used by QPainter to the file. To replay the Drawing Instruction, create a QPicture, load the Drawing Instruction file using the load () method, and then draw the QPicture on the specified drawing placement:

(2) Complete QImage and QPixmap Parsing

Http://www.civilnet.cn/bbs/browse.php? Topicno = 4691

Using the Qt program to display an image on a mobile phone is no more basic for programmers. Let's take a look at two sections of code:

// Dangerous shocould not be 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 pixmap;
Pixmap. load (":/pics/earth.png ");
Label-> setPixmap (pixmap );

// Dangerous shocould not be 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 pixmap;
Pixmap. load (":/pics/earth.png ");
QPainter painter (this );
Painter. drawPixmap (0, 0, pixmap );
Do you think there are any problems with these two codes? It seems that there is no problem. Yes, there is no problem in Windows. The problem is that we are doing Qt for Symbian! Resources on mobile phones are scarce, so we need to pay more attention when using them. Qt provides four image processing classes: QImage, QPixmap, QBitmap, and QPicture. The first two are the most commonly used.

This article uses an example to explain the use of QImage and QPixmap step by step. In this process, we will reveal the defects of the above Code.
QPixmap depends on hardware

First, we need to know that the specific implementation of QPixmap depends on the system. In the Symbian system, QPixmap is stored on the Server.
Currently, Qt stores QPixmap in graphics memory, which depends on hardware. Therefore, we need to pay special attention to the use of QPixmap. This is exactly the root cause of the above two code segments.
So why does Qt do this? At the beginning of the design, QPixmap is used to accelerate the display. For example, we use QPixmap for painting, which is much better than using other classes.

Now let's go back to our initial question. What is the problem with the above Code? We can first use the example program provided in this article for a test. When the preceding code is used to display smaller images (for example, background.png and apple.png in the Program), there is no problem. Pictures can be correctly displayed on the mobile phone.
However, when we change an image into a large image of 287KB, 1058 x 1058“earth.png, the image cannot be displayed, and the program interface is blank.

According to the test, when the earearth.png is completely decoded and stored in graphics memory, it occupies about MB of space. If there are other loaded windows and QPixmap at this time, there is probably no space.
Use QImage to load and convert to QPixmap display

So what are the security and correct methods? The answer is that we need to use QImage for preprocessing:

// Correct and recommended way
QImage image;
Image. load (":/pics/earth.png ");

QPainter painter (this );
QPixmap pixmapToShow = QPixmap: fromImage (image. scaled (size (), Qt: KeepAspectRatio ));
Painter. drawPixmap (0, 0, pixmapToShow );
Unlike QPixmap, QImage is independent of hardware and can be accessed by another thread at the same time. QImage is stored on the client, which is convenient and secure to use QImage. Since QImage is also a QPaintDevice, we can plot it in another thread without processing it in the GUI thread. This method can greatly improve the UI response speed. Therefore, when the image size is large, we can first load the image through QImage, then scale the image to the desired size, and finally convert it to QPixmap for display. Is the display effect (the image is scaled according to the original size ratio of earth.png ):


Note the usage of Qt: KeepAspectRatio. The default parameter is Qt: IgnoreAspectRatio. If we write this in the program:

QPixmap pixmapToShow = QPixmap: fromImage (image. scaled (size (), Qt: IgnoreAspectRatio ));
In this example, earth.png is stretched to fill the screen:


Display with QImage

We can also directly use QImage for display, instead of converting to QPixmap. This is determined based on the specific needs of our application. If necessary, we can write as follows:

// Correct, some times may be needed
QImage image;
Image. load (":/pics/earth.png ");

QPainter painter (this );
Painter. drawImage (0, 0, image );
The following is the display effect (of course we can also scale it and then display it) from the image, we can see that the original size shows earth.png:


Test Device

This code has passed testing on N97 and N8.

This article from the CSDN blog, reproduced please indicate the source: http://blog.csdn.net/nolatestudy/archive/2011/04/01/6295064.aspx

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.