Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
QPixmapCache Class Reference

\inmodule QtGui More...

#include <qpixmapcache.h>

+ Collaboration diagram for QPixmapCache:

Classes

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

Static Public Member Functions

static int cacheLimit ()
 Returns the cache limit (in kilobytes).
 
static void setCacheLimit (int)
 Sets the cache limit to n kilobytes.
 
static bool find (const QString &key, QPixmap *pixmap)
 Looks for a cached pixmap associated with the given key in the cache.
 
static bool find (const Key &key, QPixmap *pixmap)
 Looks for a cached pixmap associated with the given key in the cache.
 
static bool insert (const QString &key, const QPixmap &pixmap)
 Inserts a copy of the pixmap pixmap associated with the key into the cache.
 
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.
 
static void remove (const QString &key)
 Removes the pixmap associated with key from the cache.
 
static void remove (const Key &key)
 Removes the pixmap associated with key from the cache and releases the key for a future insertion.
 
static void clear ()
 Removes all pixmaps from the cache.
 

Detailed Description

\inmodule QtGui

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 10240 KB (10 MB); 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 \l{http://doc.qt.io/archives/qq/qq12-qpixmapcache.html}{Optimizing with QPixmapCache} explains how to use QPixmapCache to speed up applications by caching the results of painting.

Note
QPixmapCache is only usable from the application's main thread. Access from other threads will be ignored and return failure.
See also
QCache, QPixmap

Definition at line 13 of file qpixmapcache.h.

Member Function Documentation

◆ cacheLimit()

int QPixmapCache::cacheLimit ( )
static

Returns the cache limit (in kilobytes).

The default cache limit is 10240 KB.

See also
setCacheLimit()

Definition at line 539 of file qpixmapcache.cpp.

References qt_pixmapcache_thread_test().

Referenced by QItemDelegate::selectedPixmap().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ clear()

void QPixmapCache::clear ( )
static

Removes all pixmaps from the cache.

Definition at line 591 of file qpixmapcache.cpp.

References QCoreApplication::closingDown(), QT_CATCH, qt_pixmapcache_thread_test(), and QT_TRY.

Referenced by QAndroidStyle::QAndroidStyle(), QApplication::~QApplication(), QGuiApplicationPrivate::~QGuiApplicationPrivate(), and QWindowsVistaStyle::polish().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ find() [1/2]

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

References pixmap, ptr(), and qt_pixmapcache_thread_test().

+ Here is the call graph for this function:

◆ find() [2/2]

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

References pixmap, ptr(), and qt_pixmapcache_thread_test().

Referenced by QGraphicsEffectSource::draw(), QAndroidStyle::AndroidImageDrawable::draw(), QAndroidStyle::Android9PatchDrawable::draw(), QWindowsVistaStylePrivate::drawBackgroundThruNativeBuffer(), QQC2::QWindowsXPStylePrivate::drawBackgroundThruNativeBuffer(), QWindowsVistaStyle::drawControl(), QGraphicsScenePrivate::drawItemHelper(), QWindowsVistaStyle::drawPrimitive(), QQC2::QCommonStyle::drawPrimitive(), QCommonStyle::drawPrimitive(), QWindowsFileIconEngine::filePixmap(), generateWavyPixmap(), QPixmapStylePrivate::getCachedPixmap(), QCommonStylePrivate::iconFromMacTheme(), QPixmap::load(), QQuickUniversalFocusRectangle::paint(), PixmapEntry::pixmap(), QSvgIconEngine::pixmap(), QAbstractFileIconEngine::pixmap(), QGraphicsEffectSource::pixmap(), qt_patternForAlpha(), QPixmapIconEngine::scaledPixmap(), QGraphicsItem::scroll(), QItemDelegate::selectedPixmap(), QQC2::QCommonStyle::standardIcon(), and src_gui_image_qpixmapcache::wrapper0().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ insert() [1/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 504 of file qpixmapcache.cpp.

References cost(), pixmap, and qt_pixmapcache_thread_test().

+ Here is the call graph for this function:

◆ insert() [2/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 482 of file qpixmapcache.cpp.

References cost(), pixmap, and qt_pixmapcache_thread_test().

Referenced by QAndroidStyle::AndroidImageDrawable::draw(), QAndroidStyle::Android9PatchDrawable::draw(), QWindowsVistaStylePrivate::drawBackgroundThruNativeBuffer(), QQC2::QWindowsXPStylePrivate::drawBackgroundThruNativeBuffer(), QWindowsVistaStyle::drawControl(), QGraphicsScenePrivate::drawItemHelper(), QWindowsVistaStyle::drawPrimitive(), QQC2::QCommonStyle::drawPrimitive(), QCommonStyle::drawPrimitive(), QWindowsFileIconEngine::filePixmap(), generateWavyPixmap(), QPixmapStylePrivate::getCachedPixmap(), QCommonStylePrivate::iconFromMacTheme(), QPixmap::load(), QQuickUniversalFocusRectangle::paint(), PixmapEntry::pixmap(), QSvgIconEngine::pixmap(), QAbstractFileIconEngine::pixmap(), QGraphicsEffectSource::pixmap(), qHasPixmapTexture(), qt_patternForAlpha(), QPixmapIconEngine::scaledPixmap(), QGraphicsItem::scroll(), QItemDelegate::selectedPixmap(), QQC2::QCommonStyle::standardIcon(), and src_gui_image_qpixmapcache::wrapper0().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ remove() [1/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 577 of file qpixmapcache.cpp.

References qt_pixmapcache_thread_test().

+ Here is the call graph for this function:

◆ remove() [2/2]

void QPixmapCache::remove ( const QString & key)
static

Removes the pixmap associated with key from the cache.

Definition at line 564 of file qpixmapcache.cpp.

References qt_pixmapcache_thread_test().

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

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setCacheLimit()

void QPixmapCache::setCacheLimit ( int n)
static

Sets the cache limit to n kilobytes.

The default setting is 10240 KB.

See also
cacheLimit()

Definition at line 554 of file qpixmapcache.cpp.

References qt_pixmapcache_thread_test().

Referenced by QItemDelegate::selectedPixmap().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

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