CopperSpice API  1.7.2
QPixmapCache Class Reference

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

Classes

class  Key
 Class can be used for efficient access to the QPixmapCache More...
 

Static Public Methods

static int cacheLimit ()
 
static void clear ()
 
static bool find (const Key &key, QPixmap *pixmap)
 
static QPixmapfind (const QString &key)
 
static bool find (const QString &key, QPixmap &pixmap)
 
static bool find (const QString &key, QPixmap *pixmap)
 
static Key insert (const QPixmap &pixmap)
 
static bool insert (const QString &key, const QPixmap &pixmap)
 
static void remove (const Key &key)
 
static void remove (const QString &key)
 
static bool replace (const Key &key, const QPixmap &pixmap)
 
static void setCacheLimit (int max)
 

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.

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 user-provided string as a key, or with a QPixmapCache::Key that the cache generates. Using QPixmapCache::Key for keys is faster than using strings. The string API is very convenient for complex keys but the QPixmapCache::Key API will be very efficient and convenient for a one-to-one object-to-pixmap mapping, in this case you can store the keys as members of an object.

If two pixmaps are inserted into the cache using equal keys then the last pixmap will replace the first pixmap in the cache. This follows the behavior of the QHash and QCache classes.

The cache becomes full when the total size of all pixmaps in the cache exceeds cacheLimit(). The initial cache limit is 2048 KB (2 MB) on embedded platforms, 10240 KB (10 MB) on desktop platforms; you can change this by calling setCacheLimit() with the required value. A pixmap takes roughly (width * height * depth)/8 bytes of memory.

See also
QCache, QPixmap

Method Documentation

int QPixmapCache::cacheLimit ( )
static

Returns the cache limit (in kilobytes). The default cache limit is 2048 KB on embedded platforms, 10240 KB on desktop platforms.

See also
setCacheLimit()
void QPixmapCache::clear ( )
static

Removes all pixmaps from the cache.

bool QPixmapCache::find ( const Key key,
QPixmap pixmap 
)
static

Looks for a cached pixmap associated with the given key in the cache. If the pixmap is found, the function sets pixmap to that pixmap and returns true, otherwise it leaves pixmap> alone and returns false. If the pixmap is not found, it means that the key is no longer valid, so it will be released for the next insertion.

QPixmap * QPixmapCache::find ( const QString key)
deprecatedstatic
Deprecated:

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

Warning
If valid, you should copy the pixmap immediately (this is fast). Subsequent insertions into the cache could cause the pointer to become invalid. For this reason, we recommend you use bool find(const QString&, QPixmap*) instead.

Example:

QPixmap* pp;
if ((pp=QPixmapCache::find("my_big_image", pm))) {
p = *pp;
} else {
p.load("bigimage.png");
QPixmapCache::insert("my_big_image", new QPixmap(p));
}
painter->drawPixmap(0, 0, p);
bool QPixmapCache::find ( const QString key,
QPixmap pixmap 
)
deprecatedstatic
bool QPixmapCache::find ( const QString key,
QPixmap pixmap 
)
static

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

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

Inserts a copy of the given pixmap into the cache and returns a key that can be used to retrieve it.

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(), replace()
bool QPixmapCache::insert ( const QString key,
const QPixmap pixmap 
)
static

Inserts a copy of the pixmap pixmap associated with the key into the cache. All pixmaps inserted by the CopperSpice library have a key starting with "$qt" so your own pixmap keys should never begin "$qt".

The method returns true if the object was inserted into the cache, otherwise it returns false.

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::remove ( const Key key)
static

Removes the pixmap associated with key from the cache and releases the key for a future insertion.

void QPixmapCache::remove ( const QString key)
static

Removes the pixmap associated with key from the cache.

bool QPixmapCache::replace ( const Key key,
const QPixmap pixmap 
)
static

Replaces the pixmap associated with the given key with the pixmap specified. Returns true if the pixmap> has been correctly inserted into the cache, otherwise returns false.

See also
setCacheLimit(), insert()
void QPixmapCache::setCacheLimit ( int  max)
static

Sets the cache limit to max kilobytes. The default setting is 2048 KB on embedded platforms, 10240 KB on desktop platforms.

See also
cacheLimit()