QtBase
v6.3.1
|
The QPixmap class is an off-screen image representation that can be used as a paint device. More...
#include <qpixmap.h>
Public Types | |
typedef QExplicitlySharedDataPointer< QPlatformPixmap > | DataPtr |
Public Types inherited from QPaintDevice | |
enum | PaintDeviceMetric { PdmWidth = 1 , PdmHeight , PdmWidthMM , PdmHeightMM , PdmNumColors , PdmDepth , PdmDpiX , PdmDpiY , PdmPhysicalDpiX , PdmPhysicalDpiY , PdmDevicePixelRatio , PdmDevicePixelRatioScaled } |
Static Public Member Functions | |
static int | defaultDepth () |
static QTransform | trueMatrix (const QTransform &m, int w, int h) |
static QPixmap | fromImage (const QImage &image, Qt::ImageConversionFlags flags=Qt::AutoColor) |
static QPixmap | fromImageReader (QImageReader *imageReader, Qt::ImageConversionFlags flags=Qt::AutoColor) |
static QPixmap | fromImage (QImage &&image, Qt::ImageConversionFlags flags=Qt::AutoColor) |
Static Public Member Functions inherited from QPaintDevice | |
static qreal | devicePixelRatioFScale () |
Protected Member Functions | |
int | metric (PaintDeviceMetric) const override |
Protected Member Functions inherited from QPaintDevice | |
QPaintDevice () noexcept | |
virtual void | initPainter (QPainter *painter) const |
virtual QPaintDevice * | redirected (QPoint *offset) const |
virtual QPainter * | sharedPainter () const |
Static Protected Member Functions | |
static QPixmap | fromImageInPlace (QImage &image, Qt::ImageConversionFlags flags=Qt::AutoColor) |
Friends | |
class | QPlatformPixmap |
class | QBitmap |
class | QPaintDevice |
class | QPainter |
class | QOpenGLWidget |
class | QWidgetPrivate |
class | QRasterBuffer |
Q_GUI_EXPORT QDataStream & | operator>> (QDataStream &, QPixmap &) |
Related Functions | |
(Note that these are not member functions.) | |
QDataStream & | operator<< (QDataStream &stream, const QPixmap &pixmap) |
QDataStream & | operator>> (QDataStream &stream, QPixmap &pixmap) |
Additional Inherited Members | |
Protected Attributes inherited from QPaintDevice | |
ushort | painters |
The QPixmap class is an off-screen image representation that can be used as a paint device.
\inmodule QtGui
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 \l {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.
QPixmap::QPixmap | ( | ) |
Constructs a null pixmap.
Definition at line 109 of file qpixmap.cpp.
|
explicit |
Definition at line 164 of file qpixmap.cpp.
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.
Definition at line 130 of file qpixmap.cpp.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Constructs a pixmap of the given size.
Definition at line 145 of file qpixmap.cpp.
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 \l{resources.html}{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.
Definition at line 198 of file qpixmap.cpp.
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:
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.
Definition at line 252 of file qpixmap.cpp.
Constructs a pixmap that is a copy of the given pixmap.
Definition at line 214 of file qpixmap.cpp.
|
inlinenoexcept |
QPixmap::~QPixmap | ( | ) |
Destroys the pixmap.
Definition at line 274 of file qpixmap.cpp.
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.
Definition at line 917 of file qpixmap.cpp.
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 \l{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.
Definition at line 1009 of file qpixmap.cpp.
Returns a deep copy of the subset of the pixmap that is specified by the given rectangle. For more information on deep copies, see the \l {Implicit Data Sharing} documentation.
If the given rectangle is empty, the whole image is copied.
Definition at line 307 of file qpixmap.cpp.
|
inline |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Returns a deep copy of the subset of the pixmap that is specified by the rectangle QRect( x, y, width, height).
Definition at line 190 of file qpixmap.h.
Creates and returns a heuristic mask for this pixmap.
The function works by selecting a color from one of the corners and then chipping away pixels of that color, starting at all the edges. If clipTight is true (the default) the mask is just large enough to cover the pixels; otherwise, the mask is larger than the data pixels.
The mask may not be perfect but it should be reasonable, so you can do things such as the following:
This function is slow because it involves converting to/from a QImage, and non-trivial computations.
Definition at line 686 of file qpixmap.cpp.
QBitmap QPixmap::createMaskFromColor | ( | const QColor & | maskColor, |
Qt::MaskMode | mode = Qt::MaskInColor |
||
) | const |
Creates and returns a mask for this pixmap based on the given maskColor. If the mode is Qt::MaskInColor, all pixels matching the maskColor will be transparent. If mode is Qt::MaskOutColor, all pixels matching the maskColor will be opaque.
This function is slow because it involves converting to/from a QImage.
Definition at line 704 of file qpixmap.cpp.
|
inline |
|
static |
Returns the default pixmap depth used by the application.
On all platforms the depth of the primary screen will be returned.
Definition at line 1410 of file qpixmap.cpp.
int QPixmap::depth | ( | ) | const |
Returns the depth of the pixmap.
The pixmap depth is also called bits per pixel (bpp) or bit planes of a pixmap. A null pixmap has depth 0.
Definition at line 556 of file qpixmap.cpp.
void QPixmap::detach | ( | ) |
Detaches the pixmap from shared pixmap data.
A pixmap is automatically detached by Qt whenever its contents are about to change. This is done in almost all QPixmap member functions that modify the pixmap (fill(), fromImage(), load(), etc.), and in QPainter::begin() on a pixmap.
There are two exceptions in which detach() must be called explicitly, that is when calling the handle() or the x11PictureHandle() function (only available on X11). Otherwise, any modifications done using system calls, will be performed on the shared data.
The detach() function returns immediately if there is just a single reference or if the pixmap has not been initialized yet.
Definition at line 1436 of file qpixmap.cpp.
QSizeF QPixmap::deviceIndependentSize | ( | ) | const |
Returns the size of the pixmap in device independent pixels.
This value should be used when using the pixmap size in user interface size calculations.
The return value is equivalent to pixmap.size() / pixmap.devicePixelRatio(),
Definition at line 659 of file qpixmap.cpp.
qreal QPixmap::devicePixelRatio | ( | ) | const |
Returns the device pixel ratio for the pixmap. This is the ratio between {device pixels} and {device independent pixels}.
Use this function when calculating layout geometry based on the pixmap size: QSize layoutSize = image.size() / image.devicePixelRatio()
The default value is 1.0.
Definition at line 611 of file qpixmap.cpp.
|
overridevirtual |
Reimplemented from QPaintDevice.
Definition at line 282 of file qpixmap.cpp.
Fills the pixmap with the given color.
The effect of this function is undefined when the pixmap is being painted on.
Definition at line 883 of file qpixmap.cpp.
|
static |
Converts the given image to a pixmap using the specified flags to control the conversion. The flags argument is a bitwise-OR of the \l{Qt::ImageConversionFlags}. Passing 0 for flags sets all the default options.
In case of monochrome and 8-bit images, the image is first converted to a 32-bit pixmap and then filled with the colors in the color table. If this is too expensive an operation, you can use QBitmap::fromImage() instead.
Definition at line 1474 of file qpixmap.cpp.
|
inlinestatic |
|
staticprotected |
|
static |
Create a QPixmap from an image read directly from an imageReader. The flags argument is a bitwise-OR of the \l{Qt::ImageConversionFlags}. Passing 0 for flags sets all the default options.
On some systems, reading an image directly to QPixmap can use less memory than reading a QImage to convert it to QPixmap.
Definition at line 1528 of file qpixmap.cpp.
QPlatformPixmap * QPixmap::handle | ( | ) | const |
bool QPixmap::hasAlpha | ( | ) | const |
Returns true
if this pixmap has an alpha channel, or has a mask, otherwise returns false
.
Definition at line 1353 of file qpixmap.cpp.
bool QPixmap::hasAlphaChannel | ( | ) | const |
Returns true
if the pixmap has a format that respects the alpha channel, otherwise returns false
.
Definition at line 1364 of file qpixmap.cpp.
int QPixmap::height | ( | ) | const |
Returns the height of the pixmap.
Definition at line 515 of file qpixmap.cpp.
bool QPixmap::isDetached | ( | ) | const |
Definition at line 990 of file qpixmap.cpp.
bool QPixmap::isNull | ( | ) | const |
Returns true
if this is a null pixmap; otherwise returns false
.
A null pixmap has zero width, zero height and no contents. You cannot draw in a null pixmap.
Definition at line 491 of file qpixmap.cpp.
bool QPixmap::isQBitmap | ( | ) | const |
Returns true
if this is a QBitmap; otherwise returns false
.
Definition at line 478 of file qpixmap.cpp.
bool QPixmap::load | ( | const QString & | fileName, |
const char * | format = nullptr , |
||
Qt::ImageConversionFlags | flags = Qt::AutoColor |
||
) |
Loads a pixmap from the file with the given fileName. Returns true if the pixmap was successfully loaded; otherwise invalidates the pixmap and returns false
.
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 \l{resources.html}{Resource System} overview for details on how to embed pixmaps and other resource files in the application's executable.
If the data 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.
Note that QPixmaps are automatically added to the QPixmapCache when loaded from a file in main thread; the key used is internal and cannot be acquired.
Definition at line 737 of file qpixmap.cpp.
|
inline |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Loads a pixmap from the binary data using the specified format and conversion flags.
Definition at line 200 of file qpixmap.h.
bool QPixmap::loadFromData | ( | const uchar * | data, |
uint | len, | ||
const char * | format = nullptr , |
||
Qt::ImageConversionFlags | flags = Qt::AutoColor |
||
) |
Loads a pixmap from the len first bytes of the given binary data. Returns true
if the pixmap was loaded successfully; otherwise invalidates the pixmap and returns false
.
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.
If the data 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.
Definition at line 794 of file qpixmap.cpp.
QBitmap QPixmap::mask | ( | ) | const |
Extracts a bitmap mask from the pixmap's alpha channel.
Definition at line 1395 of file qpixmap.cpp.
|
overrideprotectedvirtual |
Reimplemented from QPaintDevice.
Definition at line 1372 of file qpixmap.cpp.
QPixmap::operator QVariant | ( | ) | const |
Returns the pixmap as a QVariant.
Definition at line 414 of file qpixmap.cpp.
|
inline |
|
delete |
Assigns the given pixmap to this pixmap and returns a reference to this pixmap.
Move-assigns other to this QPixmap instance.
Definition at line 381 of file qpixmap.cpp.
|
overridevirtual |
QRect QPixmap::rect | ( | ) | const |
Returns the pixmap's enclosing rectangle.
Definition at line 540 of file qpixmap.cpp.
bool QPixmap::save | ( | const QString & | fileName, |
const char * | format = nullptr , |
||
int | quality = -1 |
||
) | const |
Saves the pixmap to the file with the given fileName using the specified image file format and quality factor. Returns true
if successful; otherwise returns false
.
The quality factor must be in the range [0,100] or -1. Specify 0 to obtain small compressed files, 100 for large uncompressed files, and -1 to use the default settings.
If format is \nullptr, an image format will be chosen from fileName's suffix.
Definition at line 836 of file qpixmap.cpp.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
This function writes a QPixmap to the given device using the specified image file format and quality factor. This can be used, for example, to save a pixmap directly into a QByteArray:
Definition at line 854 of file qpixmap.cpp.
QPixmap QPixmap::scaled | ( | const QSize & | s, |
Qt::AspectRatioMode | aspectMode = Qt::IgnoreAspectRatio , |
||
Qt::TransformationMode | mode = Qt::FastTransformation |
||
) | const |
|
inline |
QPixmap QPixmap::scaledToHeight | ( | int | height, |
Qt::TransformationMode | mode = Qt::FastTransformation |
||
) | const |
Returns a scaled copy of the image. The returned image is scaled to the given height using the specified transformation mode. The width of the pixmap is automatically calculated so that the aspect ratio of the pixmap is preserved.
If height is 0 or negative, a null pixmap is returned.
Definition at line 1135 of file qpixmap.cpp.
QPixmap QPixmap::scaledToWidth | ( | int | w, |
Qt::TransformationMode | mode = Qt::FastTransformation |
||
) | const |
Returns a scaled copy of the image. The returned image is scaled to the given width using the specified transformation mode. The height of the pixmap is automatically calculated so that the aspect ratio of the pixmap is preserved.
If width is 0 or negative, a null pixmap is returned.
Definition at line 1105 of file qpixmap.cpp.
Scrolls the area rect of this pixmap by (dx, dy). The exposed region is left unchanged. You can optionally pass a pointer to an empty QRegion to get the region that is exposed by the scroll operation.
You cannot scroll while there is an active painter on the pixmap.
Definition at line 344 of file qpixmap.cpp.
|
inline |
This convenience function is equivalent to calling QPixmap::scroll(dx, dy, QRect(x, y, width, height), exposed).
Definition at line 195 of file qpixmap.h.
Sets the device pixel ratio for the pixmap. This is the ratio between image pixels and device-independent pixels.
The default scaleFactor is 1.0. Setting it to something else has two effects:
QPainters that are opened on the pixmap will be scaled. For example, painting on a 200x200 image if with a ratio of 2.0 will result in effective (device-independent) painting bounds of 100x100.
Code paths in Qt that calculate layout geometry based on the pixmap size will take the ratio into account: QSize layoutSize = pixmap.size() / pixmap.devicePixelRatio() The net effect of this is that the pixmap is displayed as high-DPI pixmap rather than a large pixmap (see \l{Drawing High Resolution Versions of Pixmaps and Images}).
Definition at line 639 of file qpixmap.cpp.
Sets a mask bitmap.
This function merges the mask with the pixmap's alpha channel. A pixel value of 1 on the mask means the pixmap's pixel is unchanged; a value of 0 means the pixel is transparent. The mask must have the same size as this pixmap.
Setting a null mask resets the mask, leaving the previously transparent pixels black. The effect of this function is undefined when the pixmap is being painted on.
Definition at line 578 of file qpixmap.cpp.
QSize QPixmap::size | ( | ) | const |
Returns the size of the pixmap.
Definition at line 528 of file qpixmap.cpp.
QImage QPixmap::toImage | ( | ) | const |
Converts the pixmap to a QImage. Returns a null image if the conversion fails.
If the pixmap has 1-bit depth, the returned image will also be 1 bit deep. Images with more bits will be returned in a format closely represents the underlying system. Usually this will be QImage::Format_ARGB32_Premultiplied for pixmaps with an alpha and QImage::Format_RGB32 or QImage::Format_RGB16 for pixmaps without alpha.
Note that for the moment, alpha masks on monochrome images are ignored.
Definition at line 443 of file qpixmap.cpp.
QPixmap QPixmap::transformed | ( | const QTransform & | transform, |
Qt::TransformationMode | mode = Qt::FastTransformation |
||
) | const |
Returns a copy of the pixmap that is transformed using the given transformation transform and transformation mode. The original pixmap is not changed.
The transformation transform is internally adjusted to compensate for unwanted translation; i.e. the pixmap produced is the smallest pixmap that contains all the transformed points of the original pixmap. Use the trueMatrix() function to retrieve the actual matrix used for transforming the pixmap.
This function is slow because it involves transformation to a QImage, non-trivial computations and a transformation back to a QPixmap.
Definition at line 1169 of file qpixmap.cpp.
|
static |
Returns the actual matrix used for transforming a pixmap with the given width, height and matrix.
When transforming a pixmap using the transformed() function, the transformation matrix is internally adjusted to compensate for unwanted translation, i.e. transformed() returns the smallest pixmap containing all transformed points of the original pixmap. This function returns the modified matrix, which maps points correctly from the original pixmap into the new pixmap.
Definition at line 467 of file qpixmap.cpp.
int QPixmap::width | ( | ) | const |
Returns the width of the pixmap.
Definition at line 503 of file qpixmap.cpp.
|
related |
Writes the given pixmap to the given stream as a PNG image. Note that writing the stream to a file will not produce a valid image file.
Definition at line 956 of file qpixmap.cpp.
|
friend |
Reads an image from the given stream into the given pixmap.
Definition at line 969 of file qpixmap.cpp.
|
related |
Reads an image from the given stream into the given pixmap.
Definition at line 969 of file qpixmap.cpp.
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |