Qt 4.8
Classes | Static Public Functions | List of all members
QPixmapCache Class Reference

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

#include <qpixmapcache.h>

Classes

class  Key
 The QPixmapCache::Key class can be used for efficient access to the QPixmapCache. More...
 
class  KeyData
 

Static Public Functions

static int cacheLimit ()
 Returns the cache limit (in kilobytes). More...
 
static void clear ()
 Removes all pixmaps from the cache. More...
 
static QPixmapfind (const QString &key)
 
static bool find (const QString &key, QPixmap &pixmap)
 
static bool find (const QString &key, QPixmap *pixmap)
 Looks for a cached pixmap associated with the given key in the cache. More...
 
static bool find (const Key &key, QPixmap *pixmap)
 Looks for a cached pixmap associated with the given key in the cache. More...
 
static bool insert (const QString &key, const QPixmap &pixmap)
 Inserts a copy of the pixmap pixmap associated with the key into the cache. More...
 
static Key insert (const QPixmap &pixmap)
 Inserts a copy of the given pixmap into the cache and returns a key that can be used to retrieve it. More...
 
static void remove (const QString &key)
 Removes the pixmap associated with key from the cache. More...
 
static void remove (const Key &key)
 Removes the pixmap associated with key from the cache and releases the key for a future insertion. More...
 
static bool replace (const Key &key, const QPixmap &pixmap)
 Replaces the pixmap associated with the given key with the pixmap specified. More...
 
static void setCacheLimit (int)
 Sets the cache limit to n kilobytes. More...
 

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.

The Qt Quarterly article 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 57 of file qpixmapcache.h.

Functions

◆ cacheLimit()

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

Definition at line 620 of file qpixmapcache.cpp.

Referenced by QItemDelegate::selected().

621 {
622  return cache_limit;
623 }
static int cache_limit

◆ clear()

void QPixmapCache::clear ( )
static

Removes all pixmaps from the cache.

Definition at line 666 of file qpixmapcache.cpp.

Referenced by qt_cleanup(), QGtkStyle::unpolish(), and QGtkStyleUpdateScheduler::updateTheme().

667 {
668  QT_TRY {
669  pm_cache()->clear();
670  } QT_CATCH(const std::bad_alloc &) {
671  // if we ran out of memory during pm_cache(), it's no leak,
672  // so just ignore it.
673  }
674 }
#define QT_CATCH(A)
Definition: qglobal.h:1537
#define QT_TRY
Definition: qglobal.h:1536

◆ find() [1/4]

QPixmap * QPixmapCache::find ( const QString key)
static
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);

Definition at line 493 of file qpixmapcache.cpp.

Referenced by QGraphicsEffectSource::draw(), QWindowsXPStylePrivate::drawBackgroundThruNativeBuffer(), QMacStylePrivate::drawColorlessButton(), QMacStylePrivate::drawCombobox(), QPlastiqueStyle::drawComplexControl(), QCleanlooksStyle::drawComplexControl(), QWindowsVistaStyle::drawControl(), QPlastiqueStyle::drawControl(), QCleanlooksStyle::drawControl(), QGraphicsScenePrivate::drawItemHelper(), QWindowsVistaStyle::drawPrimitive(), QPlastiqueStyle::drawPrimitive(), QWindowsStyle::drawPrimitive(), QGtkStyle::drawPrimitive(), QMacStylePrivate::drawTableHeader(), QDeclarativeRectangle::generateBorderedRect(), QDeclarativeRectangle::generateRoundedRect(), generateWavyPixmap(), QFileIconProviderPrivate::getWinIcon(), QPixmap::load(), QGtkPainter::paintArrow(), QGtkPainter::paintBox(), QGtkPainter::paintBoxGap(), QGtkPainter::paintCheckbox(), QGtkPainter::paintExpander(), QGtkPainter::paintExtention(), QGtkPainter::paintFlatBox(), QGtkPainter::paintFocus(), QGtkPainter::paintHandle(), QGtkPainter::paintHline(), QGtkPainter::paintOption(), QGtkPainter::paintResizeGrip(), QGtkPainter::paintShadow(), QGtkPainter::paintSlider(), QGtkPainter::paintVline(), QSvgIconEngine::pixmap(), QGraphicsEffectSource::pixmap(), PixmapEntry::pixmap(), QPixmapIconEngine::pixmap(), qBrushDark(), qBrushLight(), qBrushSetAlphaF(), qDrawPlainRect(), qt_mac_constructQIconFromIconRef(), qt_patternForAlpha(), qt_pixmapForBrush(), qt_plastique_draw_gradient(), QGraphicsItem::scroll(), and QItemDelegate::selected().

494 {
495  return pm_cache()->object(key);
496 }

◆ find() [2/4]

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

Use bool find(const QString&, QPixmap*) instead.

Definition at line 505 of file qpixmapcache.cpp.

506 {
507  return find(key, &pixmap);
508 }
static QPixmap * find(const QString &key)

◆ find() [3/4]

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.

Since
4.6

Example:

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 521 of file qpixmapcache.cpp.

522 {
523  QPixmap *ptr = pm_cache()->object(key);
524  if (ptr && pixmap)
525  *pixmap = *ptr;
526  return ptr != 0;
527 }
const T * ptr(const T &t)
The QPixmap class is an off-screen image representation that can be used as a paint device...
Definition: qpixmap.h:71

◆ find() [4/4]

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.

Since
4.6

Definition at line 538 of file qpixmapcache.cpp.

539 {
540  //The key is not valid anymore, a flush happened before probably
541  if (!key.d || !key.d->isValid)
542  return false;
543  QPixmap *ptr = pm_cache()->object(key);
544  if (ptr && pixmap)
545  *pixmap = *ptr;
546  return ptr != 0;
547 }
const T * ptr(const T &t)
int key
The QPixmap class is an off-screen image representation that can be used as a paint device...
Definition: qpixmap.h:71

◆ insert() [1/2]

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 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 569 of file qpixmapcache.cpp.

Referenced by QWindowsXPStylePrivate::drawBackgroundThruNativeBuffer(), QMacStylePrivate::drawColorlessButton(), QMacStylePrivate::drawCombobox(), QPlastiqueStyle::drawComplexControl(), QCleanlooksStyle::drawComplexControl(), QWindowsVistaStyle::drawControl(), QPlastiqueStyle::drawControl(), QCleanlooksStyle::drawControl(), QGraphicsScenePrivate::drawItemHelper(), QWindowsVistaStyle::drawPrimitive(), QPlastiqueStyle::drawPrimitive(), QWindowsStyle::drawPrimitive(), QGtkStyle::drawPrimitive(), QMacStylePrivate::drawTableHeader(), QDeclarativeRectangle::generateBorderedRect(), QDeclarativeRectangle::generateRoundedRect(), generateWavyPixmap(), QFileIconProviderPrivate::getWinIcon(), QPixmap::load(), QGtkPainter::paintArrow(), QGtkPainter::paintBox(), QGtkPainter::paintBoxGap(), QGtkPainter::paintCheckbox(), QGtkPainter::paintExpander(), QGtkPainter::paintExtention(), QGtkPainter::paintFlatBox(), QGtkPainter::paintFocus(), QGtkPainter::paintHandle(), QGtkPainter::paintHline(), QGtkPainter::paintOption(), QGtkPainter::paintResizeGrip(), QGtkPainter::paintShadow(), QGtkPainter::paintSlider(), QGtkPainter::paintVline(), QSvgIconEngine::pixmap(), QGraphicsEffectSource::pixmap(), PixmapEntry::pixmap(), QPixmapIconEngine::pixmap(), qBrushDark(), qBrushLight(), qBrushSetAlphaF(), qDrawPlainRect(), qt_mac_constructQIconFromIconRef(), qt_patternForAlpha(), qt_pixmapForBrush(), qt_plastique_draw_gradient(), QGraphicsItem::scroll(), and QItemDelegate::selected().

570 {
571  return pm_cache()->insert(key, pixmap, pixmap.width() * pixmap.height() * pixmap.depth() / 8);
572 }
int width() const
Returns the width of the pixmap.
Definition: qpixmap.cpp:630
int depth() const
Returns the depth of the pixmap.
Definition: qpixmap.cpp:695
int height() const
Returns the height of the pixmap.
Definition: qpixmap.cpp:645

◆ insert() [2/2]

QPixmapCache::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()
Since
4.6

Definition at line 589 of file qpixmapcache.cpp.

590 {
591  return pm_cache()->insert(pixmap, pixmap.width() * pixmap.height() * pixmap.depth() / 8);
592 }
int width() const
Returns the width of the pixmap.
Definition: qpixmap.cpp:630
int depth() const
Returns the depth of the pixmap.
Definition: qpixmap.cpp:695
int height() const
Returns the height of the pixmap.
Definition: qpixmap.cpp:645

◆ remove() [1/2]

void QPixmapCache::remove ( const QString key)
static

Removes the pixmap associated with key from the cache.

Definition at line 643 of file qpixmapcache.cpp.

Referenced by QGraphicsScenePrivate::drawItemHelper(), QGraphicsEffectSourcePrivate::invalidateCache(), QGraphicsItemCache::purge(), and QGraphicsItem::scroll().

644 {
645  pm_cache()->remove(key);
646 }

◆ remove() [2/2]

void QPixmapCache::remove ( const Key key)
static

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

Since
4.6

Definition at line 654 of file qpixmapcache.cpp.

655 {
656  //The key is not valid anymore, a flush happened before probably
657  if (!key.d || !key.d->isValid)
658  return;
659  pm_cache()->remove(key);
660 }
int key

◆ replace()

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()
Since
4.6

Definition at line 603 of file qpixmapcache.cpp.

604 {
605  //The key is not valid anymore, a flush happened before probably
606  if (!key.d || !key.d->isValid)
607  return false;
608  return pm_cache()->replace(key, pixmap, pixmap.width() * pixmap.height() * pixmap.depth() / 8);
609 }
int width() const
Returns the width of the pixmap.
Definition: qpixmap.cpp:630
int depth() const
Returns the depth of the pixmap.
Definition: qpixmap.cpp:695
int key
int height() const
Returns the height of the pixmap.
Definition: qpixmap.cpp:645

◆ setCacheLimit()

void QPixmapCache::setCacheLimit ( int  n)
static

Sets the cache limit to n kilobytes.

The default setting is 2048 KB on embedded platforms, 10240 KB on desktop platforms.

See also
cacheLimit()

Definition at line 634 of file qpixmapcache.cpp.

Referenced by QItemDelegate::selected().

635 {
636  cache_limit = n;
637  pm_cache()->setMaxCost(1024 * cache_limit);
638 }
static int cache_limit

The documentation for this class was generated from the following files: