102

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?

4
  • I'm not sure if I understand your question, but I thought it was pretty clear in the documentation: "QImage is designed and optimized for I/O, and for direct pixel access and manipulation, while QPixmap is designed and optimized for showing images on screen." doc.qt.nokia.com/latest/qpixmap.html#details
    – cgmb
    Apr 25, 2012 at 0:45
  • 1
    yeah! i had find it out,but not understand well, for example, optimized for I/O and optimized for showing, it is difference a picutre showed on difference platform ? ..Can you help me make a step to explain.. thank you..
    – Mr.Tu
    Apr 25, 2012 at 0:50
  • I'm still not 100% sure I know what you mean, but if you're using QWidgets, you can display it in a QLabel. If you're using QGraphicsView, you can display it in a QGraphicsPixmapItem. If you're using QML the Image element will handle everything for you.
    – cgmb
    Apr 25, 2012 at 1:17
  • Let me put it another way, what is the Engine, what is its work? when to use it?
    – Mr.Tu
    Apr 25, 2012 at 1:29

5 Answers 5

66

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.
4
  • thanks for your help... what is general process that QPixmpa and QImage load a picture.. what is it in memory.. thank you..
    – Mr.Tu
    Apr 26, 2012 at 2:46
  • I'm not sure what you are trying to do, but in this question I demonstrate how to load YV12 data from the disk, convert to RGB using a GLSL fragment shader, and then display it on the screen inside a QImage. I guess I could point you to right direction if you were more clear about what you are trying to accomplish. Apr 26, 2012 at 3:13
  • 24
    Does this really answer anything?
    – spinkus
    Jun 27, 2014 at 3:43
  • 10
    Just copying documentation and others' answers doesn't make sense. Mar 16, 2018 at 16:31
55

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.
3
  • thank you. This passage tell us: there are two different way to load a picture, raster and OpenGL ? is right?
    – Mr.Tu
    Apr 26, 2012 at 2:50
  • I don't quite understand the question. Neither "raster" nor "OpenGL" specifically represent ways to load a picture. Apr 26, 2012 at 3:16
  • Does QImage's "manipulating and modifying" includes it being saves as an image file? If I start out with a QImage object, which one is faster when saving it as an image file?
    – reddy
    Oct 21, 2021 at 5:33
38

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.

24
  • 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 --
24

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).

2
  • "With a Pixmap, a Redraw consists in sending only the order to redraw (a few bytes) over the network.".. That is misleading. The video-memory is limited too, so what if it does not have the data? Jan 20, 2021 at 21:24
  • 1
    @Nawaz: Of course, the image has to be sent at least once, over the network, and the memory is limited. But as long as it is in video memory (as long as it is displayed, I imagine), the redraw orders will be executed without re-sending it. When something is drawn in front of the image, for example. We observed that on our network data flow. In 2012 !... :P
    – iksess
    Jan 21, 2021 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.