What is the difference between QImage and QPixmap?

C++QtQimageQpixmap

C++ Problem Overview


I do not understand what is the difference between QImage and QPixmap, they seem to offer the same functionality. When should I use a QImage and when should I use a QPixmap?

C++ Solutions


Solution 1 - C++

Easilly answered by reading the docs on QImage and QPixmap:

> The QPixmap class is an off-screen image representation that can be used as a paint device. > > The QImage class provides a hardware-independent image representation that allows direct access to the pixel data, and can be used as a paint device.

Edit: Also, from @Dave's answer:

> You can't manipulate a QPixmap outside the GUI-thread, but QImage has no such restriction.

And from @Arnold:

> Here's a short summary that usually (not always) applies: > > * If you plan to manipulate an image, modify it, change pixels on it, > etc., use a QImage. > * If you plan to draw the same image more than once > on the screen, convert it to a QPixmap.

Solution 2 - C++

There is a nice series of articles at Qt Labs that explains a lot about the Qt graphics system. This article in particular has a section on QImage vs. QPixmap.

Here's a short summary that usually (not always) applies:

  • If you plan to manipulate an image, modify it, change pixels on it, etc., use a QImage.
  • If you plan to draw the same image more than once on the screen, convert it to a QPixmap.

Solution 3 - C++

One important difference is that you cannot create or manipulate a QPixmap on anything but the main GUI thread. You can, however, create and manipulate QImage instances on background threads and then convert them after passing them back to the GUI thread.

Solution 4 - C++

Important in industrial environments:

The QPixmap is stored on the video card doing the display. Not the QImage.

So if you have a server running the application, and a client station doing the display, it is very significant in term of network usage.

With a Pixmap, a Redraw consists in sending only the order to redraw (a few bytes) over the network.

With a QImage, it consists in sending the whole image (around a few MB).

Solution 5 - C++

  • QPixmap is an "image object" whose pixel representation are of no consequence in your code, Thus QPixmap is designed and optimized for rendering images on display screen, it is stored on the XServer when using X11, thus drawing QPixmap on XWindow is much faster than drawing QImages, as the data is already on the server, and ready to use.

> When to use QPixmap: If you just want to draw an existing image (icon .. background .. etc) especially repeatedly, then use QPixmap.

  • QImage is an "array of pixels in memory" of the client code, QImage is designed and optimized for I/O, and for direct pixel access and manipulation.

    > When to use QImage: If you want to draw, with Qpaint, or manipulate an image pixels.

  • QBitmap is only a convenient QPixmap subclass ensuring a depth of 1, its a monochrome (1-bit depth) pixmap. Just like QPixmap , QBitmap is optimized for use of implicit data sharing.

  • QPicture is a paint device that records and replays QPainter commands -- your drawing --

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMr.TuView Question on Stackoverflow
Solution 1 - C++karlphillipView Answer on Stackoverflow
Solution 2 - C++Arnold SpenceView Answer on Stackoverflow
Solution 3 - C++Dave MateerView Answer on Stackoverflow
Solution 4 - C++iksessView Answer on Stackoverflow
Solution 5 - C++Mohammad KananView Answer on Stackoverflow