Home · All Namespaces · All Classes · Grouped Classes · Modules · Functions codeless banner

QGlobalPixmapCache Class Reference
[QtBaseModule]

The QGlobalPixmapCache class provides a system-wide cache for pixmaps. More...

    #include <QGlobalPixmapCache>

Static Public Members


Detailed Description

The QGlobalPixmapCache class provides a system-wide cache for pixmaps.

This class is a tool for caching of pixmaps which are likely to be used by multiple processes, or are expensive to generate and may benefit from caching between application executions. For an application-wide QPixmap cache use QPixmapCache.

Use insert() to insert pixmaps, find() to find them, and remove() to remove them from the global cache.

QGlobalPixmapCache contains no member data, only static functions to access the global pixmap cache.

The global cache associates a pixmap with a string (key). If two pixmaps are inserted into the global cache using equal keys, then the last pixmap will hide the first pixmap.

When the global cache becomes full, insert() will fail until adequate space is made available by removing unneeded pixmaps from the global cache, or by deleting all references to the global pixmap's data.

See also QPixmapCache, QPixmap, and QGLOBAL_PIXMAP_CACHE_LIMIT.


Member Function Documentation

bool QGlobalPixmapCache::find ( const QString & key, QPixmap & pixmap )   [static]

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

Internally the global cache ensures that the pixmap is not removed until the pixmap is destroyed, therefore pixmap is directly associated with the QPixmap in the global cache and should not be reused.

Example of correct use:

    {
        QPixmap pm;
        if (!QGlobalPixmapCache::find("background_image", pm)) {
            pm.load("backgroundimage.png");
            QGlobalPixmapCache::insert("background_image", pm);
        }
        painter->drawPixmap(0, 0, pm);
    }

    {
        QPixmap pm;
        if (!QGlobalPixmapCache::find("wallpaper_image", pm)) {
            pm.load("wallpaper.png");
            QGlobalPixmapCache::insert("wallpaper_image", pm);
        }
        painter->drawPixmap(0, 0, pm);
    }

Example of incorrect use:

    QPixmap pm;
    if (!QGlobalPixmapCache::find("background_image", pm)) {
        pm.load("backgroundimage.png");
        QGlobalPixmapCache::insert("background_image", pm);
    }
    painter->drawPixmap(0, 0, pm);

    if (!QGlobalPixmapCache::find("wallpaper_image", pm)) {
        pm.load("wallpaper.png");
        QGlobalPixmapCache::insert("wallpaper_image", pm);
    }
    painter->drawPixmap(0, 0, pm);

bool QGlobalPixmapCache::insert ( const QString & key, const QPixmap & pixmap )   [static]

Inserts the pixmap pixmap associated with the key into the global cache.

All pixmaps inserted by Qt Extended have a key starting with "_$QTOPIA", so your own pixmap keys should never begin with "_$QTOPIA".

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

When the pixmap is no longer required in the global cache, remove() may be called to free space in the global cache.

If two pixmaps are inserted into the global cache using equal keys, then the last pixmap will hide the first pixmap.

void QGlobalPixmapCache::remove ( const QString & key )   [static]

Removes the pixmap associated with the key from the global cache.

Space in the global cache will not be reclaimed unless remove() is called on pixmaps that are not required or all pixmaps referencing the global pixmap data are deleted.

Note that calling remove marks the image for removal, but may not necessarily remove it until some time later. Other callers are permitted to re-reference the pixmap until that time. Consequently, the mere presence of an image in the global pixmap cache should not be used to indicate application state.


Copyright © 2009 Trolltech Trademarks
Qt Extended 4.4.3