QPixmapCache 类

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

头: #include <QPixmapCache>
qmake: QT += gui

公共类型

class Key
class KeyData

静态公共成员

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

详细描述

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

此类是工具用于优化绘制 QPixmap 。可以使用它来存储生成开销昂贵的临时像素图,而不使用更多存储空间相比 cacheLimit ()。使用 insert () 插入像素图, find () 查找它们,和 clear () 清空缓存。

QPixmapCache contains no member data, only static functions to access the global pixmap cache. It creates an internal QCache 对象为缓存像素图。

缓存关联像素图与作为键由用户提供的字符串,或关联像素图与 QPixmapCache::Key 由缓存生成。使用 QPixmapCache::Key 为键比使用字符串更快。字符串 API 对于复杂键非常方便,但 QPixmapCache::Key API 对于一对一对象到像素图的映射非常高效且方便 - 在这种情况下,可以将键存储作为对象的成员。

若使用相等键将 2 像素图插入缓存,则最后像素图将替换缓存中的第一像素图。这遵循的行为来自 QHash and QCache 类。

缓存变满当缓存中所有像素图的总大小超过 cacheLimit ()。缓存初始限制为 10240 KB (10 MB);可以改变这通过调用 setCacheLimit () 采用要求值。像素图大致需要 ( width * height * depth )/8 字节的内存。

The Qt 季刊 文章 优化采用 QPixmapCache explains how to use QPixmapCache to speed up applications by caching the results of painting.

另请参阅 QCache and QPixmap .

成员函数文档编制

[static] int QPixmapCache:: cacheLimit ()

返回缓存限制 (以 KB 为单位)。

默认缓存限制为 10240 KB。

另请参阅 setCacheLimit ().

[static] void QPixmapCache:: clear ()

从缓存移除所有像素图。

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

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 .

范例:

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

该函数在 Qt 4.6 引入。

[static] bool QPixmapCache:: find (const Key & key , QPixmap * pixmap )

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.

该函数在 Qt 4.6 引入。

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

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.

函数返回 true 若对象被插入缓存;否则它返回 false .

另请参阅 setCacheLimit ().

[static] Key QPixmapCache:: 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.

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.

该函数在 Qt 4.6 引入。

另请参阅 setCacheLimit () 和 replace ().

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

移除像素图关联 key 从缓存中。

[static] void QPixmapCache:: remove (const Key & key )

移除像素图关联 key 从缓存中并释放键以供未来插入。

该函数在 Qt 4.6 引入。

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

替换的像素图关联给定 key 采用 pixmap 指定。返回 true pixmap 已正确插入缓存;否则返回 false .

该函数在 Qt 4.6 引入。

另请参阅 setCacheLimit () 和 insert ().

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

将缓存限制设为 n KB。

默认设置为 10240 KB。

另请参阅 cacheLimit ().