QImage 类

QImage 类提供独立于硬件的图像表示 (允许直接访问像素数据,且可以用作描绘设备)。 更多...

Header: #include <QImage>
CMake: find_package(Qt6 COMPONENTS Gui REQUIRED)
target_link_libraries(mytarget PRIVATE Qt6::Gui)
qmake: QT += gui
继承: QPaintDevice

注意: 此类的所有函数 可重入 .

公共类型

enum Format { Format_Invalid, Format_Mono, Format_MonoLSB, Format_Indexed8, Format_RGB32, …, Format_BGR888 }
enum InvertMode { InvertRgb, InvertRgba }

公共函数

QImage (QImage && other )
QImage (const QImage & image )
QImage (const QString & fileName , const char * format = nullptr)
QImage (const char *const [] xpm )
QImage (const uchar * data , int width , int height , qsizetype bytesPerLine , QImage::Format format , QImageCleanupFunction cleanupFunction = nullptr, void * cleanupInfo = nullptr)
QImage (uchar * data , int width , int height , qsizetype bytesPerLine , QImage::Format format , QImageCleanupFunction cleanupFunction = nullptr, void * cleanupInfo = nullptr)
QImage (const uchar * data , int width , int height , QImage::Format format , QImageCleanupFunction cleanupFunction = nullptr, void * cleanupInfo = nullptr)
QImage (uchar * data , int width , int height , QImage::Format format , QImageCleanupFunction cleanupFunction = nullptr, void * cleanupInfo = nullptr)
QImage (int width , int height , QImage::Format format )
QImage (const QSize & size , QImage::Format format )
QImage ()
QImage & operator= (QImage && other )
QImage & operator= (const QImage & image )
virtual ~QImage ()
bool allGray () const
void applyColorTransform (const QColorTransform & transform )
int bitPlaneCount () const
uchar * bits ()
const uchar * bits () const
qsizetype bytesPerLine () const
qint64 cacheKey () const
QRgb color (int i ) const
int colorCount () const
QColorSpace colorSpace () const
QList<QRgb> colorTable () const
const uchar * constBits () const
const uchar * constScanLine (int i ) const
void convertTo (QImage::Format format , Qt::ImageConversionFlags flags = Qt::AutoColor)
void convertToColorSpace (const QColorSpace & colorSpace )
QImage convertToFormat (QImage::Format format , Qt::ImageConversionFlags flags = Qt::AutoColor) const &
QImage convertToFormat (QImage::Format format , Qt::ImageConversionFlags flags = Qt::AutoColor) &&
QImage convertToFormat (QImage::Format format , const QList<QRgb> & colorTable , Qt::ImageConversionFlags flags = Qt::AutoColor) const
QImage convertedTo (QImage::Format format , Qt::ImageConversionFlags flags = Qt::AutoColor) const &
QImage convertedTo (QImage::Format format , Qt::ImageConversionFlags flags = Qt::AutoColor) &&
QImage convertedToColorSpace (const QColorSpace & colorSpace ) const
QImage copy (const QRect & rectangle = QRect()) const
QImage copy (int x , int y , int width , int height ) const
QImage createAlphaMask (Qt::ImageConversionFlags flags = Qt::AutoColor) const
QImage createHeuristicMask (bool clipTight = true) const
QImage createMaskFromColor (QRgb color , Qt::MaskMode mode = Qt::MaskInColor) const
int depth () const
qreal devicePixelRatio () const
int dotsPerMeterX () const
int dotsPerMeterY () const
void fill (uint pixelValue )
void fill (const QColor & color )
void fill (Qt::GlobalColor color )
QImage::Format format () const
bool hasAlphaChannel () const
int height () const
void invertPixels (QImage::InvertMode mode = InvertRgb)
bool isGrayscale () const
bool isNull () const
bool load (const QString & fileName , const char * format = nullptr)
bool load (QIODevice * device , const char * format )
bool loadFromData (const uchar * data , int len , const char * format = nullptr)
bool loadFromData (const QByteArray & data , const char * format = nullptr)
void mirror (bool horizontal = false, bool vertical = true)
QImage mirrored (bool horizontal = false, bool vertical = true) const &
QImage mirrored (bool horizontal = false, bool vertical = true) &&
QPoint offset () const
QRgb pixel (const QPoint & position ) const
QRgb pixel (int x , int y ) const
QColor pixelColor (const QPoint & position ) const
QColor pixelColor (int x , int y ) const
QPixelFormat pixelFormat () const
int pixelIndex (const QPoint & position ) const
int pixelIndex (int x , int y ) const
QRect rect () const
bool reinterpretAsFormat (QImage::Format format )
void rgbSwap ()
QImage rgbSwapped () const &
QImage rgbSwapped () &&
bool save (const QString & fileName , const char * format = nullptr, int quality = -1) const
bool save (QIODevice * device , const char * format = nullptr, int quality = -1) const
QImage scaled (const QSize & size , Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio, Qt::TransformationMode transformMode = Qt::FastTransformation) const
QImage scaled (int width , int height , Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio, Qt::TransformationMode transformMode = Qt::FastTransformation) const
QImage scaledToHeight (int height , Qt::TransformationMode mode = Qt::FastTransformation) const
QImage scaledToWidth (int width , Qt::TransformationMode mode = Qt::FastTransformation) const
uchar * scanLine (int i )
const uchar * scanLine (int i ) const
void setAlphaChannel (const QImage & alphaChannel )
void setColor (int index , QRgb colorValue )
void setColorCount (int colorCount )
void setColorSpace (const QColorSpace & colorSpace )
void setColorTable (const QList<QRgb> & colors )
void setDevicePixelRatio (qreal scaleFactor )
void setDotsPerMeterX (int x )
void setDotsPerMeterY (int y )
void setOffset (const QPoint & offset )
void setPixel (const QPoint & position , uint index_or_rgb )
void setPixel (int x , int y , uint index_or_rgb )
void setPixelColor (const QPoint & position , const QColor & color )
void setPixelColor (int x , int y , const QColor & color )
void setText (const QString & key , const QString & text )
QSize size () const
qsizetype sizeInBytes () const
void swap (QImage & other )
QString text (const QString & key = QString()) const
QStringList textKeys () const
CGImageRef toCGImage () const
HBITMAP toHBITMAP () const
HICON toHICON (const QImage & mask = {}) const
QImage transformed (const QTransform & matrix , Qt::TransformationMode mode = Qt::FastTransformation) const
bool valid (const QPoint & pos ) const
bool valid (int x , int y ) const
int width () const
QVariant operator QVariant () const
bool operator!= (const QImage & image ) const
bool operator== (const QImage & image ) const

静态公共成员

QImage fromData (const uchar * data , int size , const char * format = nullptr)
QImage fromData (const QByteArray & data , const char * format = nullptr)
QImage fromHBITMAP (HBITMAP hbitmap )
QImage fromHICON (HICON icon )
QImage::Format toImageFormat (QPixelFormat format )
QPixelFormat toPixelFormat (QImage::Format format )
QTransform trueMatrix (const QTransform & matrix , int width , int height )
QImageCleanupFunction
QDataStream & operator<< (QDataStream & stream , const QImage & image )
QDataStream & operator>> (QDataStream & stream , QImage & image )

详细描述

Qt 为处理图像数据提供了 4 个类:QImage、 QPixmap , QBitmap and QPicture 。QImage 是为 I/O 和直接像素访问 操纵而设计 优化的,而 QPixmap 为在屏幕上展示图像而设计 优化的。 QBitmap 只是方便类,继承 QPixmap ,确保 1 深度。最后, QPicture 类是描绘设备,它记录并重演 QPainter 命令。

由于 QImage 是 QPaintDevice 子类, QPainter 可用于在图像上直接绘制。当使用 QPainter 在 QImage,描绘可以在当前 GUI 线程外的其它线程中履行。

QImage 类支持几种图像格式,描述通过 格式 枚举。这些包括单色、8 位、32 位且 Alpha 融合图像 (可用于所有 Qt 4.x 版本)。

QImage 提供一批函数,可用于获取有关图像的各种信息。还有几个函数使图像能够变换。

QImage 对象可以按值传递,由于 QImage 类使用 隐式数据共享 。QImage 对象还可以被流化和比较。

注意: 若愿意以静态 Qt 构建加载 QImage 对象,参考 插件怎么样 .

警告: 在 QImage 上描绘采用格式 QImage::Format_Indexed8 不被支持。

读写图像文件

QImage 提供加载图像文件的几种方式:可以在构造 QImage 对象时加载文件,或通过使用 load () 或 loadFromData () 函数稍后。QImage 还提供静态 fromData () 函数,从给定数据构造 QImage。当加载图像时,文件名可以引用实际磁盘文件,也可以引用某一应用程序嵌入资源。见 Qt 资源系统 概述,了解如何在应用程序可执行文件中嵌入图像及其它资源文件的细节。

只需调用 save () 函数以保存 QImage 对象。

可获得支持的文件格式的完整列表,透过 QImageReader::supportedImageFormats () 和 QImageWriter::supportedImageFormats () 函数。可以将新文件格式添加作为插件。默认情况下,Qt 支持以下格式:

格式 描述 Qt 支持
BMP Windows 位图 读/写
GIF Graphic Interchange Format (图形互换格式) 可选 读取
JPG Joint Photographic Experts Group (联合摄影专家组) 读/写
JPEG Joint Photographic Experts Group (联合摄影专家组) 读/写
PNG PNG (便携式网络图形) 读/写
PBM PBM (便携式位图) 读取
PGM PGM (便携式灰度图) 读取
PPM Portable Pixmap (便携式像素图) 读/写
XBM X11 Bitmap (X11 位图) 读/写
XPM X11 Pixmap (X11 像素图) 读/写

图像信息

QImage 提供可用于获取有关图像各种信息的函数集合:

可用函数
几何体 size (), width (), height (), dotsPerMeterX (),和 dotsPerMeterY () 函数提供图像大小和宽高比的有关信息。

rect () 函数返回图像的封闭矩形。 valid () 函数告诉给定坐标对是否在此矩形内。 offset () function returns the number of pixels by which the image is intended to be offset by when positioned relative to other images, which also can be manipulated using the setOffset () 函数。

Colors The color of a pixel can be retrieved by passing its coordinates to the pixel () 函数。 pixel () function returns the color as a QRgb value indepedent of the image's format.

In case of monochrome and 8-bit images, the colorCount () 和 colorTable () functions provide information about the color components used to store the image data: The colorTable () function returns the image's entire color table. To obtain a single entry, use the pixelIndex () function to retrieve the pixel index for a given pair of coordinates, then use the color () function to retrieve the color. Note that if you create an 8-bit image manually, you have to set a valid color table on the image as well.

hasAlphaChannel () function tells if the image's format respects the alpha channel, or not. The allGray () 和 isGrayscale () functions tell whether an image's colors are all shades of gray.

另请参阅 像素操纵 and 图像变换 sections.

Text text () function returns the image text associated with the given text key. An image's text keys can be retrieved using the textKeys () function. Use the setText () function to alter an image's text.
低级信息 depth () function returns the depth of the image. The supported depths are 1 (monochrome), 8, 16, 24 and 32 bits. The bitPlaneCount () function tells how many of those bits that are used. For more information see the 图像格式 章节。

format (), bytesPerLine (),和 sizeInBytes () functions provide low-level information about the data stored in the image.

cacheKey () 函数返回此 QImage 对象内容的唯一标识数。

像素操纵

The functions used to manipulate an image's pixels depend on the image format. The reason is that monochrome and 8-bit images are index-based and use a color lookup table, while 32-bit images store ARGB values directly. For more information on image formats, see the 图像格式 章节。

若是 32 位图像, setPixel () function can be used to alter the color of the pixel at the given coordinates to any other color specified as an ARGB quadruplet. To make a suitable QRgb value, use the qRgb () (adding a default alpha component to the given RGB values, i.e. creating an opaque color) or qRgba () 函数。例如:

32 位
QImage image(3, 3, QImage::Format_RGB32);
QRgb value;
value = qRgb(189, 149, 39); // 0xffbd9527
image.setPixel(1, 1, value);
value = qRgb(122, 163, 39); // 0xff7aa327
image.setPixel(0, 1, value);
image.setPixel(1, 0, value);
value = qRgb(237, 187, 51); // 0xffedba31
image.setPixel(2, 1, value);
										

In case of a 8-bit and monchrome images, the pixel value is only an index from the image's color table. So the setPixel () function can only be used to alter the color of the pixel at the given coordinates to a predefined color from the image's color table, i.e. it can only change the pixel's index value. To alter or add a color to an image's color table, use the setColor () 函数。

An entry in the color table is an ARGB quadruplet encoded as an QRgb 值。使用 qRgb () 和 qRgba () functions to make a suitable QRgb value for use with the setColor () 函数。例如:

8 位
QImage image(3, 3, QImage::Format_Indexed8);
QRgb value;
value = qRgb(122, 163, 39); // 0xff7aa327
image.setColor(0, value);
value = qRgb(237, 187, 51); // 0xffedba31
image.setColor(1, value);
value = qRgb(189, 149, 39); // 0xffbd9527
image.setColor(2, value);
image.setPixel(0, 1, 0);
image.setPixel(1, 0, 0);
image.setPixel(1, 1, 2);
image.setPixel(2, 1, 1);
										

For images with more than 8-bit per color-channel. The methods setPixelColor () 和 pixelColor () can be used to set and get with QColor 值。

QImage 还提供 scanLine () function which returns a pointer to the pixel data at the scanline with the given index, and the bits () function which returns a pointer to the first pixel data (this is equivalent to scanLine(0) ).

图像格式

Each pixel stored in a QImage is represented by an integer. The size of the integer varies depending on the format. QImage supports several image formats described by the 格式 枚举。

Monochrome images are stored using 1-bit indexes into a color table with at most two colors. There are two different types of monochrome images: big endian (MSB first) or little endian (LSB first) bit order.

8-bit images are stored using 8-bit indexes into a color table, i.e. they have a single byte per pixel. The color table is a QList < QRgb >, and the QRgb typedef is equivalent to an unsigned int containing an ARGB quadruplet on the format 0xAARRGGBB.

32-bit images have no color table; instead, each pixel contains an QRgb value. There are three different types of 32-bit images storing RGB (i.e. 0xffRRGGBB), ARGB and premultiplied ARGB values respectively. In the premultiplied format the red, green, and blue channels are multiplied by the alpha component divided by 255.

An image's format can be retrieved using the format () function. Use the convertToFormat () functions to convert an image into another format. The allGray () 和 isGrayscale () functions tell whether a color image can safely be converted to a grayscale image.

图像变换

QImage supports a number of functions for creating a new image that is a transformed version of the original: The createAlphaMask () function builds and returns a 1-bpp mask from the alpha buffer in this image, and the createHeuristicMask () function creates and returns a 1-bpp heuristic mask for this image. The latter function works by selecting a color from one of the corners, then chipping away pixels of that color starting at all the edges.

mirrored () 函数返回期望方向的图像镜像, scaled () returns a copy of the image scaled to a rectangle of the desired measures, and the rgbSwapped () function constructs a BGR image from a RGB image.

scaledToWidth () 和 scaledToHeight () functions return scaled copies of the image.

transformed () function returns a copy of the image 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 image containing all transformed points of the original image. The static trueMatrix () function returns the actual matrix used for transforming the image.

There are also functions for changing attributes of an image in-place:

Function 描述
setDotsPerMeterX () Defines the aspect ratio by setting the number of pixels that fit horizontally in a physical meter.
setDotsPerMeterY () Defines the aspect ratio by setting the number of pixels that fit vertically in a physical meter.
fill () Fills the entire image with the given pixel value.
invertPixels () Inverts all pixel values in the image using the given InvertMode 值。
setColorTable () Sets the color table used to translate color indexes. Only monochrome and 8-bit formats.
setColorCount () Resizes the color table. Only monochrome and 8-bit formats.

另请参阅 QImageReader , QImageWriter , QPixmap , QSvgRenderer , 图像合成范例 , 图像查看器范例 , 涂鸦范例 ,和 像素器范例 .

成员类型文档编制

enum QImage:: 格式

以下图像格式可用于 Qt。见表格后注意事项。

常量 描述
QImage::Format_Invalid 0 图像无效。
QImage::Format_Mono 1 The image is stored using 1-bit per pixel. Bytes are packed with the most significant bit (MSB) first.
QImage::Format_MonoLSB 2 The image is stored using 1-bit per pixel. Bytes are packed with the less significant bit (LSB) first.
QImage::Format_Indexed8 3 图像使用 8 位索引存储到颜色图。
QImage::Format_RGB32 4 图像使用 32 位 RGB 格式 (0xffRRGGBB) 存储。
QImage::Format_ARGB32 5 图像使用 32 位 ARGB 格式 (0xAARRGGBB) 存储。
QImage::Format_ARGB32_Premultiplied 6 The image is stored using a premultiplied 32-bit ARGB format (0xAARRGGBB), i.e. the red, green, and blue channels are multiplied by the alpha component divided by 255. (If RR, GG, or BB has a higher value than the alpha channel, the results are undefined.) Certain operations (such as image composition using alpha blending) are faster using premultiplied ARGB32 than with plain ARGB32.
QImage::Format_RGB16 7 图像使用 16 位 RGB 格式 (5-6-5) 存储。
QImage::Format_ARGB8565_Premultiplied 8 The image is stored using a premultiplied 24-bit ARGB format (8-5-6-5).
QImage::Format_RGB666 9 The image is stored using a 24-bit RGB format (6-6-6). The unused most significant bits is always zero.
QImage::Format_ARGB6666_Premultiplied 10 The image is stored using a premultiplied 24-bit ARGB format (6-6-6-6).
QImage::Format_RGB555 11 The image is stored using a 16-bit RGB format (5-5-5). The unused most significant bit is always zero.
QImage::Format_ARGB8555_Premultiplied 12 The image is stored using a premultiplied 24-bit ARGB format (8-5-5-5).
QImage::Format_RGB888 13 图像使用 24 位 RGB 格式 (8-8-8) 存储。
QImage::Format_RGB444 14 The image is stored using a 16-bit RGB format (4-4-4). The unused bits are always zero.
QImage::Format_ARGB4444_Premultiplied 15 The image is stored using a premultiplied 16-bit ARGB format (4-4-4-4).
QImage::Format_RGBX8888 16 The image is stored using a 32-bit byte-ordered RGB(x) format (8-8-8-8). This is the same as the Format_RGBA8888 except alpha must always be 255. (added in Qt 5.2)
QImage::Format_RGBA8888 17 The image is stored using a 32-bit byte-ordered RGBA format (8-8-8-8). Unlike ARGB32 this is a byte-ordered format, which means the 32bit encoding differs between big endian and little endian architectures, being respectively (0xRRGGBBAA) and (0xAABBGGRR). The order of the colors is the same on any architecture if read as bytes 0xRR,0xGG,0xBB,0xAA. (added in Qt 5.2)
QImage::Format_RGBA8888_Premultiplied 18 The image is stored using a premultiplied 32-bit byte-ordered RGBA format (8-8-8-8). (added in Qt 5.2)
QImage::Format_BGR30 19 The image is stored using a 32-bit BGR format (x-10-10-10). (added in Qt 5.4)
QImage::Format_A2BGR30_Premultiplied 20 The image is stored using a 32-bit premultiplied ABGR format (2-10-10-10). (added in Qt 5.4)
QImage::Format_RGB30 21 图像使用 32 位 RGB 格式 (x-10-10-10) 存储。(在 Qt 5.4 添加)
QImage::Format_A2RGB30_Premultiplied 22 The image is stored using a 32-bit premultiplied ARGB format (2-10-10-10). (added in Qt 5.4)
QImage::Format_Alpha8 23 图像使用 8 位仅 alpha 格式存储。(在 Qt 5.5 添加)
QImage::Format_Grayscale8 24 图像使用 8 位灰度格式存储。(在 Qt 5.5 添加)
QImage::Format_Grayscale16 28 图像使用 16 位灰度格式存储。(在 Qt 5.13 添加)
QImage::Format_RGBX64 25 The image is stored using a 64-bit halfword-ordered RGB(x) format (16-16-16-16). This is the same as the Format_RGBA64 except alpha must always be 65535. (added in Qt 5.12)
QImage::Format_RGBA64 26 图像使用 64 位半字有序 RGBA 格式 (16-16-16-16) 存储。(在 Qt 5.12 添加)
QImage::Format_RGBA64_Premultiplied 27 The image is stored using a premultiplied 64-bit halfword-ordered RGBA format (16-16-16-16). (added in Qt 5.12)
QImage::Format_BGR888 29 图像使用 24 位 BGR 格式存储。(在 Qt 5.14 添加)

注意: 绘制进 QImage 采用 QImage::Format_Indexed8 不支持。

注意: 避免大多数直接渲染到大多数这些格式使用 QPainter 。最好优化渲染针对 Format_RGB32 and Format_ARGB32_Premultiplied 格式,和其次是渲染到 Format_RGB16 , Format_RGBX8888 , Format_RGBA8888_Premultiplied , Format_RGBX64 and Format_RGBA64_Premultiplied formats

另请参阅 format () 和 convertToFormat ().

enum QImage:: InvertMode

This enum type is used to describe how pixel values should be inverted in the invertPixels () 函数。

常量 描述
QImage::InvertRgb 0 Invert only the RGB values and leave the alpha channel unchanged.
QImage::InvertRgba 1 反转所有通道,包括 Alpha 通道。

另请参阅 invertPixels ().

成员函数文档编制

QImage QImage:: rgbSwapped () &&

QImage QImage:: rgbSwapped () const &

返回 QImage in which the values of the red and blue components of all pixels have been swapped, effectively converting an RGB image to an BGR image.

原始 QImage 无变化。

另请参阅 rgbSwap () 和 图像变换 .

QImage QImage:: mirrored ( bool horizontal = false, bool vertical = true) &&

QImage QImage:: mirrored ( bool horizontal = false, bool vertical = true) const &

Returns a mirror of the image, mirrored in the horizontal and/or the vertical direction depending on whether horizontal and vertical are set to true or false.

注意:原始图像无变化。

另请参阅 mirror () 和 图像变换 .

QImage QImage:: convertedTo ( QImage::Format format , Qt::ImageConversionFlags flags = Qt::AutoColor) &&

QImage QImage:: convertedTo ( QImage::Format format , Qt::ImageConversionFlags flags = Qt::AutoColor) const &

返回图像副本按给定 format .

指定图像转换 flags 控制转换过程中如何处理图像数据。

该函数在 Qt 6.0 引入。

另请参阅 convertTo () 和 图像格式 .

QImage QImage:: convertToFormat ( QImage::Format format , Qt::ImageConversionFlags flags = Qt::AutoColor) &&

QImage QImage:: convertToFormat ( QImage::Format format , Qt::ImageConversionFlags flags = Qt::AutoColor) const &

返回图像副本按给定 format .

指定图像转换 flags 控制转换过程中如何处理图像数据。

另请参阅 convertTo () 和 图像格式 .

[since 5.2] QImage:: QImage ( QImage && other )

Move-constructs a QImage instance, making it point at the same object that other 所指向的。

该函数在 Qt 5.2 引入。

QImage:: QImage (const QImage & image )

构造浅拷贝为给定 image .

有关浅拷贝的更多信息,见 隐式数据共享 文档编制。

另请参阅 copy ().

QImage:: QImage (const QString & fileName , const char * format = nullptr)

构造图像并试着从文件加载图像,采用给定 fileName .

加载器试图读取图像使用指定 format 。若 format is not specified (which is the default), it is auto-detected based on the file's suffix and header. For details, see { QImageReader::setAutoDetectImageFormat ()}{ QImageReader }.

若图像加载失败,此对象为 null 图像。

The file name can either refer to an actual file on disk or to one of the application's embedded resources. See the 资源系统 概述,了解如何在应用程序可执行文件中嵌入图像及其它资源文件的细节。

另请参阅 isNull () 和 读写图像文件 .

QImage:: QImage (const char *const [] xpm )

构造图像从给定 xpm 图像。

确保图像是有效 XPM 图像。错误被默默忽略。

注意,通过使用不寻常声明稍微挤压 XPM 变量是可能的:

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 (e.g., when the code is in a shared library) and able to be stored in ROM with the application.

QImage:: QImage (const uchar * data , int width , int height , qsizetype bytesPerLine , QImage::Format format , QImageCleanupFunction cleanupFunction = nullptr, void * cleanupInfo = nullptr)

构造图像采用给定 width , height and format , that uses an existing memory buffer, data width and height must be specified in pixels. bytesPerLine specifies the number of bytes per line (stride).

The buffer must remain valid throughout the life of the QImage and all copies that have not been modified or otherwise detached from the original buffer. The image does not delete the buffer at destruction. You can provide a function pointer cleanupFunction along with an extra pointer cleanupInfo that will be called when the last copy is destroyed.

format is an indexed color format, the image color table is initially empty and must be sufficiently expanded with setColorCount () 或 setColorTable () before the image is used.

Unlike the similar QImage constructor that takes a non-const data buffer, this version will never alter the contents of the buffer. For example, calling QImage::bits () will return a deep copy of the image, rather than the buffer passed to the constructor. This allows for the efficiency of constructing a QImage from raw data, without the possibility of the raw data being changed.

QImage:: QImage ( uchar * data , int width , int height , qsizetype bytesPerLine , QImage::Format format , QImageCleanupFunction cleanupFunction = nullptr, void * cleanupInfo = nullptr)

构造图像采用给定 width , height and format , that uses an existing memory buffer, data width and height must be specified in pixels. bytesPerLine specifies the number of bytes per line (stride).

The buffer must remain valid throughout the life of the QImage and all copies that have not been modified or otherwise detached from the original buffer. The image does not delete the buffer at destruction. You can provide a function pointer cleanupFunction along with an extra pointer cleanupInfo that will be called when the last copy is destroyed.

format is an indexed color format, the image color table is initially empty and must be sufficiently expanded with setColorCount () 或 setColorTable () before the image is used.

QImage:: QImage (const uchar * data , int width , int height , QImage::Format format , QImageCleanupFunction cleanupFunction = nullptr, void * cleanupInfo = nullptr)

构造图像采用给定 width , height and format , that uses an existing read-only memory buffer, data width and height must be specified in pixels, data must be 32-bit aligned, and each scanline of data in the image must also be 32-bit aligned.

The buffer must remain valid throughout the life of the QImage and all copies that have not been modified or otherwise detached from the original buffer. The image does not delete the buffer at destruction. You can provide a function pointer cleanupFunction along with an extra pointer cleanupInfo that will be called when the last copy is destroyed.

format is an indexed color format, the image color table is initially empty and must be sufficiently expanded with setColorCount () 或 setColorTable () before the image is used.

Unlike the similar QImage constructor that takes a non-const data buffer, this version will never alter the contents of the buffer. For example, calling QImage::bits () will return a deep copy of the image, rather than the buffer passed to the constructor. This allows for the efficiency of constructing a QImage from raw data, without the possibility of the raw data being changed.

QImage:: QImage ( uchar * data , int width , int height , QImage::Format format , QImageCleanupFunction cleanupFunction = nullptr, void * cleanupInfo = nullptr)

构造图像采用给定 width , height and format , that uses an existing memory buffer, data width and height must be specified in pixels, data must be 32-bit aligned, and each scanline of data in the image must also be 32-bit aligned.

The buffer must remain valid throughout the life of the QImage and all copies that have not been modified or otherwise detached from the original buffer. The image does not delete the buffer at destruction. You can provide a function pointer cleanupFunction along with an extra pointer cleanupInfo that will be called when the last copy is destroyed.

format is an indexed color format, the image color table is initially empty and must be sufficiently expanded with setColorCount () 或 setColorTable () before the image is used.

QImage:: QImage ( int width , int height , QImage::Format format )

构造图像采用给定 width , height and format .

A null image will be returned if memory cannot be allocated.

警告: 这将创建带有未初始化数据的 QImage。调用 fill () to fill the image with an appropriate pixel value before drawing onto it with QPainter .

QImage:: QImage (const QSize & size , QImage::Format format )

构造图像采用给定 size and format .

A null 图像被返回,若无法分配内存。

警告: 这将创建带有未初始化数据的 QImage。调用 fill () to fill the image with an appropriate pixel value before drawing onto it with QPainter .

QImage:: QImage ()

构造空图像。

另请参阅 isNull ().

[since 5.2] QImage &QImage:: operator= ( QImage && other )

移动赋值 other 到此 QImage 实例。

该函数在 Qt 5.2 引入。

QImage &QImage:: operator= (const QImage & image )

Assigns a shallow copy of the given image to this image and returns a reference to this image.

有关浅拷贝的更多信息,见 隐式数据共享 文档编制。

另请参阅 copy () 和 QImage ().

[virtual] QImage:: ~QImage ()

销毁图像并清理。

bool QImage:: allGray () const

返回 true if all the colors in the image are shades of gray (i.e. their red, green and blue components are equal); otherwise false.

Note that this function is slow for images without color table.

另请参阅 isGrayscale ().

[since 5.14] void QImage:: applyColorTransform (const QColorTransform & transform )

应用颜色变换 transform to all pixels in the image.

该函数在 Qt 5.14 引入。

int QImage:: bitPlaneCount () const

返回图像位平面数。

The number of bit planes is the number of bits of color and transparency information for each pixel. This is different from (i.e. smaller than) the depth when the image format contains unused bits.

另请参阅 depth (), format (),和 图像格式 .

uchar *QImage:: bits ()

返回第一像素数据指针。这相当于 scanLine (0).

注意, QImage 使用 隐式数据共享 . This function performs a deep copy of the shared pixel data, thus ensuring that this QImage is the only one using the current return value.

另请参阅 scanLine (), sizeInBytes (),和 constBits ().

const uchar *QImage:: bits () const

这是重载函数。

注意, QImage 使用 隐式数据共享 , but this function does not perform a deep copy of the shared pixel data, because the returned data is const.

qsizetype QImage:: bytesPerLine () const

返回每图像扫描线的字节数。

这相当于 sizeInBytes () / height () if height () 非零。

另请参阅 scanLine ().

qint64 QImage:: cacheKey () const

Returns a number that identifies the contents of this QImage object. Distinct QImage objects can only have the same key if they refer to the same contents.

键将改变当变更图像时。

QRgb QImage:: color ( int i ) const

Returns the color in the color table at index i . The first color is at index 0.

The colors in an image's color table are specified as ARGB quadruplets ( QRgb ). Use the qAlpha(), qRed(), qGreen(), and qBlue() functions to get the color value components.

另请参阅 setColor (), pixelIndex (),和 像素操纵 .

int QImage:: colorCount () const

返回图像的颜色表大小。

Notice that colorCount() returns 0 for 32-bpp images because these images do not use color tables, but instead encode pixel values as ARGB quadruplets.

另请参阅 setColorCount () 和 图像信息 .

[since 5.14] QColorSpace QImage:: colorSpace () const

返回图像的色彩空间,若有定义色彩空间。

该函数在 Qt 5.14 引入。

另请参阅 setColorSpace ().

QList < QRgb > QImage:: colorTable () const

Returns a list of the colors contained in the image's color table, or an empty list if the image does not have a color table

另请参阅 setColorTable (), colorCount (),和 color ().

const uchar *QImage:: constBits () const

返回第一像素数据指针。

注意, QImage 使用 隐式数据共享 , but this function does not perform a deep copy of the shared pixel data, because the returned data is const.

另请参阅 bits () 和 constScanLine ().

const uchar *QImage:: constScanLine ( int i ) const

Returns a pointer to the pixel data at the scanline with index i . The first scanline is at index 0.

The scanline data is as minimum 32-bit aligned. For 64-bit formats it follows the native alignment of 64-bit integers (64-bit for most platforms, but notably 32-bit on i386).

注意, QImage 使用 隐式数据共享 , but this function does not perform a deep copy of the shared pixel data, because the returned data is const.

另请参阅 scanLine () 和 constBits ().

[since 5.13] void QImage:: convertTo ( QImage::Format format , Qt::ImageConversionFlags flags = Qt::AutoColor)

Converts the image to the given format in place, detaching if necessary.

指定图像转换 flags 控制转换过程中如何处理图像数据。

该函数在 Qt 5.13 引入。

另请参阅 convertedTo ().

[since 5.14] void QImage:: convertToColorSpace (const QColorSpace & colorSpace )

将图像转换成 colorSpace .

若图像没有有效色彩空间,方法什么都不做。

该函数在 Qt 5.14 引入。

另请参阅 convertedToColorSpace () 和 setColorSpace ().

QImage QImage:: convertToFormat ( QImage::Format format , const QList < QRgb > & colorTable , Qt::ImageConversionFlags flags = Qt::AutoColor) const

这是重载函数。

Returns a copy of the image converted to the given format , using the specified colorTable .

Conversion from RGB formats to indexed formats is a slow operation and will use a straightforward nearest color approach, with no dithering.

[since 5.14] QImage QImage:: convertedToColorSpace (const QColorSpace & colorSpace ) const

返回图像转换到 colorSpace .

若图像没有有效色彩空间,null QImage 被返回。

该函数在 Qt 5.14 引入。

另请参阅 convertToColorSpace ().

QImage QImage:: copy (const QRect & rectangle = QRect()) const

将图像的子区域作为新图像返回。

The returned image is copied from the position ( rectangle .x(), rectangle .y()) in this image, and will always have the size of the given rectangle .

In areas beyond this image, pixels are set to 0. For 32-bit RGB images, this means black; for 32-bit ARGB images, this means transparent black; for 8-bit images, this means the color with index 0 in the color table which can be anything; for 1-bit images, this means Qt::color0 .

若给定 rectangle is a null rectangle the entire image is copied.

另请参阅 QImage ().

QImage QImage:: copy ( int x , int y , int width , int height ) const

这是重载函数。

The returned image is copied from the position ( x , y ) in this image, and will always have the given width and height . In areas beyond this image, pixels are set to 0.

QImage QImage:: createAlphaMask ( Qt::ImageConversionFlags flags = Qt::AutoColor) const

Builds and returns a 1-bpp mask from the alpha buffer in this image. Returns a null image if the image's format is QImage::Format_RGB32 .

flags 自变量是按位 OR 的 Qt::ImageConversionFlags , and controls the conversion process. Passing 0 for flags sets all the default options.

The returned image has little-endian bit order (i.e. the image's format is QImage::Format_MonoLSB ), which you can convert to big-endian ( QImage::Format_Mono ) using the convertToFormat () 函数。

另请参阅 createHeuristicMask () 和 图像变换 .

QImage QImage:: createHeuristicMask ( bool clipTight = true) const

Creates and returns a 1-bpp heuristic mask for this image.

The function works by selecting a color from one of the corners, then chipping away pixels of that color starting at all the edges. The four corners vote for which color is to be masked away. In case of a draw (this generally means that this function is not applicable to the image), the result is arbitrary.

The returned image has little-endian bit order (i.e. the image's format is QImage::Format_MonoLSB ), which you can convert to big-endian ( QImage::Format_Mono ) using the convertToFormat () 函数。

clipTight is true (the default) the mask is just large enough to cover the pixels; otherwise, the mask is larger than the data pixels.

Note that this function disregards the alpha buffer.

另请参阅 createAlphaMask () 和 图像变换 .

QImage QImage:: createMaskFromColor ( QRgb color , Qt::MaskMode mode = Qt::MaskInColor) const

Creates and returns a mask for this image based on the given color value. If the mode is MaskInColor (the default value), all pixels matching color will be opaque pixels in the mask. If mode is MaskOutColor, all pixels matching the given color will be transparent.

另请参阅 createAlphaMask () 和 createHeuristicMask ().

int QImage:: depth () const

返回图像的深度。

The image depth is the number of bits used to store a single pixel, also called bits per pixel (bpp).

支持深度:1、8、16、24、32 及 64。

另请参阅 bitPlaneCount (), convertToFormat (), 图像格式 ,和 图像信息 .

qreal QImage:: devicePixelRatio () const

Returns the device pixel ratio for the image. This is the ratio between device pixels and device independent pixels .

Use this function when calculating layout geometry based on the image size: QSize layoutSize = image. size () / image.devicePixelRatio()

默认值为 1.0。

另请参阅 setDevicePixelRatio () 和 QImageReader .

int QImage:: dotsPerMeterX () const

Returns the number of pixels that fit horizontally in a physical meter. Together with dotsPerMeterY (), this number defines the intended scale and aspect ratio of the image.

另请参阅 setDotsPerMeterX () 和 图像信息 .

int QImage:: dotsPerMeterY () const

Returns the number of pixels that fit vertically in a physical meter. Together with dotsPerMeterX (), this number defines the intended scale and aspect ratio of the image.

另请参阅 setDotsPerMeterY () 和 图像信息 .

void QImage:: fill ( uint pixelValue )

填充整个图像采用给定 pixelValue .

If the depth of this image is 1, only the lowest bit is used. If you say fill(0), fill(2), etc., the image is filled with 0s. If you say fill(1), fill(3), etc., the image is filled with 1s. If the depth is 8, the lowest 8 bits are used and if the depth is 16 the lowest 16 bits are used.

If the image depth is higher than 32bit the result is undefined.

注意: There are no corresponding value getter, though QImage::pixelIndex () will return the same value for indexed formats, and QImage::pixel () for RGB32, ARGB32, and ARGB32PM formats.

另请参阅 depth () 和 图像变换 .

void QImage:: fill (const QColor & color )

这是重载函数。

填充整个图像采用给定 color .

若图像深度为 1,图像将被填充采用 1 若 color 等于 Qt::color1 ;否则将被填充采用 0。

If the depth of the image is 8, the image will be filled with the index corresponding the color in the color table if present; it will otherwise be filled with 0.

void QImage:: fill ( Qt::GlobalColor color )

这是重载函数。

填充图像采用给定 color ,描述为标准全局颜色。

QImage::Format QImage:: format () const

返回图像格式。

另请参阅 图像格式 .

[static] QImage QImage:: fromData (const uchar * data , int size , const char * format = nullptr)

构造 QImage from the first size bytes of the given binary data . The loader attempts to read the image using the specified format 。若 format is not specified (which is the default), the loader probes the data for a header to guess the file format.

format 有指定,它必须是某一值返回通过 QImageReader::supportedImageFormats ().

若图像加载失败,返回图像将为 null 图像。

另请参阅 load (), save (),和 读写图像文件 .

[static] QImage QImage:: fromData (const QByteArray & data , const char * format = nullptr)

这是重载函数。

加载图像从给定 QByteArray data .

[static, since 6.0] QImage QImage:: fromHBITMAP ( HBITMAP hbitmap )

返回 QImage that is equivalent to the given hbitmap .

HBITMAP does not store information about the alpha channel.

In the standard case, the alpha channel is ignored and a fully opaque image is created (typically of format QImage::Format_RGB32 ).

There are cases where the alpha channel is used, though, for example for application icon or systray icons. In that case, reinterpretAsFormat(QImage::Format_ARGB32) should be called on the returned image to ensure the format is correct.

该函数在 Qt 6.0 引入。

另请参阅 toHBITMAP () 和 reinterpretAsFormat ().

[static, since 6.0] QImage QImage:: fromHICON ( HICON icon )

返回 QImage that is equivalent to the given icon .

该函数在 Qt 6.0 引入。

另请参阅 toHICON ().

bool QImage:: hasAlphaChannel () const

返回 true 若图像拥有的格式遵守 Alpha 通道,否则返回 false .

另请参阅 图像信息 .

int QImage:: height () const

返回图像的高度。

另请参阅 图像信息 .

void QImage:: invertPixels ( QImage::InvertMode mode = InvertRgb)

反转图像所有像素值。

The given invert mode only have a meaning when the image's depth is 32. The default mode is InvertRgb , which leaves the alpha channel unchanged. If the mode is InvertRgba , the alpha bits are also inverted.

Inverting an 8-bit image means to replace all pixels using color index i with a pixel using color index 255 minus i . The same is the case for a 1-bit image. Note that the color table is not 改变。

If the image has a premultiplied alpha channel, the image is first converted to an unpremultiplied image format to be inverted and then converted back.

另请参阅 图像变换 .

bool QImage:: isGrayscale () const

对于 32 位图像,此函数相当于 allGray ().

对于颜色索引图像,此函数返回 true 若 color(i) 为 QRgb (i, i, i) 对于颜色表所有索引而言;否则返回 false .

另请参阅 allGray () 和 图像格式 .

bool QImage:: isNull () const

返回 true 若它是 null 图像,否则返回 false .

null 图像的所有参数都设置为零,并且没有分配的数据。

bool QImage:: load (const QString & fileName , const char * format = nullptr)

从文件加载图像采用给定 fileName 。返回 true 若成功加载图像;否则使图像无效并返回 false .

加载器试图读取图像使用指定 format , e.g., PNG or JPG. If format is not specified (which is the default), it is auto-detected based on the file's suffix and header. For details, see QImageReader::setAutoDetectImageFormat ().

The file name can either refer to an actual file on disk or to one of the application's embedded resources. See the 资源系统 概述,了解如何在应用程序可执行文件中嵌入图像及其它资源文件的细节。

另请参阅 读写图像文件 .

bool QImage:: load ( QIODevice * device , const char * format )

这是重载函数。

此函数读取 QImage from the given device . This can, for example, be used to load an image directly into a QByteArray .

bool QImage:: loadFromData (const uchar * data , int len , const char * format = nullptr)

Loads an image from the first len bytes of the given binary data 。返回 true 若成功加载图像;否则使图像无效并返回 false .

加载器试图读取图像使用指定 format , e.g., PNG or JPG. If format is not specified (which is the default), the loader probes the file for a header to guess the file format.

另请参阅 读写图像文件 .

bool QImage:: loadFromData (const QByteArray & data , const char * format = nullptr)

这是重载函数。

加载图像从给定 QByteArray data .

[since 6.0] void QImage:: mirror ( bool horizontal = false, bool vertical = true)

Mirrors of the image in the horizontal and/or the vertical direction depending on whether horizontal and vertical are set to true or false.

该函数在 Qt 6.0 引入。

另请参阅 mirrored () 和 图像变换 .

QPoint QImage:: offset () const

Returns the number of pixels by which the image is intended to be offset by when positioning relative to other images.

另请参阅 setOffset () 和 图像信息 .

QRgb QImage:: pixel (const QPoint & position ) const

返回像素的颜色在给定 position .

position 无效,结果未定义。

警告: This function is expensive when used for massive pixel manipulations. Use constBits () 或 constScanLine () when many pixels needs to be read.

另请参阅 setPixel (), valid (), constBits (), constScanLine (),和 像素操纵 .

QRgb QImage:: pixel ( int x , int y ) const

这是重载函数。

返回像素的颜色在坐标 ( x , y ).

[since 5.6] QColor QImage:: pixelColor (const QPoint & position ) const

返回像素的颜色在给定 position 作为 QColor .

position is not valid, an invalid QColor 被返回。

警告: This function is expensive when used for massive pixel manipulations. Use constBits () 或 constScanLine () when many pixels needs to be read.

该函数在 Qt 5.6 引入。

另请参阅 setPixelColor (), setPixel (), valid (), constBits (), constScanLine (),和 像素操纵 .

[since 5.6] QColor QImage:: pixelColor ( int x , int y ) const

这是重载函数。

返回像素的颜色在坐标 ( x , y ) 作为 QColor .

该函数在 Qt 5.6 引入。

QPixelFormat QImage:: pixelFormat () const

返回 QImage::Format 作为 QPixelFormat

int QImage:: pixelIndex (const QPoint & position ) const

返回像素索引在给定 position .

position 无效,或图像不是调色板图像 ( depth () > 8),结果未定义。

另请参阅 valid (), depth (),和 像素操纵 .

int QImage:: pixelIndex ( int x , int y ) const

这是重载函数。

返回像素索引在 ( x , y ).

QRect QImage:: rect () const

返回封闭矩形 (0, 0, width (), height ()) 为图像。

另请参阅 图像信息 .

[since 5.9] bool QImage:: reinterpretAsFormat ( QImage::Format format )

将图像格式改为 format 不改变数据。仅工作于相同深度的格式之间。

返回 true 若成功。

This function can be used to change images with alpha-channels to their corresponding opaque formats if the data is known to be opaque-only, or to change the format of a given image buffer before overwriting it with new data.

警告: The function does not check if the image data is valid in the new format and will still return true if the depths are compatible. Operations on an image with invalid data are undefined.

警告: 若图像未分离,这将导致数据被拷贝。

该函数在 Qt 5.9 引入。

另请参阅 hasAlphaChannel () 和 convertToFormat ().

[since 6.0] void QImage:: rgbSwap ()

Swaps the values of the red and blue components of all pixels, effectively converting an RGB image to an BGR image.

该函数在 Qt 6.0 引入。

另请参阅 rgbSwapped () 和 图像变换 .

bool QImage:: save (const QString & fileName , const char * format = nullptr, int quality = -1) const

保存图像到文件采用给定 fileName ,使用给定图像文件 format and quality 因子。若 format is nullptr , QImage 将试图猜测格式通过查看 fileName 的后缀。

quality 因子必须在 0 到 100 范围内或为 -1。指定 0 以获得小压缩文件,100 为大的未压缩文件,和 -1 (默认) 使用默认设置。

返回 true 若图像成功保存;否则返回 false .

另请参阅 读写图像文件 .

bool QImage:: save ( QIODevice * device , const char * format = nullptr, int quality = -1) const

这是重载函数。

此函数写入 QImage 到给定 device .

例如,这可以用于将图像直接保存到 QByteArray :

QImage image;
QByteArray ba;
QBuffer buffer(&ba);
buffer.open(QIODevice::WriteOnly);
image.save(&buffer, "PNG"); // writes image into ba in PNG format
							

QImage QImage:: scaled (const QSize & size , Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio, Qt::TransformationMode transformMode = Qt::FastTransformation) const

Returns a copy of the image scaled to a rectangle defined by the given size according to the given aspectRatioMode and transformMode .

若给定 size 为空,此函数返回 null 图像。

另请参阅 isNull () 和 图像变换 .

QImage QImage:: scaled ( int width , int height , Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio, Qt::TransformationMode transformMode = Qt::FastTransformation) const

这是重载函数。

Returns a copy of the image scaled to a rectangle with the given width and height according to the given aspectRatioMode and transformMode .

width height 为 0 或负数,此函数返回 null 图像。

QImage QImage:: 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 .

This function automatically calculates the width of the image so that the ratio of the image is preserved.

若给定 height 为 0 或负数,返回 null 图像。

另请参阅 图像变换 .

QImage QImage:: scaledToWidth ( int width , 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 .

This function automatically calculates the height of the image so that its aspect ratio is preserved.

若给定 width 为 0 或负数,返回 null 图像。

另请参阅 图像变换 .

uchar *QImage:: scanLine ( int i )

Returns a pointer to the pixel data at the scanline with index i . The first scanline is at index 0.

The scanline data is as minimum 32-bit aligned. For 64-bit formats it follows the native alignment of 64-bit integers (64-bit for most platforms, but notably 32-bit on i386).

For example, to remove the green component of each pixel in an image:

for (int y = 0; y < image.height(); ++y) {
    QRgb *line = reinterpret_cast<QRgb*>(image.scanLine(y));
    for (int x = 0; x < image.width(); ++x) {
        QRgb &rgb = line[x];
        rgb = qRgba(qRed(rgb), qGreen(0), qBlue(rgb), qAlpha(rgb));
    }
}
							

警告: If you are accessing 32-bpp image data, cast the returned pointer to QRgb* ( QRgb has a 32-bit size) and use it to read/write the pixel value. You cannot use the uchar* pointer directly, because the pixel format depends on the byte order on the underlying platform. Use qRed(), qGreen(), qBlue(), and qAlpha() to access the pixels.

另请参阅 bytesPerLine (), bits (), 像素操纵 ,和 constScanLine ().

const uchar *QImage:: scanLine ( int i ) const

这是重载函数。

void QImage:: setAlphaChannel (const QImage & alphaChannel )

将此图像的 Alpha 通道设为给定 alphaChannel .

alphaChannel is an 8 bit alpha image, the alpha values are used directly. Otherwise, alphaChannel is converted to 8 bit grayscale and the intensity of the pixel values is used.

If the image already has an alpha channel, the existing alpha channel is multiplied with the new one. If the image doesn't have an alpha channel it will be converted to a format that does.

操作类似于描绘 alphaChannel as an alpha image over this image using QPainter::CompositionMode_DestinationIn .

另请参阅 hasAlphaChannel (), 图像变换 ,和 图像格式 .

void QImage:: setColor ( int index , QRgb colorValue )

设置颜色在给定 index 在颜色表中,到给定 colorValue 。颜色值是 ARGB 四元组。

index 超出颜色表当前大小,展开它采用 setColorCount ().

另请参阅 color (), colorCount (), setColorTable (),和 像素操纵 .

void QImage:: setColorCount ( int colorCount )

重置颜色表大小以包含 colorCount 条目。

若颜色表被展开,所有额外颜色将被设为透明 (即 qRgba (0, 0, 0, 0)).

When the image is used, the color table must be large enough to have entries for all the pixel/index values present in the image, otherwise the results are undefined.

另请参阅 colorCount (), colorTable (), setColor (),和 图像变换 .

[since 5.14] void QImage:: setColorSpace (const QColorSpace & colorSpace )

将图像色彩空间设为 colorSpace 不对图像数据履行任何转换。

该函数在 Qt 5.14 引入。

另请参阅 colorSpace ().

void QImage:: setColorTable (const QList < QRgb > & colors )

将用于翻译颜色索引的颜色表设为 QRgb 值,到指定 colors .

When the image is used, the color table must be large enough to have entries for all the pixel/index values present in the image, otherwise the results are undefined.

另请参阅 colorTable (), setColor (),和 图像变换 .

void QImage:: setDevicePixelRatio ( qreal scaleFactor )

Sets the device pixel ratio for the image. This is the ratio between image pixels and device-independent pixels.

默认 scaleFactor is 1.0. Setting it to something else has two effects:

QPainters that are opened on the image 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 image size will take the ratio into account: QSize layoutSize = image. size () / image. devicePixelRatio () The net effect of this is that the image is displayed as high-DPI image rather than a large image (see 绘制高分辨率版本的像素图和图像 ).

另请参阅 devicePixelRatio ().

void QImage:: setDotsPerMeterX ( int x )

Sets the number of pixels that fit horizontally in a physical meter, to x .

Together with dotsPerMeterY (), this number defines the intended scale and aspect ratio of the image, and determines the scale at which QPainter will draw graphics on the image. It does not change the scale or aspect ratio of the image when it is rendered on other paint devices.

另请参阅 dotsPerMeterX () 和 图像信息 .

void QImage:: setDotsPerMeterY ( int y )

Sets the number of pixels that fit vertically in a physical meter, to y .

Together with dotsPerMeterX (), this number defines the intended scale and aspect ratio of the image, and determines the scale at which QPainter will draw graphics on the image. It does not change the scale or aspect ratio of the image when it is rendered on other paint devices.

另请参阅 dotsPerMeterY () 和 图像信息 .

void QImage:: setOffset (const QPoint & offset )

Sets the number of pixels by which the image is intended to be offset by when positioning relative to other images, to offset .

另请参阅 offset () 和 图像信息 .

void QImage:: setPixel (const QPoint & position , uint index_or_rgb )

设置像素索引或颜色在给定 position to index_or_rgb .

If the image's format is either monochrome or paletted, the given index_or_rgb value must be an index in the image's color table, otherwise the parameter must be a QRgb 值。

position is not a valid coordinate pair in the image, or if index_or_rgb >= colorCount () in the case of monochrome and paletted images, the result is undefined.

警告: This function is expensive due to the call of the internal detach() function called within; if performance is a concern, we recommend the use of scanLine () 或 bits () to access pixel data directly.

另请参阅 pixel () 和 像素操纵 .

void QImage:: setPixel ( int x , int y , uint index_or_rgb )

这是重载函数。

设置像素索引或颜色在 ( x , y ) 到 index_or_rgb .

[since 5.6] void QImage:: setPixelColor (const QPoint & position , const QColor & color )

设置颜色在给定 position to color .

position is not a valid coordinate pair in the image, or the image's format is either monochrome or paletted, the result is undefined.

警告: This function is expensive due to the call of the internal detach() function called within; if performance is a concern, we recommend the use of scanLine () 或 bits () to access pixel data directly.

该函数在 Qt 5.6 引入。

另请参阅 pixelColor (), pixel (), bits (), scanLine (),和 像素操纵 .

[since 5.6] void QImage:: setPixelColor ( int x , int y , const QColor & color )

这是重载函数。

设置像素颜色在 ( x , y ) 到 color .

该函数在 Qt 5.6 引入。

void QImage:: setText (const QString & key , const QString & text )

将图像文本设为给定 text 并将其关联到给定 key .

若仅仅想要存储单文本块 (即:comment 或仅仅描述),可以传递空键,或使用像 Description 的一般键。

图像文本被嵌入图像数据当调用 save () 或 QImageWriter::write ().

并非所有图像格式都支持嵌入文本。可以找出哪些特定图像或格式支持嵌入文本通过使用 QImageWriter::supportsOption ()。给出范例:

    QImageWriter writer;
    writer.setFormat("png");
    if (writer.supportsOption(QImageIOHandler::Description))
        qDebug() << "Png supports embedded text";
							

可以使用 QImageWriter::supportedImageFormats () 以找出哪些图像格式是可用的。

另请参阅 text () 和 textKeys ().

QSize QImage:: size () const

返回图片大小,即其 width () 和 height ().

另请参阅 图像信息 .

[since 5.10] qsizetype QImage:: sizeInBytes () const

返回图像数据大小 (以字节为单位)。

该函数在 Qt 5.10 引入。

另请参阅 bytesPerLine (), bits (),和 图像信息 .

void QImage:: swap ( QImage & other )

交换图像 other 与此图像。此操作非常快且从不失败。

QString QImage:: text (const QString & key = QString()) const

返回的图像文本关联给定 key 。若指定 key 是空字符串,返回整个图像文本,每个键文本对由换行符分隔。

另请参阅 setText () 和 textKeys ().

QStringList QImage:: textKeys () const

返回此图像的文本键。

You can use these keys with text () to list the image text for a certain key.

另请参阅 text ().

CGImageRef QImage:: toCGImage () const

创建 CGImage 等效于此 QImage .

返回 CGImageRef 句柄。

The returned CGImageRef partakes in the QImage implicit sharing, and holds a reference to the QImage data. CGImage is immutable and will never detach the QImage . Writing to the QImage will detach as usual.

此函数很快,且不会拷贝或转换图像数据。

支持以下图像格式,且会映射到相应本机图像类型:

Qt CoreGraphics
Format_ARGB32 kCGImageAlphaFirst | kCGBitmapByteOrder32Host
Format_RGB32 kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Host
Format_RGBA8888_Premultiplied kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big
Format_RGBA8888 kCGImageAlphaLast | kCGBitmapByteOrder32Big
Format_RGBX8888 kCGImageAlphaNoneSkipLast | kCGBitmapByteOrder32Big
Format_ARGB32_Premultiplied kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host

Other formats are not supported; this function returns a null CGImageRef for those cases. Users of this function may then convert the QImage to a supported formate first, for example Format_ARGB32_Premultiplied .

CGImageRef 颜色空间被设为 sRGB 颜色空间。

[since 6.0] HBITMAP QImage:: toHBITMAP () const

创建 HBITMAP 相当于 QImage .

返回 HBITMAP 句柄。

调用者负责释放 HBITMAP 数据在使用后。

For usage with with standard GDI calls, such as BitBlt() , the image should have the format QImage::Format_RGB32 .

When using the resulting HBITMAP for the AlphaBlend() GDI function, the image should have the format QImage::Format_ARGB32_Premultiplied (use convertToFormat ()).

When using the resulting HBITMAP as application icon or a systray icon, the image should have the format QImage::Format_ARGB32 .

该函数在 Qt 6.0 引入。

另请参阅 fromHBITMAP () 和 convertToFormat ().

[since 6.0] HICON QImage:: toHICON (const QImage & mask = {}) const

创建 HICON 相当于 QPixmap , applying the mask mask .

mask is not null, it needs to be of format QImage::Format_Mono . Returns the HICON 句柄。

调用者负责释放 HICON 数据在使用后。

该函数在 Qt 6.0 引入。

另请参阅 fromHICON ().

[static] QImage::Format QImage:: toImageFormat ( QPixelFormat format )

转换 format QImage::Format

[static] QPixelFormat QImage:: toPixelFormat ( QImage::Format format )

转换 format QPixelFormat

QImage QImage:: transformed (const QTransform & matrix , Qt::TransformationMode mode = Qt::FastTransformation) const

Returns a copy of the image that is transformed using the given transformation matrix and transformation mode .

The returned image will normally have the same {Image Formats}{format} as the original image. However, a complex transformation may result in an image where not all pixels are covered by the transformed pixels of the original image. In such cases, those background pixels will be assigned a transparent color value, and the transformed image will be given a format with an alpha channel, even if the orginal image did not have that.

The transformation matrix is internally adjusted to compensate for unwanted translation; i.e. the image produced is the smallest image that contains all the transformed points of the original image. Use the trueMatrix () function to retrieve the actual matrix used for transforming an image.

Unlike the other overload, this function can be used to perform perspective transformations on images.

另请参阅 trueMatrix () 和 图像变换 .

[static] QTransform QImage:: trueMatrix (const QTransform & matrix , int width , int height )

返回用于变换图像的实际矩阵采用给定 width , height and matrix .

When transforming an image using the transformed () function, the transformation matrix is internally adjusted to compensate for unwanted translation, i.e. transformed () returns the smallest image containing all transformed points of the original image. This function returns the modified matrix, which maps points correctly from the original image into the new image.

Unlike the other overload, this function creates transformation matrices that can be used to perform perspective transformations on images.

另请参阅 transformed () 和 图像变换 .

bool QImage:: valid (const QPoint & pos ) const

返回 true if pos 是图像中的有效坐标对;否则返回 false .

另请参阅 rect () 和 QRect::contains ().

bool QImage:: valid ( int x , int y ) const

这是重载函数。

返回 true if QPoint ( x , y ) 是图像中的有效坐标对;否则返回 false .

int QImage:: width () const

返回图像宽度。

另请参阅 图像信息 .

QVariant QImage:: operator QVariant () const

将图像返回作为 QVariant .

bool QImage:: operator!= (const QImage & image ) const

返回 true 若此图像和给定 image 拥有不同内容;否则返回 false .

比较可能很慢,除非有一些明显差异 (譬如:不同宽度),在这种情况下函数会快速返回。

另请参阅 operator= ().

bool QImage:: operator== (const QImage & image ) const

返回 true 若此图像和给定 image 拥有相同内容;否则返回 false .

比较可能很慢,除非有一些明显差异 (如:不同大小或格式),在这种情况下函数会快速返回。

另请参阅 operator= ().

相关非成员

[since 5.0] QImageCleanupFunction

具有以下签名的函数,可用于实现基本图像内存管理:

void myImageCleanupHandler(void *info);
							

该 typedef 在 Qt 5.0 引入。

QDataStream & operator<< ( QDataStream & stream , const QImage & image )

写入给定 image 到给定 stream 作为 PNG 图像,或作为 BMP 图像若流的版本为 1。注意:将流写入文件不会产生有效图像文件。

另请参阅 QImage::save () 和 序列化 Qt 数据类型 .

QDataStream & operator>> ( QDataStream & stream , QImage & image )

读取图像从给定 stream 并将它存储在给定 image .

另请参阅 QImage::load () 和 序列化 Qt 数据类型 .