QBitmap Class
QBitmap 类提供单色(1 位色深)的位图。更多内容...
| 头文件: | #include <QBitmap> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Gui) target_link_libraries(mytarget PRIVATE Qt6::Gui) |
| qmake: | QT += gui |
| 继承自: | QPixmap |
- 所有成员列表,包括继承的成员
- 已弃用的成员
- QBitmap 属于绘图类和隐式共享类。
公共函数
| QBitmap() | |
| QBitmap(const QSize &size) | |
| QBitmap(const QString &fileName, const char *format = nullptr) | |
| QBitmap(int width, int height) | |
| void | clear() |
| void | swap(QBitmap &other) |
| QBitmap | transformed(const QTransform &matrix) const |
| operator QVariant() const |
静态公共成员
| QBitmap | fromData(const QSize &size, const uchar *bits, QImage::Format monoFormat = QImage::Format_MonoLSB) |
| QBitmap | fromImage(const QImage &image, Qt::ImageConversionFlags flags = Qt::AutoColor) |
| QBitmap | fromImage(QImage &&image, Qt::ImageConversionFlags flags = Qt::AutoColor) |
(since 6.0) QBitmap | fromPixmap(const QPixmap &pixmap) |
详细说明
QBitmap 类是一个单色离屏绘图设备,主要用于创建自定义的 `QCursor ` 和 `QBrush ` 对象、构建 `QRegion ` 对象,以及为位图和小部件设置蒙版。
QBitmap 是QPixmap 的子类,确保深度为 1,但深度为 0 的空对象除外。如果将深度大于 1 的位图分配给一个位图,该位图将自动进行抖动处理。
在 QBitmap 对象(或深度为 1 的QPixmap 对象)上绘制时,请使用QColor 对象Qt::color0 和Qt::color1 。
使用Qt::color0 绘制会将位图的位设置为0,而使用Qt::color1 绘制则将位设置为1。对于位图,0位表示背景(或透明像素),1位表示前景(或不透明像素)。 使用clear()函数将所有位设置为Qt::color0 。请注意,使用Qt::black 和Qt::white 颜色没有意义,因为QColor::pixel()的值对于黑色不一定是0,对于白色不一定是1。
QBitmap 类提供了transformed() 函数,该函数返回位图的变换副本;使用QTransform 参数可对位图进行平移、缩放、剪切和旋转。此外,QBitmap 还提供了静态函数fromData(),该函数返回根据给定的uchar 数据构建的位图,以及静态函数fromImage(),该函数返回QImage 对象的转换副本。
另请参阅 QPixmap 、QImage 、QImageReader 以及QImageWriter 。
成员函数文档
QBitmap::QBitmap()
构建一个空位图。
另请参阅 QPixmap::isNull()。
[explicit] QBitmap::QBitmap(const QSize &size)
根据给定的size 构建一个位图。位图中的像素未初始化。
另请参阅 clear()。
[explicit] QBitmap::QBitmap(const QString &fileName, const char *format = nullptr)
根据给定的fileName 构建一个位图。如果文件不存在,或者格式未知,则该位图将为空位图。
fileName 和format 参数将传递给QPixmap::load()函数。如果文件格式中每个像素使用超过1位,生成的位图将自动进行抖动处理。
另请参阅 QPixmap::isNull() 和QImageReader::imageFormat()。
QBitmap::QBitmap(int width, int height)
根据给定的width 和height 构建一个位图。位图内部的像素未初始化。
另请参阅 clear()。
void QBitmap::clear()
清除位图,将所有位设置为Qt::color0 。
[static] QBitmap QBitmap::fromData(const QSize &size, const uchar *bits, QImage::Format monoFormat = QImage::Format_MonoLSB)
根据给定的size 构建一个位图,并将内容设置为提供的bits 。
位图数据必须按字节对齐,并采用monoFormat 指定的位序。mono格式必须为QImage::Format_Mono 或QImage::Format_MonoLSB 。使用QImage::Format_Mono 来指定XBM格式的数据。
另请参阅 fromImage()。
[static] QBitmap QBitmap::fromImage(const QImage &image, Qt::ImageConversionFlags flags = Qt::AutoColor)
返回指定image 的副本,该副本已使用指定的图像转换flags 转换为位图。
另请参阅 fromData()。
[static] QBitmap QBitmap::fromImage(QImage &&image, Qt::ImageConversionFlags flags = Qt::AutoColor)
返回所给image 的副本,该副本已使用指定的图像转换flags 转换为位图。
这是一个重载函数。
另请参阅 fromData()。
[static, since 6.0] QBitmap QBitmap::fromPixmap(const QPixmap &pixmap)
返回将给定的pixmap 转换为位图后的副本。
如果像素图的深度大于 1,生成的位图将自动进行抖动处理。
该函数在 Qt 6.0 中引入。
另请参阅 QPixmap::depth()。
void QBitmap::swap(QBitmap &other)
将此位图与other 互换。此操作速度极快,且绝不会失败。
QBitmap QBitmap::transformed(const QTransform &matrix) const
返回该位图的副本,并根据给定的matrix 进行变换。
另请参阅 QPixmap::transformed()。
QBitmap::operator QVariant() const
将位图作为QVariant 返回。
© 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.