问题
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?
回答1:
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.
回答2:
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:
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.
回答4:
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).
回答5:
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 --
来源:https://stackoverflow.com/questions/10307860/what-is-the-difference-between-qimage-and-qpixmap