Qtopia Home - Classes - Hierachy - Annotated - Functions - Licenses - Reference |
|
The QPixmap class is an off-screen pixel-based paint device. More...
#include <qpixmap.h>
Inherits QPaintDevice and Qt.
Inherited by QBitmap.
It is one of the two classes Qt provides for dealing with images, the other being QImage. QPixmap is designed and optimized for drawing; QImage is designed and optimized for I/O and for direct pixel access/manipulation. There are (slow) functions to convert between QImage 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 managed by the underlying window system. Pixels can only be accessed through QPainter functions, through bitBlt(), and by converting the QPixmap to a QImage.
You can display a QPixmap on the screen easily using e.g. QLabel::setPixmap(), and all the QButton subclasses support pixmap use.
The QPixmap class uses lazy copying, so it is practical to pass pass QPixmap objects as arguments.
Note about Windows 95 and 98: On Windows 9x, the system crashes if you create more than approximately 1000 pixmaps, independent of the size of the pixmaps or installed RAM. Windows NT does not have this limitation.
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, Qt stores the pixmap in a way which is very memory-efficient when there are many pixmaps.
If your application uses dozens or hundreds of pixmaps, e.g. on tool bar buttons, in popup menus, and you plan to run it on Windows 95 or Windows 98, then we recommend using code like this:
QPixmap::setDefaultOptimization( QPixmap::MemoryOptim );
while ( ... ) {
// load tool bar pixmaps etc.
QPixmap *pixmap = new QPixmap(fileName);
}
QPixmap::setDefaultOptimization( QPixmap::NormalOptim );
See also QBitmap, QImage, QImageIO, and Shared Classes.
This enum type defines the color modes that exist for converting QImage objects to QPixmap. The current values are:
QPixmap has the choice of optimizing for speed or memory in a few places, and the best choice varies from pixmap to pixmap, but can generally be derived heuristically. This enum type defines a number of optimization modes you can set for any pixmap, to tweak the speed/memory tradeoffs:
We recommend sticking with DefaultOptim
See also isNull().
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().
The parameters are passed on to load().
See also isNull(), load(), loadFromData(), save(), and imageFormat().
The parameters are passed on to load().
See also isNull(), load(), loadFromData(), save(), and imageFormat().
Error are silently ignored.
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 e.g. when the code is in a shared library, and ROMable when the application is to be stored in ROM.
In order to use that sort of declaration, you must cast the variable
back to const char **
See also loadFromData().
The conversion_flags argument is a bitwise-OR from the following choices. The options marked (default) are the choice if no other choice from the list is included (they are zero):
Passing 0 for conversion_flags gives all the default options.
Note that even though a QPixmap with depth 1 behaves much like a QBitmap, isQBitmap() returns FALSE.
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.
Bugs and limitations:
See also convertToImage(), isQBitmap(), QImage::convertDepth(), defaultDepth(), and QImage::hasAlphaBuffer().
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.
Bugs and limitations:
See also convertFromImage().
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().
See also depth().
See also setDefaultOptimization(), setOptimization(), and optimization().
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().
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 widget's background color or pixmap. If the background is empty, nothing is done.
The ofs point is an offset in the widget.
The point ofs is a point in the widget's coordinate system. The pixmap's top left pixel will be mapped to the point ofs 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.
Example:
void CuteWidget::paintEvent( QPaintEvent *e )
{
QRect ur = e->rect(); // rectangle to update
QPixmap pix( ur.size() ); // Pixmap for double-buffering
pix.fill( this, ur.topLeft() ); // fill with widget background
QPainter p( &pix );
p.translate( -ur.x(), -ur.y() ); // use widget coordinate system
// when drawing on pixmap
// ... draw on pixmap ...
p.end();
bitBlt( this, ur.topLeft(), &pix );
}
If widget has children, they are painted too, appropriately located.
If you specify x, y, w or h, only the rectangle you specify is painted. The defaults are 0, 0 (top-left corner) and -1,-1 (which means the entire widget).
(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.)
If widget is 0, or if the rectangle defined by x, y, the modified w and the modified h does not overlap the widget->rect(), this function returns a null QPixmap.
This function actually asks widget to paint itself (and its children to paint themselves). QPixmap::grabWindow() grabs pixels off the screen, which is a bit faster and picks up exactly what's on-screen. This function works by calling paintEvent() with painter redirection turned on, which gets the result of paintEvent(), without e.g. overlying windows.
If there is overlap, it returns a pixmap of the size you want, containing a rendering of widget. If the rectangle you ask for is a superset of widget, the area outside widget are covered with the widget's background.
See also grabWindow(), QPainter::redirect(), and QWidget::paintEvent().
The arguments (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.
Note also that the mouse cursor is generally not grabbed.
The reason we use 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.
Warning: Grabbing an area outside the screen is not safe in general. This depends on the underlying window system.
See also grabWidget().
See also width(), size(), and rect().
The QImageIO documentation lists the supported image formats.
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().
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.
See the convertFromImage() documentation for a description of the conversion_flags argument.
The QImageIO documentation lists the supported image formats and explains how to add extra formats.
See also loadFromData(), save(), imageFormat(), QImage::load(), and QImageIO.
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.
See the convertFromImage() documentation for a description of the conversion_flags argument.
The QImageIO documentation lists the supported image formats and explains how to add extra formats.
See also load(), save(), imageFormat(), QImage::loadFromData(), and QImageIO.
See also setMask() and QBitmap.
Use the QPaintDeviceMetrics class instead.
Reimplemented from QPaintDevice.
See also convertFromImage().
Returns the optimization setting for this pixmap.
The default optimization setting is QPixmap::NormalOptim. You may change this settings in two ways:
See also setOptimization(), setDefaultOptimization(), and defaultOptimization().
See also width(), height(), and size().
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.
See also load(), loadFromData(), imageFormat(), QImage::save(), and QImageIO.
See also load(), loadFromData(), imageFormat(), QImage::save(), and QImageIO.
See also mask().
Returns a number that uniquely identifies the contents of this QPixmap object. This means that multiple QPixmaps objects can have the same serial number as long as they refer to the same contents. The serial number is for example very useful for caching.
See also QPixmapCache.
All new pixmaps that are created will use this default optimization. You may also set optimization for individual pixmaps using the setOptimization() function.
The initial default optimization setting is QPixmap::Normal.
See also defaultOptimization(), setOptimization(), and optimization().
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.
The optimization setting affects pixmap operations, in particular drawing of transparent pixmaps (bitBlt() a pixmap with a mask set) and pixmap transformations (the xForm() function).
Pixmap optimization involves keeping intermediate results in a cache buffer and use the data in the cache to speed up bitBlt() and xForm(). The cost is more memory consumption, up to twice as much as an unoptimized pixmap.
Use the setDefaultOptimization() to change the default optimization for all new pixmaps.
See also optimization(), setDefaultOptimization(), and defaultOptimization().
See also width(), height(), and rect().
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.
See also height(), size(), and rect().
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 baseline 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();
Bugs and limitations:
See also trueMatrix(), QWMatrix, and QPainter::setWorldMatrix().
See also QPixmap::save() and Format of the QDataStream operators.
See also QPixmap::load() and Format of the QDataStream operators.
This file is part of the Qtopia platform, copyright © 1995-2005 Trolltech, all rights reserved.
| Copyright © 2005 Trolltech | Trademarks | Qtopia version 2.2.0
|