QPixmapCache Class Reference

#include <qpixmapcache.h>

List of all members.


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

The {Qt Quarterly} article {http://doc.trolltech.com/qq/qq12-qpixmapcache.html}{Optimizing with QPixmapCache} explains how to use QPixmapCache to speed up applications by caching the results of painting.

See also:
QCache, QPixmap

Definition at line 33 of file qpixmapcache.h.

Static Public Member Functions

static int cacheLimit ()
static void setCacheLimit (int)
static QPixmapfind (const QString &key)
static bool find (const QString &key, QPixmap &)
static bool insert (const QString &key, const QPixmap &)
static void remove (const QString &key)
static void clear ()


Member Function Documentation

int QPixmapCache::cacheLimit (  )  [static]

Returns the cache limit (in kilobytes).

The default setting is 1024 kilobytes.

See also:
setCacheLimit()

Definition at line 253 of file qpixmapcache.cpp.

References cache_limit.

Referenced by QGraphicsSvgItem::paint().

00254 {
00255     return cache_limit;
00256 }

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

Sets the cache limit to n kilobytes.

The default setting is 1024 kilobytes.

See also:
cacheLimit()

Definition at line 266 of file qpixmapcache.cpp.

References cache_limit.

00267 {
00268     cache_limit = n;
00269     pm_cache()->setMaxCost(1024 * cache_limit);
00270 }

QPixmap * QPixmapCache::find ( const QString key  )  [static]

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. 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 find(const QString&, QPixmap&) instead.
Example:
        QPixmap* pp;
        QPixmap p;
        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);

Definition at line 189 of file qpixmapcache.cpp.

Referenced by cached(), QWindowsXPStylePrivate::drawBackgroundThruNativeBuffer(), QCleanlooksStyle::drawComplexControl(), QPlastiqueStyle::drawComplexControl(), QPlastiqueStyle::drawControl(), QCleanlooksStyle::drawControl(), QPlastiqueStyle::drawPrimitive(), Oubliette::fillTile(), QWindowsStylePrivate::findIcon(), QPixmap::load(), Q3IconView::mask(), QGraphicsSvgItem::paint(), Oubliette::paintEvent(), QPixmapIconEngine::pixmap(), qBrushDark(), qBrushLight(), qBrushSetAlphaF(), qt_patternForAlpha(), qt_pixmapForBrush(), qt_plastique_draw_gradient(), and QItemDelegate::selected().

00190 {
00191     return pm_cache()->object(key);
00192 }

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);

Definition at line 211 of file qpixmapcache.cpp.

References key, and ptr.

00212 {
00213     QPixmap *ptr = pm_cache()->object(key);
00214     if (ptr)
00215         pm = *ptr;
00216     return ptr != 0;
00217 }

bool QPixmapCache::insert ( const QString key,
const QPixmap pm 
) [static]

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.

The function returns true if the object was inserted into the cache; otherwise it returns false.

See also:
setCacheLimit()

Definition at line 240 of file qpixmapcache.cpp.

References QPixmap::depth(), QPixmap::height(), key, and QPixmap::width().

Referenced by cached(), QWindowsXPStylePrivate::drawBackgroundThruNativeBuffer(), QCleanlooksStyle::drawComplexControl(), QPlastiqueStyle::drawComplexControl(), QPlastiqueStyle::drawControl(), QCleanlooksStyle::drawControl(), QPlastiqueStyle::drawPrimitive(), Oubliette::fillTile(), QWindowsStylePrivate::findIcon(), QPixmap::load(), Q3IconView::mask(), QGraphicsSvgItem::paint(), Oubliette::paintEvent(), QPixmapIconEngine::pixmap(), qBrushDark(), qBrushLight(), qBrushSetAlphaF(), qt_patternForAlpha(), qt_pixmapForBrush(), qt_plastique_draw_gradient(), and QItemDelegate::selected().

00241 {
00242     return pm_cache()->insert(key, pm, pm.width() * pm.height() * pm.depth() / 8);
00243 }

Here is the call graph for this function:

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

Removes the pixmap associated with key from the cache.

Definition at line 275 of file qpixmapcache.cpp.

References key.

00276 {
00277     pm_cache()->remove(key);
00278 }

void QPixmapCache::clear (  )  [static]

Removes all pixmaps from the cache.

Definition at line 285 of file qpixmapcache.cpp.

Referenced by qt_cleanup().

00286 {
00287     pm_cache()->clear();
00288 }


The documentation for this class was generated from the following files:
Generated on Thu Mar 15 18:40:43 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1