The QPixmap class is an off-screen image representation that can be used as a paint device. 更多...
继承 QPaintDevice .
Inherited by QBitmap .
The QPixmap class is an off-screen image representation that can be used as a paint device.
Qt 为处理图像数据提供了 4 个类: 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 () 函数返回 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 命令。
QPixmap 可以轻松地显示在屏幕上,使用 QLabel 或某一 QAbstractButton 子类 (譬如 QPushButton and QToolButton ). QLabel 拥有 pixmap 属性,而 QAbstractButton has an icon 特性。
In addition to the ordinary constructors, a QPixmap can be constructed using the static grabWidget () 和 grabWindow () functions which creates a QPixmap and paints the given widget, or window, into it.
QPixmap objects can be passed around by value since the QPixmap class uses implicit data sharing. For more information, see the Implicit Data Sharing documentation. QPixmap objects can also be streamed.
Note that the pixel data in a pixmap is internal and is managed by the underlying window system. Because QPixmap is a QPaintDevice 子类, QPainter can be used to draw directly onto pixmaps. Pixels can only be accessed through QPainter functions or by converting the QPixmap 到 QImage 。不管怎样, fill () function is available for initializing the entire pixmap with a given color.
有函数能转换 QImage 和 QPixmap。通常, 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. On Windows, the QPixmap class also supports conversion between HBITMAP and QPixmap. On Symbian, the QPixmap class also supports conversion between CFbsBitmap and 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 provides several ways of reading an image file: The file can be loaded when constructing the QPixmap object, or by using the load () 或 loadFromData () functions later on. When loading an image, the file name can either refer to an actual file on disk or to one of the application's embedded resources. See Qt 资源系统 overview for details on how to embed images and other resource files in the application's executable.
只需调用 save () function to save a QPixmap object.
The complete list of supported file formats are available through the QImageReader.supportedImageFormats () and QImageWriter.supportedImageFormats () functions. New file formats can be added as plugins. By default, Qt supports the following formats:
| Format | 描述 | Qt 支持 |
|---|---|---|
| BMP | Windows 位图 | 读/写 |
| GIF | Graphic Interchange Format (图形互换格式) 可选 | Read |
| JPG | Joint Photographic Experts Group (联合摄影专家组) | 读/写 |
| JPEG | Joint Photographic Experts Group (联合摄影专家组) | 读/写 |
| PNG | Portable Network Graphics (便携式网络图形) | 读/写 |
| PBM | Portable Bitmap (便携式位图) | Read |
| PGM | Portable Graymap (便携式灰度图) | Read |
| PPM | Portable Pixmap (便携式像素图) | 读/写 |
| XBM | X11 Bitmap (X11 位图) | 读/写 |
| XPM | X11 Pixmap (X11 像素图) | 读/写 |
QPixmap provides a collection of functions that can be used to obtain a variety of information about the pixmap:
| 可用函数 | |
|---|---|
| Geometry | size (), width () 和 height () functions provide information about the pixmap's size. The rect () function returns the image's enclosing rectangle. |
| Alpha 组件 |
hasAlphaChannel
() returns true
if the pixmap has a format that respects the alpha channel,
otherwise returns false. The
hasAlpha
(),
setMask
() 和
mask
() functions are legacy and should not
be used. They are potentially very slow.
createHeuristicMask () function creates and returns a 1-bpp heuristic mask (i.e. a QBitmap ) for this pixmap. It works by selecting a color from one of the corners and then chipping away pixels of that color, starting at all the edges. The createMaskFromColor () function creates and returns a mask (i.e. a QBitmap ) for the pixmap based on a given color. |
| 低级信息 |
depth
() 函数返回
the depth of the pixmap. The
defaultDepth
() function returns the
default depth, i.e. the depth used by the application on the given
screen.
cacheKey () function returns a number that uniquely identifies the contents of the QPixmap object. x11Info () function returns information about the configuration of the X display used by the screen to which the pixmap currently belongs. The x11PictureHandle () function returns the X11 Picture handle of the pixmap for XRender support. Note that the two latter functions are only available on x11. |
QPixmap 对象可以转换成 QImage 使用 toImage () 函数。同样, QImage 可以转换成 QPixmap 使用 fromImage (). If this is too expensive an operation, you can use QBitmap.fromImage () 代替。
In addition, on Windows, the QPixmap class supports conversion to and from HBITMAP: the toWinHBITMAP () function creates a HBITMAP equivalent to the QPixmap, based on the given HBitmapFormat ,并返回 the HBITMAP handle. The fromWinHBITMAP () 函数返回 a QPixmap that is equivalent to the given bitmap which has the specified format. The QPixmap class also supports conversion to and from HICON: the toWinHICON () function creates a HICON equivalent to the QPixmap, and returns the HICON handle. The fromWinHICON () 函数返回 QPixmap that is equivalent to the given icon.
In addition, on Symbian, the QPixmap class supports conversion to and from CFbsBitmap: the toSymbianCFbsBitmap () function creates CFbsBitmap equivalent to the QPixmap, based on given mode and returns a CFbsBitmap object. The fromSymbianCFbsBitmap () function returns a QPixmap that is equivalent to the given bitmap and given mode.
QPixmap supports a number of functions for creating a new pixmap that is a transformed version of the original:
scaled (), scaledToWidth () 和 scaledToHeight () functions return scaled copies of the pixmap, while the copy () function creates a QPixmap that is a plain copy of the original one.
transformed () function returns a copy of the pixmap that is transformed with the given transformation matrix and transformation mode: Internally, the transformation matrix is adjusted to compensate for unwanted translation, i.e. transformed () returns the smallest pixmap containing all transformed points of the original pixmap. 静态 trueMatrix () function returns the actual matrix used for transforming the pixmap.
注意: When using the native X11 graphics system, the pixmap becomes invalid when the QApplication instance is destroyed.
Win32 only: 此枚举定义如何转换介于 HBITMAP and QPixmap is performed.
警告: This enum is only available on Windows.
| 常量 | 值 | 描述 |
|---|---|---|
| QPixmap.NoAlpha | 0 | The alpha channel is ignored and always treated as being set to fully opaque. This is preferred if the HBITMAP is used with standard GDI calls, such as BitBlt() . |
| QPixmap.PremultipliedAlpha | 1 | HBITMAP is treated as having an alpha channel and premultiplied colors. This is preferred if the HBITMAP is accessed through the AlphaBlend() GDI 函数。 |
| QPixmap.Alpha | 2 | HBITMAP is treated as having a plain alpha channel. This is the preferred format if the HBITMAP is going to be used as an application icon or systray icon. |
另请参阅 fromWinHBITMAP () 和 toWinHBITMAP ().
构造 null 像素图。
另请参阅 isNull ().
构造像素图采用给定 width and height . If either width or height is zero, a null pixmap is constructed.
警告: This will create a QPixmap with uninitialized data. Call fill () to fill the pixmap with an appropriate color before drawing onto it with QPainter .
另请参阅 isNull ().
从文件构造像素图采用给定 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 。若 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 资源系统 overview for details on how to embed images and other resource files in the application's 可执行文件。
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.
fileName , format and flags 参数 are passed on to load (). This means that the data in fileName is not compiled into the binary. 若 fileName contains a relative path (e.g. the filename only) the relevant file must be found relative to the runtime working directory.
另请参阅 Reading and Writing Image Files .
构造像素图从给定 xpm data, which must be a valid XPM image.
错误被默默忽略。
Note that it's possible to squeeze the XPM variable a little bit by using an unusual declaration:
static const char * const start_xpm[]={ "16 15 8 1", "a c #cec6bd", ....
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.
构造像素图拷贝为给定 pixmap .
另请参阅 copy ().
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.
Replaces this pixmap's data with the given image 使用 the specified flags 来控制转换。 flags 自变量是按位 OR 的 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.
该函数在 Qt 4.7 引入。
另请参阅 fromImage ().
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 Implicit Data Sharing 文档编制。
若给定 rectangle is empty, the whole image is copied.
另请参阅 operator= (), QPixmap (),和 Pixmap 变换 .
这是重载函数。
Returns a deep copy of the subset of the pixmap that is specified by the rectangle QRect ( x , y , width , height ).
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:
QPixmap myPixmap; myPixmap->setMask(myPixmap->createHeuristicMask());
This function is slow because it involves converting to/from a QImage , and non-trivial computations.
另请参阅 QImage.createHeuristicMask () and createMaskFromColor ().
Creates and returns a mask for this pixmap based on the given maskColor 。若 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 .
另请参阅 createHeuristicMask () 和 QImage.createMaskFromColor ().
这是重载函数。
Creates and returns a mask for this pixmap based on the given maskColor . Same as calling createMaskFromColor(maskColor, Qt.MaskInColor )
另请参阅 createHeuristicMask () 和 QImage.createMaskFromColor ().
Returns the default pixmap depth used by the application.
On Windows and Mac, the default depth is always 32. On X11 and embedded, the depth of the screen will be returned by this 函数。
另请参阅 depth (), QColormap.depth (),和 像素图信息 .
返回像素图的深度。
The pixmap depth is also called bits per pixel (bpp) or bit planes of a pixmap. A null pixmap has depth 0.
另请参阅 defaultDepth () 和 像素图信息 .
从共享像素图数据分离像素图。
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.
填充像素图,采用给定 color .
The effect of this function is undefined when the pixmap is being painted on.
另请参阅 Pixmap 变换 .
Fills the pixmap with the widget 's background color or pixmap according to the given offset.
QPoint offset defines a point in widget coordinates to which the pixmap's top-left pixel will be mapped to. This is only significant if the widget has a background pixmap; otherwise the pixmap will simply be filled with the background color of the widget.
这是重载函数。
Fills the pixmap with the widget 's background color or pixmap. The given point, ( x , y ), defines an offset in widget coordinates to which the pixmap's top-left pixel will be mapped to.
转换给定 image 到像素图使用指定 flags 来控制转换。 flags argument is a bitwise-OR of the Qt.ImageConversionFlags . Passing 0 for flags 设置所有默认选项。
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 () 代替。
另请参阅 fromImageReader (), toImage (),和 像素图转换 .
创建 QPixmap from an image read directly from an imageReader 。 flags argument is a bitwise-OR of the Qt.ImageConversionFlags . Passing 0 for flags 设置所有默认选项。
在某些系统,将图像直接读取到 QPixmap 可以使用更少内存相比读取 QImage 以转换它到 QPixmap .
另请参阅 fromImage (), toImage (),和 像素图转换 .
Win32 only: 返回 QPixmap that is equivalent to the given bitmap . The conversion is 基于指定的 format .
警告: This function is only available on Windows.
另请参阅 toWinHBITMAP () 和 像素图转换 .
Win32 only: 返回 QPixmap that is equivalent to the given icon .
警告: This function is only available on Windows.
该函数在 Qt 4.6 引入。
另请参阅 toWinHICON () 和 像素图转换 .
Creates a pixmap and paints the given widget , restricted by the given rectangle , in it. If the widget has any children, then they are also painted in the appropriate positions.
If no rectangle is specified (the default) the entire widget is painted.
若 widget is 0, the specified rectangle doesn't overlap the widget's rectangle, or an error occurs, the function will return a null QPixmap . If the rectangle is a superset of the given widget , the areas outside the widget are covered with the widget's background.
This function actually asks widget to paint itself (and its children to paint themselves) by calling paintEvent() with painter redirection turned on. But QPixmap also provides the grabWindow () function which is a bit faster by grabbing pixels directly off the screen. In addition, if there are overlaying windows, grabWindow (), unlike grabWidget(), will see them.
警告: Do not grab a widget from its QWidget.paintEvent (). However, it is safe to grab a widget from another widget's paintEvent() .
另请参阅 grabWindow ().
这是重载函数。
Creates a pixmap and paints the given widget , restricted by QRect ( x , y , width , height ), in it.
警告: Do not grab a widget from its QWidget.paintEvent (). However, it is safe to grab a widget from another widget's paintEvent() .
Creates and returns a pixmap constructed by grabbing the contents of the given window 限定通过 QRect ( x , y , width , height ).
自变量 ( x , y ) specify the offset in the window, whereas ( width , height ) specify the area to be copied. If width is negative, the function copies everything to the right border of the window. If height is negative, the function copies everything to the bottom of the 窗口。
窗口系统标识符 ( WId ) can be retrieved 使用 QWidget.winId () function. The rationale for using a window identifier and not a QWidget , is to enable grabbing of windows that are not part of the application, window system frames, and so on.
The grabWindow() function grabs pixels from the screen, not from the window, i.e. if there is another window partially or entirely over the one you grab, you get pixels from the overlying window, too. The mouse cursor is generally not grabbed.
注意:在 X11 若给定 window doesn't have the same depth as the root window, and another window partially or entirely obscures the one you grab, you will not get pixels from the overlying window. The contents of the obscured areas in the pixmap will be undefined and uninitialized.
On Windows Vista and above grabbing a layered window, which is created by setting the Qt.WA_TranslucentBackground attribute, will not work. Instead grabbing the desktop widget should work.
警告: In general, grabbing an area outside the screen is not safe. This depends on the underlying window system.
另请参阅 grabWidget () 和 屏幕截图范例 .
Returns true if this pixmap has an alpha channel, or has a mask, otherwise returns false.
另请参阅 hasAlphaChannel () 和 mask ().
Returns true if the pixmap has a format that respects the alpha channel, otherwise returns false.
另请参阅 hasAlpha ().
返回像素图的高度。
另请参阅 size () 和 Pixmap Information .
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.
Returns true if this is a QBitmap ; otherwise returns false.
从文件加载像素图采用给定 fileName . Returns true if the pixmap was successfully loaded; otherwise returns false.
The loader attempts to read the pixmap using the specified format 。若 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 资源系统 overview for details on how to embed pixmaps and other resource files in the application's 可执行文件。
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; the key used is internal and can not be acquired.
另请参阅 loadFromData () 和 Reading and Writing Image Files .
Loads a pixmap from the len first bytes of the given binary data . Returns true if the pixmap was loaded successfully; otherwise returns false.
The loader attempts to read the pixmap using the specified format 。若 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.
另请参阅 load () 和 Reading and Writing Image Files .
Extracts a bitmap mask from the pixmap's alpha channel.
警告: This is potentially an expensive operation. The mask of the pixmap is extracted dynamically from the pixeldata.
另请参阅 setMask () 和 Pixmap Information .
Returns the pixmap's enclosing rectangle.
另请参阅 Pixmap Information .
把像素图保存到文件,采用给定 fileName 使用指定图像文件 format and quality factor. Returns true if successful; otherwise returns false.
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.
若 format is 0, an image format will be chosen from fileName 的后缀。
另请参阅 Reading and Writing Image Files .
这是重载函数。
此函数写入 QPixmap 到 given device 使用指定图像文件 format and quality factor. This can be used, for example, to save a pixmap directly into a QByteArray :
QPixmap pixmap;
QByteArray bytes;
QBuffer buffer(&bytes);
buffer.open(QIODevice.WriteOnly);
pixmap.save(&buffer, "PNG"); // writes pixmap into bytes in PNG format
缩放像素图到给定 size , using the aspect ratio and transformation modes specified by aspectRatioMode and transformMode .
若给定 size is empty, this function returns a null pixmap.
In some cases it can be more beneficial to draw the pixmap to a painter with a scale set rather than scaling the pixmap. This is the case when the painter is for instance based on OpenGL or when the scale factor changes rapidly.
这是重载函数。
Returns a copy of the pixmap scaled to a rectangle with the given width and height according to the given aspectRatioMode and transformMode .
若 width 或 height is zero or negative, this function returns a null pixmap.
Returns a scaled copy of the image. The returned image is scaled 到给定 height using the specified transformation mode . The width of the pixmap is automatically calculated so that the aspect ratio of the pixmap is preserved.
若 height is 0 or negative, a null pixmap is returned.
Returns a scaled copy of the image. The returned image is scaled 到给定 width using the specified transformation mode . The height of the pixmap is automatically calculated so that the aspect ratio of the pixmap is preserved.
若 width 为 0 或负数,返回 null 像素图。
This convenience function is equivalent to calling QPixmap.scroll( dx , dy , QRect ( x , y , width , height ), exposed ).
该函数在 Qt 4.6 引入。
另请参阅 QWidget.scroll () 和 QGraphicsItem.scroll ().
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.
QPixmap pixmap("background.png"); QRegion exposed; pixmap.scroll(10, 10, pixmap.rect(), &exposed);
You cannot scroll while there is an active painter on the pixmap.
该函数在 Qt 4.6 引入。
另请参阅 QWidget.scroll () 和 QGraphicsItem.scroll ().
设置遮罩位图。
此函数合并 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.
警告: 这是潜在的昂贵操作。
另请参阅 mask (), 像素图变换 , and QBitmap .
返回像素图尺寸。
另请参阅 width (), height (),和 像素图信息 .
交换像素图 other with this pixmap. This operation is very fast and never fails.
该函数在 Qt 4.8 引入。
将像素图转换成 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.
Win32 only: 创建 HBITMAP equivalent to the QPixmap , based on the given format . Returns the HBITMAP 句柄。
调用者负责释放 HBITMAP 数据在使用后。
警告: This function is only available on Windows.
另请参阅 fromWinHBITMAP () 和 像素图转换 .
Win32 only: 创建 HICON equivalent to the QPixmap . Returns the HICON 句柄。
调用者负责释放 HICON 数据在使用后。
警告: This function is only available on Windows.
该函数在 Qt 4.6 引入。
另请参阅 fromWinHICON () 和 像素图转换 .
Returns a copy of the pixmap that is transformed using the given transformation transform and transformation mode 。 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 .
另请参阅 trueMatrix () 和 Pixmap 变换 .
这是重载函数。
This convenience function loads the matrix 成 QTransform and calls the overloaded 函数。
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.
另请参阅 transformed () 和 Pixmap 变换 .
这是重载函数。
This convenience function loads the matrix m 成 QTransform and calls the overloaded 函数采用 QTransform 和 width w and the height h .
返回像素图的宽度。
另请参阅 size () 和 Pixmap Information .