QPixmap Class▲
-
Header: QPixmap
-
CMake:
find_package(Qt6 REQUIRED COMPONENTS Gui)
target_link_libraries(mytarget PRIVATE Qt6::Gui)
-
qmake: QT += gui
-
Inherits: QPaintDevice
-
Inherited By: QBitmap
-
Group: QPixmap is part of Painting Classes, shared
Detailed Description▲
Qt provides four classes for handling image data: QImage, QPixmap, QBitmap and QPicture. 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. QBitmap is only a convenience class that inherits QPixmap, ensuring a depth of 1. The isQBitmap() function returns true if a QPixmap object is really a bitmap, otherwise returns false. Finally, the QPicture class is a paint device that records and replays QPainter commands.
A QPixmap can easily be displayed on the screen using QLabel or one of QAbstractButton's subclasses (such as QPushButton and QToolButton). QLabel has a pixmap property, whereas QAbstractButton has an icon property.
QPixmap objects can be passed around by value since the QPixmap class uses implicit data sharing. For more information, see the Implicit Data Sharing documentation. QPixmap objects can also be streamed.
Note that the pixel data in a pixmap is internal and is managed by the underlying window system. Because QPixmap is a QPaintDevice subclass, QPainter can be used to draw directly onto pixmaps. Pixels can only be accessed through QPainter functions or by converting the QPixmap to a QImage. However, the fill() function is available for initializing the entire pixmap with a given color.
There are functions to convert between QImage and QPixmap. Typically, the QImage class is used to load an image file, optionally manipulating the image data, before the QImage object is converted into a QPixmap to be shown on screen. Alternatively, if no manipulation is desired, the image file can be loaded directly into a QPixmap.
QPixmap provides a collection of functions that can be used to obtain a variety of information about the pixmap. In addition, there are several functions that enables transformation of the pixmap.
Reading and Writing Image Files▲
QPixmap provides several ways of reading an image file: The file can be loaded when constructing the QPixmap object, or by using the load() or loadFromData() functions later on. When loading an image, the file name can either refer to an actual file on disk or to one of the application's embedded resources. See The Qt Resource System overview for details on how to embed images and other resource files in the application's executable.
Simply call the save() function to save a QPixmap object.
The complete list of supported file formats are available through the QImageReader::supportedImageFormats() and QImageWriter::supportedImageFormats() functions. New file formats can be added as plugins. By default, Qt supports the following formats:
Format |
Description |
Qt's support |
---|---|---|
BMP |
Windows Bitmap |
Read/write |
GIF |
Graphic Interchange Format (optional) |
Read |
JPG |
Joint Photographic Experts Group |
Read/write |
JPEG |
Joint Photographic Experts Group |
Read/write |
PNG |
Portable Network Graphics |
Read/write |
PBM |
Portable Bitmap |
Read |
PGM |
Portable Graymap |
Read |
PPM |
Portable Pixmap |
Read/write |
XBM |
X11 Bitmap |
Read/write |
XPM |
X11 Pixmap |
Read/write |
Pixmap Information▲
QPixmap provides a collection of functions that can be used to obtain a variety of information about the pixmap:
Available Functions |
|
---|---|
Geometry |
The size(), width() and height() functions provide information about the pixmap's size. The rect() function returns the image's enclosing rectangle. |
Alpha component |
The hasAlphaChannel() returns true if the pixmap has a format that respects the alpha channel, otherwise returns false. The hasAlpha(), setMask() and mask() functions are legacy and should not be used. They are potentially very slow. The createHeuristicMask() function creates and returns a 1-bpp heuristic mask (i.e. a QBitmap) for this pixmap. It works by selecting a color from one of the corners and then chipping away pixels of that color, starting at all the edges. The createMaskFromColor() function creates and returns a mask (i.e. a QBitmap) for the pixmap based on a given color. |
Low-level information |
The depth() function returns the depth of the pixmap. The defaultDepth() function returns the default depth, i.e. the depth used by the application on the given screen. The cacheKey() function returns a number that uniquely identifies the contents of the QPixmap object. |
Pixmap Conversion▲
A QPixmap object can be converted into a QImage using the toImage() function. Likewise, a QImage can be converted into a QPixmap using the fromImage(). If this is too expensive an operation, you can use QBitmap::fromImage() instead.
To convert a QPixmap to and from HICON you can use the QtWinExtras functions QtWin::toHICON() and QtWin::fromHICON() respectively.
Pixmap Transformations▲
QPixmap supports a number of functions for creating a new pixmap that is a transformed version of the original:
The scaled(), scaledToWidth() and scaledToHeight() functions return scaled copies of the pixmap, while the copy() function creates a QPixmap that is a plain copy of the original one.
The transformed() function returns a copy of the pixmap that is transformed with the given transformation matrix and transformation mode: Internally, the transformation matrix is adjusted to compensate for unwanted translation, i.e. transformed() returns the smallest pixmap containing all transformed points of the original pixmap. The static trueMatrix() function returns the actual matrix used for transforming the pixmap.
See Also▲
See also QBitmap, QImage, QImageReader, QImageWriter
Member Function Documentation▲
QPixmap::QPixmap()▲
QPixmap::QPixmap(int width, int height)▲
Constructs a pixmap with the given width and height. If either width or height is zero, a null pixmap is constructed.
This will create a QPixmap with uninitialized data. Call fill() to fill the pixmap with an appropriate color before drawing onto it with QPainter.
See Also▲
See also isNull()
[explicit] QPixmap::QPixmap(const QSize &size)▲
This is an overloaded function.
Constructs a pixmap of the given size.
QPixmap::QPixmap(const QString &fileName, const char *format = nullptr, Qt::ImageConversionFlags flags = Qt::AutoColor)▲
Constructs a pixmap from the file with the given fileName. If the file does not exist or is of an unknown format, the pixmap becomes a null pixmap.
The loader attempts to read the pixmap using the specified format. If the format is not specified (which is the default), the loader probes the file for a header to guess the file format.
The file name can either refer to an actual file on disk or to one of the application's embedded resources. See the Resource System overview for details on how to embed images and other resource files in the application's executable.
If the image needs to be modified to fit in a lower-resolution result (e.g. converting from 32-bit to 8-bit), use the flags to control the conversion.
The fileName, format and flags parameters are passed on to load(). This means that the data in fileName is not compiled into the binary. If fileName contains a relative path (e.g. the filename only) the relevant file must be found relative to the runtime working directory.
See Also▲
See also Reading and Writing Image Files
[explicit] QPixmap::QPixmap(const char *const[] xpm)▲
Constructs a pixmap from the given xpm data, which must be a valid XPM image.
Errors are silently ignored.
Note that it's possible to squeeze the XPM variable a little bit by using an unusual declaration:
static
const
char
*
const
start_xpm[] =
{
"16 15 8 1"
,
"a c #cec6bd"
,
// etc.
}
;
The extra const makes the entire definition read-only, which is slightly more efficient (for example, when the code is in a shared library) and ROMable when the application is to be stored in ROM.
QPixmap::QPixmap(const QPixmap &pixmap)▲
QPixmap::QPixmap(QPixmap &&other)▲
[virtual] QPixmap::~QPixmap()▲
Destroys the pixmap.
qint64 QPixmap::cacheKey() const▲
Returns a number that identifies this QPixmap. Distinct QPixmap objects can only have the same cache key if they refer to the same contents.
The cacheKey() will change when the pixmap is altered.
bool QPixmap::convertFromImage(const QImage &image, Qt::ImageConversionFlags flags = Qt::AutoColor)▲
Replaces this pixmap's data with the given image using the specified flags to control the conversion. The flags argument is a bitwise-OR of the Qt::ImageConversionFlags. Passing 0 for flags sets all the default options. Returns true if the result is that this pixmap is not null.
Note: this function was part of Qt 3 support in Qt 4.6 and earlier. It has been promoted to official API status in 4.7 to support updating the pixmap's image without creating a new QPixmap as fromImage() would.
See Also▲
See also fromImage()