Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions |

# QPixmapCache Class Reference

The QPixmapCache class provides an application-wide cache for pixmaps. More...

    #include <QPixmapCache>

- List of all members, including inherited members
- Obsolete members

### Static Public Members

- int cacheLimit ()
- void clear ()
- bool find ( const QString & key, QPixmap & pm )
- void insert ( const QString & key, const QPixmap & pm )
- void remove ( const QString & key )
- void setCacheLimit ( int n )

## Detailed Description

The QPixmapCache class provides an application-wide cache for pixmaps.

This class is a tool for optimized drawing with QPixmap. You can use it to store temporary pixmaps that are expensive to generate without using more storage space than cacheLimit(). Use insert() to insert pixmaps, find() to find them, and clear() to empty the cache.

For example, QRadioButton has a non-trivial visual representation so we don't want to regenerate a pixmap whenever a radio button is displayed or changes state. In the function QRadioButton::drawButton(), we do not draw the radio button directly. Instead, we first check the global pixmap cache for a pixmap with the key "$qt_radio_nnn_", where `nnn` is a numerical value that specifies the the radio button state. If a pixmap is found, we bitBlt() it onto the widget and return. Otherwise, we create a new pixmap, draw the radio button in the pixmap, and finally insert the pixmap in the global pixmap cache, using the key above. The bitBlt() is ten times faster than drawing the radio button. All radio buttons in the program share the cached pixmap since QPixmapCache is global.

QPixmapCache contains no member data, only static functions to access the global pixmap cache. It creates an internal QCache object for caching the pixmaps.

The cache associates a pixmap with a string (key). If two pixmaps are inserted into the cache using equal keys, then the last pixmap will hide the first pixmap. The QHash and QCache classes do exactly the same.

The cache becomes full when the total size of all pixmaps in the cache exceeds cacheLimit(). The initial cache limit is 1024 KB (1 MB); it is changed with setCacheLimit(). A pixmap takes roughly (width * height * depth)/8 bytes of memory.

See the QCache documentation for more details about the cache mechanism.

## Member Function Documentation

### int QPixmapCache::cacheLimit () `[static]`

Returns the cache limit (in kilobytes).

The default setting is 1024 kilobytes.

See also setCacheLimit() and ..

### void QPixmapCache::clear () `[static]`

Removes all pixmaps from the cache.

### bool QPixmapCache::find ( const QString & key, QPixmap & pm ) `[static]`

Looks for a cached pixmap associated with the key in the cache. If the pixmap is found, the function sets pm to that pixmap and returns true; otherwise it leaves pm alone and returns false.

Example:

        QPixmap pm;
        if (!QPixmapCache::find("my_big_image", pm)) {
            pm.load("bigimage.png");
            QPixmapCache::insert("my_big_image", pm);
        }
        painter->drawPixmap(0, 0, pm);

### void QPixmapCache::insert ( const QString & key, const QPixmap & pm ) `[static]`

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

Inserts a copy of the pixmap pm associated with the key into the cache.

All pixmaps inserted by the Qt library have a key starting with "$qt", so your own pixmap keys should never begin "$qt".

When a pixmap is inserted and the cache is about to exceed its limit, it removes pixmaps until there is enough room for the pixmap to be inserted.

The oldest pixmaps (least recently accessed in the cache) are deleted when more space is needed.

See also setCacheLimit() and ..

### void QPixmapCache::remove ( const QString & key ) `[static]`

Removes the pixmap associated with key from the cache.

### void QPixmapCache::setCacheLimit ( int n ) `[static]`

Sets the cache limit to n kilobytes.

The default setting is 1024 kilobytes.

See also cacheLimit().