本页内容

QPixelFormat Class

QPixelFormat 是一个用于描述图形缓冲区中不同像素布局的类。更多内容...

头文件: #include <QPixelFormat>
CMake: find_package(Qt6 REQUIRED COMPONENTS Gui)
target_link_libraries(mytarget PRIVATE Qt6::Gui)
qmake: QT += gui

公共类型

enum AlphaPosition { AtBeginning, AtEnd }
enum AlphaPremultiplied { NotPremultiplied, Premultiplied }
enum AlphaUsage { IgnoresAlpha, UsesAlpha }
enum ByteOrder { LittleEndian, BigEndian, CurrentSystemEndian }
enum ColorModel { RGB, BGR, Indexed, Grayscale, CMYK, …, Alpha }
enum TypeInterpretation { UnsignedInteger, UnsignedShort, UnsignedByte, FloatingPoint }
enum YUVLayout { YUV444, YUV422, YUV411, YUV420P, YUV420SP, …, Y16 }

公共函数

QPixelFormat()
QPixelFormat(QPixelFormat::ColorModel colorModel, uchar firstSize, uchar secondSize, uchar thirdSize, uchar fourthSize, uchar fifthSize, uchar alphaSize, QPixelFormat::AlphaUsage alphaUsage, QPixelFormat::AlphaPosition alphaPosition, QPixelFormat::AlphaPremultiplied premultiplied, QPixelFormat::TypeInterpretation typeInterpretation, QPixelFormat::ByteOrder byteOrder = CurrentSystemEndian, uchar subEnum = 0)
QPixelFormat::AlphaPosition alphaPosition() const
uchar alphaSize() const
QPixelFormat::AlphaUsage alphaUsage() const
uchar bitsPerPixel() const
uchar blackSize() const
uchar blueSize() const
uchar brightnessSize() const
QPixelFormat::ByteOrder byteOrder() const
uchar channelCount() const
QPixelFormat::ColorModel colorModel() const
uchar cyanSize() const
uchar greenSize() const
uchar hueSize() const
uchar lightnessSize() const
uchar magentaSize() const
QPixelFormat::AlphaPremultiplied premultiplied() const
uchar redSize() const
uchar saturationSize() const
QPixelFormat::TypeInterpretation typeInterpretation() const
uchar yellowSize() const
QPixelFormat::YUVLayout yuvLayout() const
QPixelFormat qPixelFormatAlpha(uchar channelSize, QPixelFormat::TypeInterpretation typeInterpretation = QPixelFormat::UnsignedInteger)
QPixelFormat qPixelFormatCmyk(uchar channelSize, uchar alphaSize = 0, QPixelFormat::AlphaUsage alphaUsage = QPixelFormat::IgnoresAlpha, QPixelFormat::AlphaPosition alphaPosition = QPixelFormat::AtBeginning, QPixelFormat::TypeInterpretation typeInterpretation = QPixelFormat::UnsignedInteger)
QPixelFormat qPixelFormatGrayscale(uchar channelSize, QPixelFormat::TypeInterpretation typeInterpretation = QPixelFormat::UnsignedInteger)
QPixelFormat qPixelFormatHsl(uchar channelSize, uchar alphaSize = 0, QPixelFormat::AlphaUsage alphaUsage = QPixelFormat::IgnoresAlpha, QPixelFormat::AlphaPosition alphaPosition = QPixelFormat::AtBeginning, QPixelFormat::TypeInterpretation typeInterpretation = QPixelFormat::FloatingPoint)
QPixelFormat qPixelFormatHsv(uchar channelSize, uchar alphaSize = 0, QPixelFormat::AlphaUsage alphaUsage = QPixelFormat::IgnoresAlpha, QPixelFormat::AlphaPosition alphaPosition = QPixelFormat::AtBeginning, QPixelFormat::TypeInterpretation typeInterpretation = QPixelFormat::FloatingPoint)
QPixelFormat qPixelFormatRgba(uchar redSize, uchar greenSize, uchar blueSize, uchar alphaSize, QPixelFormat::AlphaUsage alphaUsage, QPixelFormat::AlphaPosition alphaPosition, QPixelFormat::AlphaPremultiplied premultiplied = QPixelFormat::NotPremultiplied, QPixelFormat::TypeInterpretation typeInterpretation = QPixelFormat::UnsignedInteger)
QPixelFormat qPixelFormatYuv(QPixelFormat::YUVLayout yuvLayout, uchar alphaSize = 0, QPixelFormat::AlphaUsage alphaUsage = QPixelFormat::IgnoresAlpha, QPixelFormat::AlphaPosition alphaPosition = QPixelFormat::AtBeginning, QPixelFormat::AlphaPremultiplied premultiplied = QPixelFormat::NotPremultiplied, QPixelFormat::TypeInterpretation typeInterpretation = QPixelFormat::UnsignedByte, QPixelFormat::ByteOrder byteOrder = QPixelFormat::BigEndian)

详细说明

在 Qt 中,通常需要表示图形缓冲区中像素的布局。QPixelFormat 最多可描述 5 个颜色通道和 1 个透明度通道,包括这些通道在内存中如何单独表示以及彼此之间的关系等详细信息。

typeInterpretation() 和byteOrder() 确定每个像素应如何被读取/解释,而alphaSize()、alphaUsage()、alphaPosition() 以及premultiplied() 则描述了可能存在的Alpha通道的位置及其属性。

不支持描述 YUV 的宏像素。取而代之的是提供了一个YUV formats 列表。当 QPixelFormat 描述 YUV 格式时,bitsPerPixel() 的值将根据 YUV 布局推导得出。

成员类型文档

enum QPixelFormat::AlphaPosition

此枚举描述了像素格式的alpha position 。

常量值描述
QPixelFormat::AtBeginning0Alpha通道将置于颜色通道之前。例如:ARGB。
QPixelFormat::AtEnd1Alpha通道将位于颜色通道之后。例如:ARGB。

另请参阅 alphaSize()、alphaUsage() 和premultiplied()。

enum QPixelFormat::AlphaPremultiplied

该枚举描述了像素格式的Alpha通道是否被premultiplied 到颜色通道中。

常量值描述
QPixelFormat::NotPremultiplied0alpha 通道未乘入颜色通道。
QPixelFormat::Premultiplied1Alpha 通道被乘入颜色通道。

另请参阅 alphaSize()、alphaUsage() 和alphaPosition()。

enum QPixelFormat::AlphaUsage

此枚举描述了像素格式的alpha usage 。

常量值描述
QPixelFormat::IgnoresAlpha1未使用 alpha 通道。
QPixelFormat::UsesAlpha0使用 alpha 通道。

另请参阅 alphaSize()、alphaPosition() 和premultiplied()。

enum QPixelFormat::ByteOrder

此枚举描述了像素格式的byte order 。

常量值描述
QPixelFormat::LittleEndian0字节顺序为小端序。
QPixelFormat::BigEndian1字节序为大端序。
QPixelFormat::CurrentSystemEndian2该枚举不会被存储,而是在构造函数中转换为与当前系统枚举相匹配的字节序枚举。

另请参阅 typeInterpretation()。

enum QPixelFormat::ColorModel

此枚举描述了像素格式的color model 。

常量值描述
QPixelFormat::RGB0颜色模型为 RGB。
QPixelFormat::BGR1从逻辑上讲,这是 RGB 的反字节序版本。但是,为了便于使用,它拥有自己的模型。
QPixelFormat::Indexed2该颜色模型使用调色板。
QPixelFormat::Grayscale3该颜色模型为灰度。
QPixelFormat::CMYK4该颜色模型是 CMYK。
QPixelFormat::HSL5该颜色模型是 HSL。
QPixelFormat::HSV6该颜色模型是 HSV。
QPixelFormat::YUV7该颜色模型是 YUV。
QPixelFormat::Alpha (since Qt 5.5)8没有颜色模型,仅使用透明度。

enum QPixelFormat::TypeInterpretation

此枚举描述了像素格式的type interpretation 。

常量值描述
QPixelFormat::UnsignedInteger0应将像素读取为一个或多个unsigned int 。
QPixelFormat::UnsignedShort1应将像素读取为一个或多个unsigned short 。
QPixelFormat::UnsignedByte2像素应被读取为一个或多个byte 。
QPixelFormat::FloatingPoint3像素应被读取为一个或多个浮点数,其具体类型由颜色/阿尔法通道定义,即 16 位半精度浮点格式为qfloat16 ,32 位全精度浮点格式为float 。

另请参阅 byteOrder()。

enum QPixelFormat::YUVLayout

该枚举描述了像素格式的YUV layout ,前提是其颜色模型为QPixelFormat::YUV 。

常量值
QPixelFormat::YUV4440
QPixelFormat::YUV4221
QPixelFormat::YUV4112
QPixelFormat::YUV420P3
QPixelFormat::YUV420SP4
QPixelFormat::YV125
QPixelFormat::UYVY6
QPixelFormat::YUYV7
QPixelFormat::NV128
QPixelFormat::NV219
QPixelFormat::IMC110
QPixelFormat::IMC211
QPixelFormat::IMC312
QPixelFormat::IMC413
QPixelFormat::Y814
QPixelFormat::Y1615

成员函数文档

[constexpr noexcept] QPixelFormat::QPixelFormat()

创建一个空像素格式。该格式映射到QImage::Format_Invalid 。

[constexpr noexcept] QPixelFormat::QPixelFormat(QPixelFormat::ColorModel colorModel, uchar firstSize, uchar secondSize, uchar thirdSize, uchar fourthSize, uchar fifthSize, uchar alphaSize, QPixelFormat::AlphaUsage alphaUsage, QPixelFormat::AlphaPosition alphaPosition, QPixelFormat::AlphaPremultiplied premultiplied, QPixelFormat::TypeInterpretation typeInterpretation, QPixelFormat::ByteOrder byteOrder = CurrentSystemEndian, uchar subEnum = 0)

创建一个 QPixelFormat,将其数据赋值给属性。colorModel 将被放入一个长度为 4 位的缓冲区中。

firstSize secondSize thirdSize 这些参数均用于表示通道的大小。各通道的具体用途将根据 而定。对于RGB模式,firstSize将表示红色通道;对于CMYK模式,则表示青色通道的值。fourthSize fifthSize alphaSize colorModel

alphaUsage 表示是否使用 alpha 通道。

alphaPosition 是 alpha 通道的位置。

premultiplied 表示 alpha 通道是否已与颜色通道相乘。

typeInterpretation 表示像素的解释方式。

byteOrder 表示像素格式的字节序。默认值为CurrentSystemEndian ,对于非字节顺序格式,该值将解析为系统的字节序;对于QPixelFormat::UnsignedByte ,则解析为QPixelFormat::BigEndian 。

subEnum 用于需要通过提供额外枚举来存储某些附加信息的色彩模型。YUV 会使用此参数来存储 YUV 类型,默认值为 0。

注意:BGR 格式有其专用的颜色模型,不应通过使用与RGB格式相反的字节序来描述。

[constexpr noexcept] QPixelFormat::AlphaPosition QPixelFormat::alphaPosition() const

用于获取 alpha 通道相对于颜色通道位置的访问器函数。

对于各通道分别映射到独立单位的格式,Alpha 位置是相对于这些单位而言的。例如,对于QImage::Format_RGBA16FPx4 这种格式,其 Alpha 位置为QPixelFormat::AtEnd ,此时 Alpha 即为最后读取的qfloat16 。

对于将多个通道打包到单个单元中的格式,QPixelFormat::AtBeginning 和QPixelFormat::AtEnd 的值会映射到打包单元的高位和低位,具体取决于该格式自身的byteOrder()。

例如,对于QImage::Format_ARGB32 ,其类型解释为QPixelFormat::UnsignedInteger ,且byteOrder() 始终与主机系统匹配,因此QPixelFormat::AtBeginning 的 alpha 位置意味着 alpha 总可在0xFF000000 处找到。

如果像素格式与主机字节序不匹配,则必须谨慎操作,以确保将像素格式布局正确映射到主机内存布局。

另请参阅 alphaUsage()、alphaSize() 和premultiplied()。

[constexpr noexcept] uchar QPixelFormat::alphaSize() const

用于获取Alpha通道大小的访问器函数。

[constexpr noexcept] QPixelFormat::AlphaUsage QPixelFormat::alphaUsage() const

用于判断是否使用阿尔法通道的访问器函数。

有时像素格式会为 alpha 通道预留空间,因此 `alphaSize()` 会返回大于 0 的值,但该 alpha 通道并未被使用或被忽略。

例如,对于QImage::Format_RGB32 ,其bitsPerPixel()值为32,因为Alpha通道的大小为8,但alphaUsage()返回QPixelFormat::IgnoresAlpha 。

请注意,在这种情况下,未使用的 alpha 通道的position 仍然很重要,因为它会影响颜色通道的放置。

另请参阅 alphaPosition()、alphaSize() 和premultiplied()。

[constexpr noexcept] uchar QPixelFormat::bitsPerPixel() const

用于获取每个像素所用位数的辅助函数。该函数返回所有颜色通道的总和加上Alpha通道的大小。

[constexpr noexcept] uchar QPixelFormat::blackSize() const

黑色/关键色通道的辅助函数。

[constexpr noexcept] uchar QPixelFormat::blueSize() const

用于获取蓝色通道大小的辅助函数。

[constexpr noexcept] uchar QPixelFormat::brightnessSize() const

用于亮度通道大小的访问器函数。

[constexpr noexcept] QPixelFormat::ByteOrder QPixelFormat::byteOrder() const

像素格式的字节顺序决定了各个类型单元的内存布局,具体如typeInterpretation()中所描述的那样。

该函数永远不会返回QPixelFormat::CurrentSystemEndian ,因为该值会在构造函数中被转换为系统的字节序值。

对于typeInterpretation()QPixelFormat::UnsignedByte 的像素格式,该值通常为QPixelFormat::BigEndian ,而其他类型的解释通常会反映当前系统的字节序。

如果像素格式的字节顺序与当前系统匹配,则可以使用相同的位掩码和操作来读取和处理各个类型单元,而与主机系统的字节序无关。例如,对于具有QPixelFormat::UnsignedInteger 类型解释的QImage::Format_ARGB32 ,无论主机字节序如何,都可以通过用0xFF000000 对unsigned int 进行掩码来读取 alpha 通道。

如果像素格式与主机字节序不匹配,则必须特别注意这一点。在这种情况下,诸如QImage 之类的类不会交换内部位来匹配主机系统的字节序。

另请参阅 typeInterpretation() 和alphaPosition()。

[constexpr noexcept] uchar QPixelFormat::channelCount() const

用于获取通道数的访问函数。

通道数表示大小大于 0 的通道(颜色和透明度)。

[constexpr noexcept] QPixelFormat::ColorModel QPixelFormat::colorModel() const

颜色模型的访问器函数。

请注意,对于QPixelFormat::YUV ,无法单独描述各个宏像素。取而代之的是提供一个YUV formats 列表,并根据YUV布局推导出bitsPerPixel()的值。

[constexpr noexcept] uchar QPixelFormat::cyanSize() const

青色通道的辅助函数。

[constexpr noexcept] uchar QPixelFormat::greenSize() const

用于获取绿色通道大小的辅助函数。

[constexpr noexcept] uchar QPixelFormat::hueSize() const

用于色相通道大小的访问器函数。

[constexpr noexcept] uchar QPixelFormat::lightnessSize() const

用于亮度通道大小的辅助函数。

[constexpr noexcept] uchar QPixelFormat::magentaSize() const

magenta 颜色通道的 Accessor 函数。

[constexpr noexcept] QPixelFormat::AlphaPremultiplied QPixelFormat::premultiplied() const

用于判断Alpha通道是否与颜色通道相乘的辅助函数。

[constexpr noexcept] uchar QPixelFormat::redSize() const

用于获取红色通道大小的辅助函数。

[constexpr noexcept] uchar QPixelFormat::saturationSize() const

用于获取饱和通道大小的辅助函数。

[constexpr noexcept] QPixelFormat::TypeInterpretation QPixelFormat::typeInterpretation() const

类型解释决定了应如何读取每个像素。

每个像素由一个或多个指定类型的单元表示,这些单元在内存中按顺序排列。

注意: 像素格式的 byte order 和主机系统的字节序仅影响被读取的每个单独单位的内存布局,而不影响这些单位之间的相对顺序。

例如,QImage::Format_Mono 的像素深度(pixel format )为每像素 1 位,类型解释(type interpretation)为QPixelFormat::UnsignedByte ,应作为单个byte 读取。同样,QImage::Format_RGB888 的像素深度(pixel format )为每像素 24 位,类型解释(type interpretation)为QPixelFormat::UnsignedByte ,应作为三个连续的byte读取。

许多QImage formats 均为32位,其类型解释为QPixelFormat::UnsignedInteger ,应解读为单个unsigned int 。

对于QPixelFormat::FloatingPoint 格式(如QImage::Format_RGBA16FPx4 或QImage::Format_RGBA32FPx4 ),其类型根据各个颜色/Alpha 通道的大小确定:16 位半精度浮点格式为qfloat16 ,32 位全精度浮点格式为float 。

另请参阅 byteOrder()。

[constexpr noexcept] uchar QPixelFormat::yellowSize() const

黄色色通的辅助函数。

[constexpr noexcept] QPixelFormat::YUVLayout QPixelFormat::yuvLayout() const

YUVLayout 的辅助函数。由于 YUV 色彩模型使用宏像素,因此很难描述 YUV 像素格式的颜色通道。相反,像素的布局被存储为一个枚举类型。

相关非成员

[constexpr noexcept] QPixelFormat qPixelFormatAlpha(uchar channelSize, QPixelFormat::TypeInterpretation typeInterpretation = QPixelFormat::UnsignedInteger)

用于创建Alpha格式的构造函数。可以通过向channelSize 传递1来描述蒙版格式。此外,还可以通过将channelSize 设为8,并将FloatingPoint 设为typeInterpretation ,使用double类型描述每个像素,从而定义非常精确的Alpha格式。

另请参阅 QPixelFormat::TypeInterpretation 。

[constexpr noexcept] QPixelFormat qPixelFormatCmyk(uchar channelSize, uchar alphaSize = 0, QPixelFormat::AlphaUsage alphaUsage = QPixelFormat::IgnoresAlpha, QPixelFormat::AlphaPosition alphaPosition = QPixelFormat::AtBeginning, QPixelFormat::TypeInterpretation typeInterpretation = QPixelFormat::UnsignedInteger)

用于创建 CMYK 格式的构造函数。通道数为 4 或 5,具体取决于 `alphaSize ` 是否大于零。CMYK 颜色通道都将被设置为 `channelSize` 的值。

alphaUsage alphaPosition 以及 均可通过同名的访问器进行访问。typeInterpretation

另请参阅 QPixelFormat::TypeInterpretation 。

[constexpr noexcept] QPixelFormat qPixelFormatGrayscale(uchar channelSize, QPixelFormat::TypeInterpretation typeInterpretation = QPixelFormat::UnsignedInteger)

用于创建灰度格式的构造函数。通过向channelSize 传入1,可以定义单色格式。此外,还可以通过将channelSize 设为8,并将FloatingPoint 设为typeInterpretation ,使用double类型描述每个像素,从而定义非常精确的灰度格式。

另请参阅 QPixelFormat::TypeInterpretation 。

[constexpr noexcept] QPixelFormat qPixelFormatHsl(uchar channelSize, uchar alphaSize = 0, QPixelFormat::AlphaUsage alphaUsage = QPixelFormat::IgnoresAlpha, QPixelFormat::AlphaPosition alphaPosition = QPixelFormat::AtBeginning, QPixelFormat::TypeInterpretation typeInterpretation = QPixelFormat::FloatingPoint)

用于创建 HSL 格式的构造函数。通道数为 3 或 4,具体取决于 `alphaSize ` 是否大于 0。

channelSize 将把hueSize 、saturationSize 和lightnessSize 设置为相同的值。

alphaUsage alphaPosition 以及 均可通过同名的访问器进行访问。typeInterpretation

[constexpr noexcept] QPixelFormat qPixelFormatHsv(uchar channelSize, uchar alphaSize = 0, QPixelFormat::AlphaUsage alphaUsage = QPixelFormat::IgnoresAlpha, QPixelFormat::AlphaPosition alphaPosition = QPixelFormat::AtBeginning, QPixelFormat::TypeInterpretation typeInterpretation = QPixelFormat::FloatingPoint)

用于创建HSV格式的构造函数。通道数为3或4,具体取决于alphaSize 是否大于0。

channelSize 将把hueSize 、saturationSize 和brightnessSize 设置为相同的值。

alphaUsage alphaPosition 以及 均可通过同名的访问器进行访问。typeInterpretation

[constexpr noexcept] QPixelFormat qPixelFormatRgba(uchar redSize, uchar greenSize, uchar blueSize, uchar alphaSize, QPixelFormat::AlphaUsage alphaUsage, QPixelFormat::AlphaPosition alphaPosition, QPixelFormat::AlphaPremultiplied premultiplied = QPixelFormat::NotPremultiplied, QPixelFormat::TypeInterpretation typeInterpretation = QPixelFormat::UnsignedInteger)

一个用于创建RGB像素格式的构造函数。redSize 、greenSize 、blueSize 分别表示各颜色通道的大小。alphaSize 描述了Alpha通道的大小,其位置由alphaPosition 指定。alphaUsage 用于确定是否使用Alpha通道。 将 alpha 通道大小设置为 8,并将alphaUsage 设置为IgnoresAlpha ,即可创建一种 32 位格式,其中 rgb 通道合计仅使用 24 位。premultiplied typeInterpretation 可通过同名的访问器进行访问。

另请参阅 QPixelFormat::TypeInterpretation 。

QPixelFormat qPixelFormatYuv(QPixelFormat::YUVLayout yuvLayout, uchar alphaSize = 0, QPixelFormat::AlphaUsage alphaUsage = QPixelFormat::IgnoresAlpha, QPixelFormat::AlphaPosition alphaPosition = QPixelFormat::AtBeginning, QPixelFormat::AlphaPremultiplied premultiplied = QPixelFormat::NotPremultiplied, QPixelFormat::TypeInterpretation typeInterpretation = QPixelFormat::UnsignedByte, QPixelFormat::ByteOrder byteOrder = QPixelFormat::BigEndian)

用于创建描述YUV格式的QPixelFormat 的构造函数,其参数为yuvLayout 。alphaSize 描述了潜在Alpha通道的大小,其位置由alphaPosition 指定。“first”、“second”……“fifth”通道均被设置为0。alphaUsage 、premultiplied 、typeInterpretation 和byteOrder 的功能与其他格式相同。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.