# QPixmapCache Class Reference

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

`#include <qpixmapcache.h>`

List of all member functions.

## Static Public Members

- int cacheLimit ()
- void setCacheLimit ( int )
- QPixmap* find ( const char * key )
- bool find ( const char * key, QPixmap & )
- bool insert ( const char * key, QPixmap * )
- void insert ( const char * key, const QPixmap & )
- void clear ()

## Detailed Description

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

This class is a tool for optimized drawing with QPixmaps. Here follows an example. The QRadioButton widget has a non-trivial visual representation. In the drawButton() function, we do not draw the radio button directly. Instead, we first check the global pixmap cache if a pixmap called "$qt_radio_s_" exists. The `s` 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 10 times faster than drawing the radio button. All radio buttons in the program share the cached pixmap since QPixmapCache is application-global.

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

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

The cache becomes full when the total size of all pixmaps in the cache exceeds the cache limit. The initial cache limit is 1024 KByte (1 MByte). A pixmap takes roughly width*height*depth/8 bytes of memory.

See the QCache documentation for a 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().

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

Removes all pixmaps from the cache.

### QPixmap * QPixmapCache::find ( const char * key ) `[static]`

Returns the pixmap associated with key in the cache, or null if there is no such pixmap.

NOTE: if valid, you should copy the pixmap immediately (this is quick since QPixmaps are implicitly shared), because subsequent insertions into the cache could cause the pointer to become invalid. For this reason, we recommend you use find(const char*, QPixmap&) instead.

Example:

        QPixmap* pp;
        QPixmap p;
        if ( (pp=QPixmapCache::find("my_previous_copy", pm)) ) {
            p = *pp;
        } else {
            p.load("bigimage.gif");
            QPixmapCache::insert("my_previous_copy", new QPixmap(p));
        }
        painter->drawPixmap(0, 0, p);

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

Sets pm to the cached pixmap associated with key in the cache and returns TRUE. If FALSE is returned, no cached copy was found, and pm is unchanged.

Example:

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

### bool QPixmapCache::insert ( const char * key, QPixmap * pm ) `[static]`

Inserts the pixmap pm associated with key into the cache. Returns TRUE if successful, or FALSE if the pixmap is too big for the cache.

NOTE: If this function returns FALSE, you must delete pm yourself. Additionally, be very careful about using pm after calling this function, as any other insertions into the cache, from anywhere in the application, or within Qt itself, could cause the pixmap to be discarded from the cache, and the pointer to become invalid.

Due to these dangers, we strongly recommend that you use insert(const char*, const QPixmap&) instead.

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

Inserts a copy of the pixmap pm associated with key into the cache. Returns TRUE if successful, or FALSE if the pixmap is too big for the cache.

All pixmaps inserted by the Qt library have a key starting with "$qt..". Use something else for you pixmaps.

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

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

Sets the cache limit to n kilobytes.

The default setting is 1024 kilobytes.

See also: cacheLimit().

Search the documentation, FAQ, qt-interest archive and more (uses www.troll.no):

This file is part of the Qt toolkit, copyright © 1995-99 Troll Tech, all rights reserved.

It was generated from the following files:

- qpixmapcache.h: 1998/08/19
- qpixmapcache.cpp: 1998/07/03