Home · All Classes · Main Classes · Annotated · Grouped Classes · Functions

QPixmap Class Reference

The QPixmap class is an off-screen, pixel-based paint device. More...

#include <QPixmap>

Part of the QtGui module.

Inherits QPaintDevice.

Inherited by Q3CanvasPixmap and QBitmap.

Public Types

Public Functions

Static Public Members

Protected Functions

Related Non-Members


Detailed Description

The QPixmap class is an off-screen, pixel-based paint device.

QPixmap is one of the three classes Qt provides for dealing with images, the others being QImage and QPicture. QPixmap is designed and optimized for drawing on screen; QImage is designed for I/O and for direct pixel access; QPicture provides a scalable, vectorial picture. There are (slow) functions to convert between QImage and QPixmap: toImage() and fromImage(). There's also a QIcon class that stores various versions of an icon.

A common way to create a pixmap is to use the constructor that takes a file name. For example:

    label->setPixmap(QPixmap("paste.png"));

The file name can be either refer to an actual file on disk or to one of the application's embedded resources. See the Resource System overview for details on how to embed images and other resource files in the application's executable.

Pixel data in a pixmap is internal and is managed by the underlying window system. Pixels can be accessed only through QPainter functions and by converting the QPixmap to a QImage.

You can easily display a QPixmap on the screen using QLabel or one of QAbstractButton's subclasses (such as QPushButton and QToolButton). QLabel has a pixmap property, whereas QAbstractButton has an icon property.

The QPixmap class uses implicit sharing, so you can pass QPixmap objects around by value.

You can retrieve the width(), height(), depth(), and size() of a pixmap. The enclosing rectangle can be determined with 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.

In addition to loading a pixmap from file using load() you can also loadFromData(). You can obtain a transformed version of the pixmap using transform().

Note Regarding Windows 95 and 98

The system may crash if you create more than about 1000 pixmaps, independent of the size of the pixmaps or installed RAM. Windows NT systems (including Windows 2000 and XP) do not have the same limitation, but depending on the graphics equipment the system will fail to allocate pixmap objects when it runs 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, Qt stores the pixmap in a way that is very memory-efficient when there are many pixmaps.

See also QBitmap, QImage, QImageReader, and Shared Classes.


Member Type Documentation

enum QPixmap::Optimization

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:

ConstantValueDescription
QPixmap::DefaultOptim0Whatever 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.
QPixmap::NoOptim1No optimization (currently the same as MemoryOptim).
QPixmap::MemoryOptimNoOptimOptimize for minimal memory use on Windows 9x and X11 systems.
QPixmap::NormalOptim Optimize for typical usage. Often uses more memory than MemoryOptim, and is often faster.
QPixmap::BestOptim 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.
QPixmap::LoadOptim Optimize for pixmaps that are loaded from disk rather than stored in memory.

We recommend using DefaultOptim.


Member Function Documentation

QPixmap::QPixmap ()

Constructs a null pixmap.

See also isNull().

QPixmap::QPixmap ( const QImage & image )

Constructs a pixmap from the QImage image.

See also fromImage().

QPixmap::QPixmap ( int w, int h, int depth = -1, Optimization optimization = DefaultOptim )

Constructs a pixmap with w width, h height and depth bits per pixel. The pixmap is optimized in accordance with the optimization value.

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 w or h is zero, a null pixmap is constructed.

See also isNull() and QPixmap::Optimization.

QPixmap::QPixmap ( const QString & fileName, const char * format = 0, Qt::ImageConversionFlags flags = Qt::AutoColor, Optimization optimization = DefaultOptim )

Constructs a pixmap from the file with the given fileName. If the file does not exist or is of an unknown format, the pixmap becomes a null pixmap.

The fileName, format and flags parameters are passed on to load(). This means that the data in fileName is not compiled into the binary. If fileName contains a relative path (e.g. the filename only) the relevant file must be found relative to the runtime working directory.

The file name can be either refer to an actual file on disk or to one of the application's embedded resources. See the Resource System overview for details on how to embed images and other resource files in the application's executable.

The way the pixmap is handled is specified by optimization.

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 specify how you'd prefer this to happen.

See also Qt::ImageConversionFlags, isNull(), load(), loadFromData(), save(), imageFormat(), and Optimization.

QPixmap::QPixmap ( const char * const[] xpm )

Constructs a pixmap from xpm, which must be a valid XPM image.

Errors 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 (for example, 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 ** when you create the QPixmap.

QPixmap::QPixmap ( const QPixmap & pixmap )

Constructs a pixmap that is a copy of pixmap.

QPixmap::QPixmap ( int w, int h, const uchar * bits, bool isXbitmap )   [protected]

Constructs a monochrome pixmap, with width w and height h, that is initialized with the data in bits. The isXbitmap indicates whether the data is an X bitmap and defaults to false. This constructor is protected and used by the QBitmap class.

QPixmap::QPixmap ( const QSize & size, int depth = -1, Optimization optimization = DefaultOptim )

Constructs a pixmap of size size, depth bits per pixel, optimized in accordance with the optimization value.

QPixmap::~QPixmap ()

Destroys the pixmap.

QBitmap QPixmap::createHeuristicMask ( bool clipTight = true ) const

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.

The mask may not be perfect but it should be reasonable, so you can do things such as the following:

    pm->setMask(pm->createHeuristicMask());

This function is slow because it involves transformation to a QImage, non-trivial computations and a transformation back to a QBitmap.

If clipTight is true the mask is just large enough to cover the pixels; otherwise, the mask is larger than the data pixels.

See also QImage::createHeuristicMask().

QBitmap QPixmap::createMaskFromColor ( const QColor & maskColor ) const

Creates and returns a mask for this pixmap based on maskColor.

This function is slow because it involves transformation to a QImage and a transformation back to a QBitmap.

See also createHeuristicMask().

int QPixmap::defaultDepth ()   [static]

Returns the default pixmap depth, i.e. the depth a pixmap gets if -1 is specified.

See also depth().

Optimization QPixmap::defaultOptimization ()   [static]

Returns the default pixmap optimization setting.

See also setDefaultOptimization(), setOptimization(), and optimization().

int QPixmap::depth () const

Returns the depth of the pixmap.

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().

void QPixmap::detach ()   [virtual]

This is a 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.), 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 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.

detach() returns immediately if there is just a single reference or if the pixmap has not been initialized yet.

void QPixmap::fill ( const QColor & fillColor = Qt::white )

Fills the pixmap with the color fillColor.

void QPixmap::fill ( const QWidget * widget, const QPoint & offset )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Fills the pixmap with the widget's background color or pixmap.

The offset point is an offset in the widget.

The point offset is a point in the widget's coordinate system. The pixmap's top-left pixel will be mapped to the point offset 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.

void QPixmap::fill ( const QWidget * widget, int xoff, int yoff )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Fills the pixmap with the widget's background color or pixmap. xoff, yoff is an offset in the widget.

bool QPixmap::fromImage ( const QImage & img, Qt::ImageConversionFlags flags = Qt::AutoColor )

Converts image img and sets this pixmap. Returns true if successful; otherwise returns false.

The flags argument is a bitwise-OR of the Qt::ImageConversionFlags. Passing 0 for flags sets 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 Qt::color0 and Qt::color1 and converted to an image, the pixels painted with Qt::color0 will produce pixel index 0 in the image and those painted with Qt::color1 will produce pixel index 1.

See also convertToImage(), isQBitmap(), QImage::convertDepth(), defaultDepth(), and QImage::hasAlphaBuffer().

HDC QPixmap::getDC () const   [virtual]

Returns the window system handle of the widget, for low-level access. Using this function is not portable.

An HDC aquired with getDC() has to be released with releaseDC().

Reimplemented from QPaintDevice.

QPixmap QPixmap::grabWidget ( QWidget * widget, int x = 0, int y = 0, int w = -1, int h = -1 )   [static]

Creates a pixmap and paints widget in it.

If the widget has any children, then they are also painted in the appropriate positions.

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 will return 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. If there are overlaying windows, grabWindow() will see them, but not this function.

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 areas outside widget are covered with the widget's background.

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.

See also grabWindow() and QWidget::paintEvent().

QPixmap QPixmap::grabWidget ( QWidget * widget, const QRect & rect )   [static]

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Creates a pixmap and paints widget in it. If the widget has any children, then they are also painted in the appropriate positions.

If rect is a valid rectangle, only the rectangle you specify is painted.

grabWidget(), QRect::isValid()

QPixmap QPixmap::grabWindow ( WId window, int x = 0, int y = 0, int w = -1, int h = -1 )   [static]

Grabs the contents of the window window and makes a pixmap out of it. Returns the pixmap.

The arguments (x, y) specify the offset in the window, whereas (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 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.

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.

Warning: X11 only: If window is not 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 contests of the obscured areas in the pixmap are undefined and uninitialized.

See also grabWidget().

bool QPixmap::hasAlpha () const

Returns true this pixmap has an alpha channel or a mask.

See also hasAlphaChannel() and mask().

bool QPixmap::hasAlphaChannel () const

Returns true if the pixmap has an alpha channel; otherwise it returns false.

NOTE: If the pixmap has a mask but not alpha channel, this function returns false.

See also hasAlpha() and mask().

int QPixmap::height () const

Returns the height of the pixmap.

See also width(), size(), and rect().

bool QPixmap::isNull () const

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.

Resizing an existing pixmap to (0, 0) makes a pixmap into a null pixmap.

See also resize().

bool QPixmap::isQBitmap () const

Returns true if this is a QBitmap; otherwise returns false.

bool QPixmap::load ( const QString & fileName, const char * format = 0, Qt::ImageConversionFlags flags = Qt::AutoColor )

Loads a pixmap from the file fileName at runtime. Returns true if successful; otherwise returns false.

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's format.

See the fromImage() documentation for a description of the flags argument.

The QImageReader documentation lists the supported image formats and explains how to add extra formats.

The file name can be either refer to an actual file on disk or to one of the application's embedded resources. See the Resource System overview for details on how to embed images and other resource files in the application's executable.

See also loadFromData(), save(), imageFormat(), QImage::load(), and QImageReader.

bool QPixmap::loadFromData ( const uchar * buf, uint len, const char * format = 0, Qt::ImageConversionFlags flags = Qt::AutoColor )

Loads a pixmap from the binary data in buf (len bytes). Returns true if successful; otherwise returns false.

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's format.

See the fromImage() documentation for a description of the flags argument.

The QImageReader documentation lists the supported image formats and explains how to add extra formats.

See also load(), save(), imageFormat(), QImage::loadFromData(), and QImageReader.

bool QPixmap::loadFromData ( const QByteArray & buf, const char * format = 0, Qt::ImageConversionFlags flags = Qt::AutoColor )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

const QBitmap * QPixmap::mask () const

Returns the mask bitmap, or 0 if no mask has been set.

See also setMask(), QBitmap, and hasAlpha().

int QPixmap::metric ( PaintDeviceMetric m ) const   [virtual protected]

Internal implementation of the virtual QX11Info::metric() function.

m is the metric to get.

Reimplemented from QPaintDevice.

Optimization QPixmap::optimization () const

Returns the optimization setting for this pixmap.

The default optimization setting is QPixmap::NormalOptim. You can change this setting in two ways:

See also setOptimization(), setDefaultOptimization(), and defaultOptimization().

QRect QPixmap::rect () const

Returns the enclosing rectangle (0,0,width(),height()) of the pixmap.

See also width(), height(), and size().

void QPixmap::releaseDC ( HDC hdc ) const   [virtual]

Releases the HDC aquired by a previous call to getDC(). Using this function is not portable.

Reimplemented from QPaintDevice.

void QPixmap::resize ( int w, int h )

Resizes the pixmap to w width and h height. If either w or h is 0, 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.

void QPixmap::resize ( const QSize & size )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Resizes the pixmap to size size.

bool QPixmap::save ( const QString & fileName, const char * format, int quality = -1 ) const

Saves the pixmap to the file fileName using the image file format format and a quality factor quality. quality 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.

See also load(), loadFromData(), imageFormat(), QImage::save(), and QImageReader.

bool QPixmap::save ( QIODevice * device, const char * format, int quality = -1 ) const

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

This function writes a QPixmap to the QIODevice, device. 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 ba in PNG format

QPixmap QPixmap::scale ( int w, int h, Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio, Qt::TransformationMode transformMode = Qt::FastTransformation ) const

Returns a copy of the pixmap scaled to a rectangle of width w and height h according to aspectRatioMode and transformMode.

If either the width w or the height h is zero or negative, this function returns a null pixmap.

See also scaleWidth(), scaleHeight(), and transform().

QPixmap QPixmap::scale ( const QSize & size, Qt::AspectRatioMode aspectMode = Qt::IgnoreAspectRatio, Qt::TransformationMode transformMode = Qt::FastTransformation ) const

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Scales the pixmap to the given size, using the aspect ratio and transformation modes specified by aspectMode and transformMode.

QPixmap QPixmap::scaleHeight ( int h ) const

Returns a scaled copy of the pixmap. The returned pixmap has a height of h pixels. This function automatically calculates the width of the pixmap so that the ratio of the pixmap is preserved.

If h is 0 or negative a null pixmap is returned.

See also scale(), scaleWidth(), and transform().

QPixmap QPixmap::scaleWidth ( int w ) const

Returns a scaled copy of the pixmap. The returned pixmap has a width of w pixels. This function automatically calculates the height of the pixmap so that the ratio of the pixmap is preserved.

If w is 0 or negative a null pixmap is returned.

See also scale(), scaleHeight(), and transform().

bool QPixmap::selfMask () const

Returns true if the pixmap's mask is identical to the pixmap itself; otherwise returns false.

See also mask().

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.

A null pixmap always have a serial number of 0.

An example of where this is useful is for caching QPixmaps.

See also QPixmapCache.

void QPixmap::setDefaultOptimization ( Optimization optimization )   [static]

Sets the default pixmap optimization.

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().

void QPixmap::setMask ( const QBitmap & newmask )

Sets a mask bitmap.

The newmask bitmap defines the clip mask for this pixmap. Every pixel in newmask 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.

Warning: Setting the mask on a pixmap will cause any alpha channel data to be cleared. For example:

    QPixmap alpha("image-with-alpha.png");
    QPixmap alphacopy = alpha;
    alphacopy.setMask(*alphacopy.mask());

Now, alpha and alphacopy are visually different.

Setting a null mask resets the mask.

See also mask(), createHeuristicMask(), and QBitmap.

void QPixmap::setOptimization ( Optimization optimization )

Sets pixmap drawing optimization for this pixmap.

The optimization setting affects pixmap operations, in particular drawing of transparent pixmaps (bitBlt() a pixmap with a mask set) and pixmap transformations (the transform() function).

Pixmap optimization involves keeping intermediate results in a cache buffer and using the cache to speed up QPainter::drawPixmap() and transform(). 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().

QSize QPixmap::size () const

Returns the size of the pixmap.

See also width(), height(), and rect().

QImage QPixmap::toImage () const

Converts the pixmap to a QImage. Returns a null image if it fails.

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.

Note that for the moment, alpha masks on monochrome images are ignored.

See also convertFromImage().

QPixmap QPixmap::transform ( const QMatrix & matrix, Qt::TransformationMode mode = Qt::FastTransformation ) const

Returns a copy of the pixmap that is transformed using matrix. The original pixmap is not changed.

The transformation matrix is internally adjusted to compensate for unwanted translation, i.e. transform() returns the smallest image that contains all the transformed points of the original image.

This function is slow because it involves transformation to a QImage, non-trivial computations and a transformation back to a QPixmap.

See also trueMatrix(), QMatrix, QPainter::setWorldMatrix(), and QImage::transform().

QMatrix QPixmap::trueMatrix ( const QMatrix & m, int w, int h )   [static]

Returns the actual matrix used for transforming a pixmap with w width and h height and matrix m.

When transforming a pixmap with transform(), the transformation matrix is internally adjusted to compensate for unwanted translation, i.e. transform() 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 transform() and QMatrix.

int QPixmap::width () const

Returns the width of the pixmap.

See also height(), size(), and rect().

const QX11Info & QPixmap::x11Info () const

Returns a pointer to a QX11Info object. This pointer is owned by QPixmap and should not be deleted.

Qt::HANDLE QPixmap::xftDrawHandle () const

Returns the Xft draw handle of the pixmap for XRender support. Use of this function is not portable. This function will return 0 if XRender support is not compiled into Qt, if the XRender extension is not supported on the X11 display, or if the handle could not be created.

Qt::HANDLE QPixmap::xftPictureHandle () const

Returns the Xft picture handle of the pixmap for XRender support. Use of this function is not portable. This function will return 0 if XRender support is not compiled into Qt, if the XRender extension is not supported on the X11 display, or if the handle could not be created.

QPixmap::operator QVariant () const

Returns the pixmap as a QVariant

bool QPixmap::operator! () const

Returns true if this is a null pixmap; otherwise returns false.

See also isNull().

QPixmap & QPixmap::operator= ( const QPixmap & pixmap )

Assigns the pixmap pixmap to this pixmap and returns a reference to this pixmap.

QPixmap & QPixmap::operator= ( const QImage & image )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Converts the image image to a pixmap that is assigned to this pixmap. Returns a reference to the pixmap.

See also fromImage() and ..


Related Non-Members

QDataStream & operator>> ( QDataStream & s, QPixmap & pixmap )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Reads a pixmap from the stream s into the pixmap pixmap.

Format of the QDataStream operators

See also QPixmap::load().


Copyright © 2005 Trolltech Trademarks
Qt 4.0.0-b2