QPixmap is one of the two classes TQt provides for dealing with images; the other is TQImage. QPixmap is designed and optimized for drawing; TQImage is designed and optimized for I/O and for direct pixel access/manipulation. There are (slow) functions to convert between TQImage and QPixmap: convertToImage() and convertFromImage().
One common use of the QPixmap class is to enable smooth updating of widgets. Whenever something complex needs to be drawn, you can use a pixmap to obtain flicker-free drawing, like this:
Pixel data in a pixmap is internal and is managed by the underlying window system. Pixels can be accessed only through QPainter functions, through bitBlt(), and by converting the QPixmap to a TQImage.
You can easily display a QPixmap on the screen using QLabel::setPixmap(). For example, all the QButton subclasses support pixmap use.
.PP
The QPixmap class uses copy-on-write, so it is practical to pass QPixmap objects by value.
.PP
You can retrieve the width(), height(), depth() and size() of a pixmap. The enclosing rectangle is given by rect(). Pixmaps can be filled with fill() and resized with resize(). You can create and set a mask with createHeuristicMask() and setMask(). Use selfMask() to see if the pixmap is identical to its mask.
.PP
In addition to loading a pixmap from file using load() you can also loadFromData(). You can control optimization with setOptimization() and obtain a transformed version of the pixmap using xForm()
Note regarding Windows 95 and 98: on Windows 9x the system crashes if you create more than about 1000 pixmaps, independent of the size of the pixmaps or installed RAM. Windows NT-systems (including 2000, XP and following versions) do not have the same limitation, but depending on the graphics equipment the system will fail to allocate pixmap objects at some point (due to system running out of GDI resources).
Qt tries to work around the resource limitation. If you set the pixmap optimization to QPixmap::MemoryOptim and the width of your pixmap is less than or equal to 128 pixels, TQt stores the pixmap in a way that is very memory-efficient when there are many pixmaps.
If your application uses dozens or hundreds of pixmaps (for example on tool bar buttons and in popup menus), and you plan to run it on Windows 95 or Windows 98, we recommend using code like this:
QPixmap has the choice of optimizing for speed or memory in a few places; the best choice varies from pixmap to pixmap but can generally be derived heuristically. This enum type defines a number of optimization modes that you can set for any pixmap to tweak the speed/memory tradeoffs:
.TP
\fCQPixmap::DefaultOptim\fR - Whatever QPixmap::defaultOptimization() returns. A pixmap with this optimization will have whatever the current default optimization is. If the default optimization is changed using setDefaultOptimization(), then this will not effect any pixmaps that have already been created.
.TP
\fCQPixmap::NoOptim\fR - No optimization (currently the same as MemoryOptim).
.TP
\fCQPixmap::MemoryOptim\fR - Optimize for minimal memory use on Windows 9x and X11 systems.
.TP
\fCQPixmap::NormalOptim\fR - Optimize for typical usage. Often uses more memory than MemoryOptim, and is often faster.
.TP
\fCQPixmap::BestOptim\fR - Optimize for pixmaps that are drawn very often and where performance is critical. Generally uses more memory than NormalOptim and may provide a little more speed.
.SH "QPixmap::QPixmap ( int w, int h, int depth = -1, Optimization optimization = DefaultOptim )"
Constructs a pixmap with \fIw\fR width, \fIh\fR height and \fIdepth\fR bits per pixel. The pixmap is optimized in accordance with the \fIoptimization\fR value.
.PP
The contents of the pixmap is uninitialized.
.PP
The \fIdepth\fR can be either 1 (monochrome) or the depth of the current video mode. If \fIdepth\fR is negative, then the hardware depth of the current video mode will be used.
.PP
If either \fIw\fR or \fIh\fR is zero, a null pixmap is constructed.
Constructs a pixmap from the file \fIfileName\fR. If the file does not exist or is of an unknown format, the pixmap becomes a null pixmap.
.PP
The \fIfileName\fR, \fIformat\fR and \fImode\fR parameters are passed on to load(). This means that the data in \fIfileName\fR is not compiled into the binary. If \fIfileName\fR contains a relative path (e.g. the filename only) the relevant file must be found relative to the runtime working directory.
.PP
See also QPixmap::ColorMode, isNull(), load(), loadFromData(), save(), and imageFormat().
Constructs a pixmap from the file \fIfileName\fR. If the file does not exist or is of an unknown format, the pixmap becomes a null pixmap.
.PP
The \fIfileName\fR, \fIformat\fR and \fIconversion_flags\fR parameters are passed on to load(). This means that the data in \fIfileName\fR is not compiled into the binary. If \fIfileName\fR contains a relative path (e.g. the filename only) the relevant file must be found relative to the runtime working directory.
.PP
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 \fIconversion_flags\fR to specify how you'd prefer this to happen.
Constructs a pixmap from \fIxpm\fR, which must be a valid XPM image.
.PP
Errors are silently ignored.
.PP
Note that it's possible to squeeze the XPM variable a little bit by using an unusual declaration:
.PP
.nf
.br
static const char * const start_xpm[]={
.br
"16 15 8 1",
.br
"a c #cec6bd",
.br
....
.br
.fi
.PP
The extra \fCconst\fR 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.
.PP
In order to use that sort of declaration you must cast the variable back to \fCconst char **\fR when you create the QPixmap.
Constructs a pixmaps by loading from \fIimg_data\fR. The data can be in any image format supported by Qt.
.PP
See also loadFromData().
.SH "QPixmap::QPixmap ( const QPixmap & pixmap )"
Constructs a pixmap that is a copy of \fIpixmap\fR.
.SH "QPixmap::QPixmap ( int w, int h, const uchar * bits, bool isXbitmap )\fC [protected]\fR"
Constructs a monochrome pixmap, with width \fIw\fR and height \fIh\fR, that is initialized with the data in \fIbits\fR. The \fIisXbitmap\fR indicates whether the data is an X bitmap and defaults to FALSE. This constructor is protected and used by the QBitmap class.
The \fIconversion_flags\fR argument is a bitwise-OR of the TQt::ImageConversionFlags. Passing 0 for \fIconversion_flags\fR sets all the default options.
Note that even though a QPixmap with depth 1 behaves much like a QBitmap, isQBitmap() returns FALSE.
.PP
If a pixmap with depth 1 is painted with color0 and color1 and converted to an image, the pixels painted with color0 will produce pixel index 0 in the image and those painted with color1 will produce pixel index 1.
If the pixmap has 1-bit depth, the returned image will also be 1 bit deep. If the pixmap has 2- to 8-bit depth, the returned image has 8-bit depth. If the pixmap has greater than 8-bit depth, the returned image has 32-bit depth.
.PP
Note that for the moment, alpha masks on monochrome images are ignored.
Creates and returns a heuristic mask 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.
.PP
The mask may not be perfect but it should be reasonable, so you can do things such as the following:
A pixmap is automatically detached by TQt whenever its contents is about to change. This is done in all QPixmap member functions that modify the pixmap (fill(), resize(), convertFromImage(), load(), etc.), in bitBlt() for the destination pixmap and in QPainter::begin() on a pixmap.
It is possible to modify a pixmap without letting TQt know. You can first obtain the system-dependent handle() and then call system-specific functions (for instance, BitBlt under Windows) that modify the pixmap contents. In such cases, you can call detach() to cut the pixmap loose from other pixmaps that share data with this one.
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
.PP
Fills the pixmap with the \fIwidget\fR's background color or pixmap. If the background is empty, nothing is done. \fIxofs\fR, \fIyofs\fR is an offset in the widget.
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
.PP
Fills the pixmap with the \fIwidget\fR's background color or pixmap. If the background is empty, nothing is done.
.PP
The \fIofs\fR point is an offset in the widget.
.PP
The point \fIofs\fR is a point in the widget's coordinate system. The pixmap's top-left pixel will be mapped to the point \fIofs\fR in the widget. This is significant if the widget has a background pixmap; otherwise the pixmap will simply be filled with the background color of the widget.
.PP
Example:
.PP
.nf
.br
void CuteWidget::paintEvent( QPaintEvent *e )
.br
{
.br
QRect ur = e->rect(); // rectangle to update
.br
QPixmap pix( ur.size() ); // Pixmap for double-buffering
.br
pix.fill( this, ur.topLeft() ); // fill with widget background
.br
.br
QPainter p( &pix );
.br
p.translate( -ur.x(), -ur.y() ); // use widget coordinate system
If the \fIwidget\fR has any children, then they are also painted in the appropriate positions.
.PP
If you specify \fIx\fR, \fIy\fR, \fIw\fR or \fIh\fR, only the rectangle you specify is painted. The defaults are 0, 0 (top-left corner) and -1,-1 (which means the entire widget).
.PP
(If \fIw\fR is negative, the function copies everything to the right border of the window. If \fIh\fR is negative, the function copies everything to the bottom of the window.)
.PP
If \fIwidget\fR is 0, or if the rectangle defined by \fIx\fR, \fIy\fR, the modified \fIw\fR and the modified \fIh\fR does not overlap the \fIwidget\fR->rect(), this function will return a null QPixmap.
.PP
This function actually asks \fIwidget\fR to paint itself (and its children to paint themselves). QPixmap::grabWindow() grabs pixels off the screen, which is a bit faster and picks up \fIexactly\fR what's on-screen. This function works by calling paintEvent() with painter redirection turned on. If there are overlaying windows, grabWindow() will see them, but not this function.
.PP
If there is overlap, it returns a pixmap of the size you want, containing a rendering of \fIwidget\fR. If the rectangle you ask for is a superset of \fIwidget\fR, the areas outside \fIwidget\fR are covered with the widget's background.
.PP
If an error occurs when trying to grab the widget, such as the size of the widget being too large to fit in memory, an isNull() pixmap is returned.
.SH "QPixmap QPixmap::grabWindow ( WId window, int x = 0, int y = 0, int w = -1, int h = -1 )\fC [static]\fR"
Grabs the contents of the window \fIwindow\fR and makes a pixmap out of it. Returns the pixmap.
.PP
The arguments \fI(x, y)\fR specify the offset in the window, whereas \fI(w, h)\fR specify the width and height of the area to be copied.
.PP
If \fIw\fR is negative, the function copies everything to the right border of the window. If \fIh\fR is negative, the function copies everything to the bottom of the window.
.PP
Note that grabWindow() grabs pixels from the screen, not from the window. If there is another window partially or entirely over the one you grab, you get pixels from the overlying window, too.
.PP
Note also that the mouse cursor is generally not grabbed.
The reason we use a window identifier and not a TQWidget is to enable grabbing of windows that are not part of the application, window system frames, and so on.
\fBWarning:\fR Grabbing an area outside the screen is not safe in general. This depends on the underlying window system.
.PP
\fBWarning:\fR X11 only: If \fIwindow\fR is not the same depth as the root window and another window partially or entirely obscures the one you grab, you will \fInot\fR get pixels from the overlying window. The contests of the obscured areas in the pixmap are undefined and uninitialized.
.PP
See also grabWidget().
.PP
Example: qmag/qmag.cpp.
.SH "bool QPixmap::hasAlpha () const"
Returns TRUE this pixmap has an alpha channel or a mask.
.PP
See also hasAlphaChannel() and mask().
.SH "bool QPixmap::hasAlphaChannel () const"
Returns TRUE if the pixmap has an alpha channel; otherwise it returns FALSE.
.PP
NOTE: If the pixmap has a mask but not alpha channel, this function returns FALSE.
.PP
See also hasAlpha() and mask().
.SH "int QPixmap::height () const"
Returns the height of the pixmap.
.PP
See also width(), size(), and rect().
.PP
Examples:
.)l desktop/desktop.cpp, movies/main.cpp, scribble/scribble.cpp, scrollview/scrollview.cpp, t10/cannon.cpp, and xform/xform.cpp.
Loads a pixmap from the file \fIfileName\fR at runtime. Returns TRUE if successful; otherwise returns FALSE.
.PP
If \fIformat\fR is specified, the loader attempts to read the pixmap using the specified format. If \fIformat\fR is not specified (default), the loader reads a few bytes from the header to guess the file's format.
.PP
See the convertFromImage() documentation for a description of the \fIconversion_flags\fR argument.
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
.PP
Loads a pixmap from the file \fIfileName\fR at runtime.
.PP
If \fIformat\fR is specified, the loader attempts to read the pixmap using the specified format. If \fIformat\fR is not specified (default), the loader reads a few bytes from the header to guess the file's format.
.PP
The \fImode\fR is used to specify the color mode of the pixmap.
.PP
See also QPixmap::ColorMode.
.SH "bool QPixmap::loadFromData ( const uchar * buf, uint len, const char * format, int conversion_flags )"
Loads a pixmap from the binary data in \fIbuf\fR (\fIlen\fR bytes). Returns TRUE if successful; otherwise returns FALSE.
.PP
If \fIformat\fR is specified, the loader attempts to read the pixmap using the specified format. If \fIformat\fR is not specified (default), the loader reads a few bytes from the header to guess the file's format.
.PP
See the convertFromImage() documentation for a description of the \fIconversion_flags\fR argument.
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
.PP
Loads a pixmap from the binary data in \fIbuf\fR (\fIlen\fR bytes) using color mode \fImode\fR. Returns TRUE if successful; otherwise returns FALSE.
.PP
If \fIformat\fR is specified, the loader attempts to read the pixmap using the specified format. If \fIformat\fR is not specified (default), the loader reads a few bytes from the header to guess the file's format.
.PP
See also QPixmap::ColorMode.
.SH "bool QPixmap::loadFromData ( const QByteArray & buf, const char * format = 0, int conversion_flags = 0 )"
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
.SH "const QBitmap * QPixmap::mask () const"
Returns the mask bitmap, or 0 if no mask has been set.
.PP
See also setMask(), QBitmap, and hasAlpha().
.SH "int QPixmap::metric ( int m ) const\fC [virtual protected]\fR"
Internal implementation of the virtual QPaintDevice::metric() function.
Saves the pixmap to the file \fIfileName\fR using the image file format \fIformat\fR and a quality factor \fIquality\fR. \fIquality\fR 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. Returns TRUE if successful; otherwise returns FALSE.
pixmap.save( &buffer, "PNG" ); // writes pixmap into ba in PNG format
.br
.fi
.SH "bool QPixmap::selfMask () const"
Returns TRUE if the pixmap's mask is identical to the pixmap itself; otherwise returns FALSE.
.PP
See also mask().
.SH "int QPixmap::serialNumber () const"
Returns a number that uniquely identifies the contents of this QPixmap object. This means that multiple QPixmap objects can have the same serial number as long as they refer to the same contents.
.PP
An example of where this is useful is for caching QPixmaps.
All \fInew\fR pixmaps that are created will use this default optimization. You may also set optimization for individual pixmaps using the setOptimization() function.
.PP
The initial default \fIoptimization\fR setting is \fCQPixmap::Normal\fR.
.PP
See also defaultOptimization(), setOptimization(), and optimization().
The \fInewmask\fR bitmap defines the clip mask for this pixmap. Every pixel in \fInewmask\fR corresponds to a pixel in this pixmap. Pixel value 1 means opaque and pixel value 0 means transparent. The mask must have the same size as this pixmap.
.PP
\fBWarning:\fR Setting the mask on a pixmap will cause any alpha channel data to be cleared. For example:
.PP
.nf
.br
QPixmap alpha( "image-with-alpha.png" );
.br
QPixmap alphacopy = alpha;
.br
alphacopy.setMask( *alphacopy.mask() );
.br
.fi
Now, alpha and alphacopy are visually different.
.PP
Setting a null mask resets the mask.
.PP
See also mask(), createHeuristicMask(), and QBitmap.
The \fIoptimization\fR setting affects pixmap operations, in particular drawing of transparent pixmaps (bitBlt() a pixmap with a mask set) and pixmap transformations (the xForm() function).
.PP
Pixmap optimization involves keeping intermediate results in a cache buffer and using the cache to speed up bitBlt() and xForm(). The cost is more memory consumption, up to twice as much as an unoptimized pixmap.
.PP
Use the setDefaultOptimization() to change the default optimization for all new pixmaps.
.PP
See also optimization(), setDefaultOptimization(), and defaultOptimization().
.PP
Example: desktop/desktop.cpp.
.SH "QSize QPixmap::size () const"
Returns the size of the pixmap.
.PP
See also width(), height(), and rect().
.PP
Example: movies/main.cpp.
.SH "QWMatrix QPixmap::trueMatrix ( const QWMatrix & matrix, int w, int h )\fC [static]\fR"
Returns the actual matrix used for transforming a pixmap with \fIw\fR width and \fIh\fR height and matrix \fImatrix\fR.
.PP
When transforming a pixmap with xForm(), the transformation matrix is internally adjusted to compensate for unwanted translation, i.e. xForm() returns the smallest pixmap containing all transformed points of the original pixmap.
.PP
This function returns the modified matrix, which maps points correctly from the original pixmap into the new pixmap.
.PP
See also xForm() and QWMatrix.
.SH "int QPixmap::width () const"
Returns the width of the pixmap.
.PP
See also height(), size(), and rect().
.PP
Examples:
.)l desktop/desktop.cpp, movies/main.cpp, scribble/scribble.cpp, scrollview/scrollview.cpp, and xform/xform.cpp.
Returns a copy of the pixmap that is transformed using \fImatrix\fR. The original pixmap is not changed.
.PP
The transformation \fImatrix\fR is internally adjusted to compensate for unwanted translation, i.e. xForm() returns the smallest image that contains all the transformed points of the original image.
.)l desktop/desktop.cpp, fileiconview/qfileiconview.cpp, movies/main.cpp, and qmag/qmag.cpp.
.SH RELATED FUNCTION DOCUMENTATION
.SH "void copyBlt ( QPixmap * dst, int dx, int dy, const QPixmap * src, int sx, int sy, int sw, int sh )"
Copies a block of pixels from \fIsrc\fR to \fIdst\fR. The alpha channel and mask data (if any) is also copied from \fIsrc\fR. NOTE: \fIsrc\fR is \fInot\fR alpha blended or masked when copied to \fIdst\fR. Use bitBlt() or QPainter::drawPixmap() to perform alpha blending or masked drawing.
.PP
\fIsx\fR, \fIsy\fR is the top-left pixel in \fIsrc\fR (0, 0 by default), \fIdx\fR, \fIdy\fR is the top-left position in \fIdst\fR and \fIsw\fR, \\sh is the size of the copied block (all of \fIsrc\fR by default).
.PP
If \fIsrc\fR, \fIdst\fR, \fIsw\fR or \fIsh\fR is 0 (zero), copyBlt() does nothing. If \fIsw\fR or \fIsh\fR is negative, copyBlt() copies starting at \fIsx\fR (and respectively, \fIsy\fR) and ending at the right edge (and respectively, the bottom edge) of \fIsrc\fR.
.PP
copyBlt() does nothing if \fIsrc\fR and \fIdst\fR have different depths.