The QPixmap class is an off-screen buffer paint device. More...
#include <qpixmap.h>
Inherits QPaintDevice.
Inherited by QBitmap.
A 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.
Example of flicker-free update:
void MyWidget::paintEvent( QPaintEvent * ) { QPixmap pm( size() ); // create pixmap QPainter p; // our painter pm.fill( backgroundColor() ); // initialize pixmap p.begin( &pm ); // start painting pixmap ... // draw something p.end(); // painting done bitBlt( this, 0,0, &pm ); // copy pixmap to widget }
The bitBlt() function has quite a few arguments that are not used in this example.
Pixel data in a pixmap is internal and managed by the underlying window system. Pixels can only be accessed through QImage, QPainter functions and the bitBlt().
You can load and save pixmaps using several image formats. You can display a QPixmap on the screen easily using QLabel::setPixmap(), and all the button classes support pixmap use.
A pixmap can be converted to a QImage to get direct access to the pixels. A QImage can also be converted back to a pixmap.
The QPixmap class is optimized by the use of implicit sharing, so it is very efficient to pass QPixmap objects as arguments.
See also: QBitmap, QImage, QImageIO and Shared Classes
Examples: picture/makepic.cpp hello/hello.cpp qmag/qmag.cpp widgets/widgets.cpp
Constructs a pixmap which is a copy of pixmap.
Constructs a pixmap from the file fileName. If the file does not exist, or is of an unknown format, the pixmap becomes a null pixmap.
The parameters are passed on to load().
See also: isNull(), load(), loadFromData(), save() and imageFormat().
Constructs a pixmap from xpm, which must be a valid XPM image.
[protected]
Constructs a monochrome pixmap which is initialized with the data in bits. This constructor is protected and used by the QBitmap class.
Constructs a pixmap with w width, h height and of depth bits per pixels.
The contents of the pixmap is uninitialized.
The depth can be either 1 (monochrome) or the depth of the current video mode. If depth is negative, then the hardware depth of the current video mode will be used.
If either width or height is zero, a null pixmap is constructed.
See also: isNull().
Constructs a null pixmap.
See also: isNull().
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it takes.
Destroys the pixmap.
Converts an image and sets this pixmap. Returns TRUE if successful.
The mode argument specifies whether the resulting pixmap should be a monochrome (depth == 1) or a normal (native depth) pixmap. This argument is ignored if this pixmap is a QBitmap.
If this pixmap is a QBitmap or mode == QPixmap::Mono,
the pixmap
becomes monochrome. If necessary, it is dithered using the
Floyd-Steinberg dithering algorithm.
If mode == QPixmap::Auto
(default) and the image has depth 1 and contains only black and white
pixels, then the pixmap becomes monochrome, as above.
If mode == QPixmap::Color,
the pixmap is dithered/converted to the
native display depth.
Note that even though a QPixmap with depth 1 behaves much like a QBitmap, isQBitmap() returns FALSE.
If image has more colors than the number of available colors, we try to pick the most important colors.
See also: convertToImage(), isQBitmap(), QImage::convertDepth() and defaultDepth().
Bugs and limitations:
Converts the pixmap to an image. Returns a null image if the operation failed.
If the pixmap has 1 bit depth, the returned image will also be 1 bits deep. If the pixmap has 2-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.
See also: convertFromImage().
Bugs and limitations:
Creates and returns a heuristic mask for this pixmap. It works by selecting a color from one of the corners, then chipping away pixels of that color, starting at all the edges.
The mask may not be perfect but should be reasonable, so you can do things like:
pm->setMask( pm->createHeuristicMask() );;
This function is slow because it involves transformation to a QImage, non-trivial computations and a transformation back to QBitmap.
See also: QImage::createHeuristicMask().
[static]
Returns the default pixmap depth, i.e. the depth a pixmap gets if -1 is specified.
See also: depth().
Returns the depth of the image.
The pixmap depth is also called bits per pixel (bpp) or bit planes of a pixmap. A null pixmap has depth 0.
See also: defaultDepth(), isNull() and QImage::convertDepth().
[virtual]
Special-purpose function that detaches the pixmap from shared pixmap data.
A pixmap is automatically detached by Qt 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 Qt know. You can first obtain the system-dependent handle and then call system-specific functions (for instance BitBlt under Windows) that modifies the pixmap contents. In this case, you can call detach() to cut the pixmap loose from other pixmaps that share data with this one.
detach() returns immediately if there is just a single reference or if the pixmap has not been initialized yet.
Fills the pixmap with the color fillColor.
Examples: hello/hello.cpp
Fills the pixmap with the widget's background color or pixmap.
The ofs point is an offset in the widget.
Fills the pixmap with the widget's background color or pixmap.
The xofs and yofs is an offset in the widget.
[static]
Grabs the contents of a window and makes a pixmap out of it. Returns the pixmap.
The argments (x,y) specify the offset in the window, while (w,h) specify the width and height of the area to be copied.
If w is negative, the function copies everything to the right border of the window. If h is negative, the function copies everything to the bottom of the window.
Note that grabWindows() grabs pixels from the screen, not from the window. This means that If there is another window partially or entirely over the one you grab, you get pixels from the overlying window too.
The reason we use a window identifier and not a QWidget is to enable grabbing of windows that are not part of the application.
Warning: Grabbing an area outside the window, or screen, is not safe in general. This depends on the underlying window system.
Returns the height of the pixmap.
See also: width(), size() and rect().
[static]
Returns a string that specifies the image format of the file fileName, or null if the file cannot be read or if the format cannot be recognized.
The QImageIO documentation lists the supported image formats.
[static]
Returns the global pixmap optimization flag. The default value is TRUE.
See also: optimizeGlobally(), optimize() and isOptimized().
Returns TRUE if it is a null pixmap.
A null pixmap has zero width, zero height and no contents. You cannot draw in a null pixmap or bitBlt() anything to it.
Resizing an existing pixmap to (0,0) makes a pixmap into a null pixmap.
See also: resize().
Examples: qmag/qmag.cpp
Returns the optimization flag for the pixmap.
The optimization flag is initially set to the global pixmap optimization flag allAreOptimized(), which is TRUE by default.
See also: optimize(), optimizeGlobally() and isGloballyOptimized().
Returns TRUE if this is a QBitmap, otherwise FALSE.
Loads a pixmap from the file fileName. Returns TRUE if successful, or FALSE if the pixmap could not be loaded.
If format is specified, the loader attempts to read the pixmap using the specified format. If format is not specified (default), the loader reads a few bytes from the header to guess the file format.
The mode argument specifies whether the resulting pixmap should be a monochrome (depth == 1) or a normal (native depth) pixmap. This argument is ignored if this pixmap is a QBitmap. See the convertFromImage() documentation for a detailed description.
The QImageIO documentation lists the supported image formats and explains how to add extra formats.
See also: loadFromData(), save(), imageFormat(), QImage::load() and QImageIO.
Examples: picture/makepic.cpp widgets/widgets.cpp
Loads a pixmap from the binary data in buf (len bytes). Returns TRUE if successful, or FALSE if the pixmap could not be loaded.
If format is specified, the loader attempts to read the pixmap using the specified format. If format is not specified (default), the loader reads a few bytes from the header to guess the file format.
The mode argument specifies whether the resulting pixmap should be a monochrome (depth == 1) or a normal (native depth) pixmap. This argument is ignored if this pixmap is a QBitmap. See the convertFromImage() documentation for a detailed description.
The QImageIO documentation lists the supported image formats and explains how to add extra formats.
See also: load(), save(), imageFormat(), QImage::loadFromData() and QImageIO.
Returns the mask bitmap, or null if no mask has been set.
See also: setMask() and QBitmap.
[virtual protected]
Internal implementation of the virtual QPaintDevice::metric() function.
Use the QPaintDeviceMetrics class instead.
Reimplemented from QPaintDevice.
Converts the image image to a pixmap that is assigned to this pixmap. Returns a reference to the pixmap.
See also: convertFromImage().
Assigns the pixmap pixmap to this pixmap and returns a reference to this pixmap.
Enables pixmap optimization if enable is TRUE, or disables optimization if enable is FALSE.
Pixmap optimization makes some pixmap operations faster. The disadvantage is that pixmap optimization consumes some extra memory, rougly width()*depth()*height()/8 bytes.
See also: isOptimized(), optimizeGlobally() and isGloballyOptimized().
[static]
Sets the global pixmap optimization flag.
All new pixmaps that are created will be optimized (equivalent to calling optimize() for each pixmap) if enable is TRUE. Global optimization is turned off if enable is FALSE.
Optimization can be overridden for individual pixmaps by optimize().
The default value is TRUE.
See also: isGloballyOptimized(), optimize() and isOptimized().
Returns the enclosing rectangle (0,0,width(),height()) of the pixmap.
See also: width(), height() and size().
Resizes the pixmap to w width and h height. If either w or h is less than 1, the pixmap becomes a null pixmap.
If both w and h are greater than 0, a valid pixmap is created. New pixels will be uninitialized (random) if the pixmap is expanded.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it takes.
Saves the pixmap to the file fileName, using the image file format format. Returns TRUE if successful, or FALSE if the pixmap could not be saved.
See also: load(), loadFromData(), imageFormat(), QImage::save() and QImageIO.
Returns TRUE if the pixmap's mask is identical to the pixmap itself.
See also: mask().
Returns a number that uniquely identifies this QPixmap object. The serial number is very useful for caching.
See also: QPixmapCache.
Sets a mask bitmap.
The mask bitmap defines the clip mask for this pixmap. Every pixel in mask 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.
Setting a null mask resets the mask,
See also: mask(), createHeuristicMask() and QBitmap.
Returns the size of the pixmap.
See also: width(), height() and rect().
[static]
Returns the actual matrix used for transforming a pixmap with w width and h height.
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.
This function returns the modified matrix, which maps points correctly from the original pixmap into the new pixmap.
See also: xForm() and QWMatrix.
Returns the width of the pixmap.
See also: height(), size() and rect().
Transforms the pixmap using matrix, and returns the transformed pixmap.
Qt uses this function to implement rotated text on window systems that do not support such complex features.
Example of how to manually draw a rotated text at (100,200) in a widget:
char *str = "Trolls R Qt"; // text to be drawn QFont f( "Charter", 24 ); // use Charter 24pt font QPixmap pm( 8, 8 ); QPainter p; QRect r; // text bounding rectangle QPoint bl; // text baselink position p.begin( &pm ); // first get the bounding p.setFont( f ); // text rectangle r = p.fontMetrics().boundingRect(str); bl = -r.topLeft(); // get baseline position p.end(); pm.resize( r.size() ); // resize to fit the text pm.fill( white ); // fills pm with white p.begin( &pm ); // begin painting pm p.setFont( f ); // set the font p.setPen( blue ); // set blue text color p.drawText( bl, str ); // draw the text p.end(); // painting done QWMatrix m; // transformation matrix m.rotate( -33.4 ); // rotate coordinate system QPixmap rp = pm.xForm( m ); // rp is rotated pixmap QWMatrix t = QPixmap::trueMatrix( m, pm.width(), pm.height() ); int x, y; t.map( bl.x(),bl.y(), &x,&y ); // get pm's baseline pos in rp bitBlt( myWidget, 100-x, 200-y, // blt rp into a widget &rp, 0, 0, -1, -1 );
This example outlines how Qt implements rotated text under X11. The font calculation is the most tedious part. The rotation itself is only 3 lines of code.
If you want to draw rotated text, you do not have to implement all the code above. The code below does exactly the same thing as the example above, except that it uses a QPainter.
char *str = "Trolls R Qt"; // text to be drawn QFont f( "Charter", 24 ); // use Charter 24pt font QPainter p; p.begin( myWidget ); p.translate( 100, 200 ); // translates coord system p.rotate( -33.4 ); // rotates it counterclockwise p.setFont( f ); p.drawText( 0, 0, str ); p.end();
See also: trueMatrix(), QWMatrix and QPainter::setWorldMatrix().
Bugs and limitations:
Examples: qmag/qmag.cpp
Writes a pixmap to the stream as a BMP image.
See also: QPixmap::save().
Reads a pixmap from the stream.
See also: QPixmap::load().
This file is part of the Qt toolkit, copyright © 1995-97 Troll Tech, all rights reserved.
It was generated from the following files: