QPixmapCache 类

QPixmapCache 类提供应用程序范围像素图缓存。 更多...

头: #include <QPixmapCache>
CMake: find_package(Qt6 COMPONENTS Gui REQUIRED)
target_link_libraries(mytarget PRIVATE Qt6::Gui)
qmake: QT += gui

公共类型

class Key

静态公共成员

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

详细描述

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

QPixmapCache 不包含成员数据,仅静态函数能访问全局像素图缓存。它创建内部 QCache 对象为缓存像素图。

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

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

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

Qt 季刊 文章 优化采用 QPixmapCache 阐述如何使用 QPixmapCache 通过缓存描绘结果来加速应用程序。

注意: QPixmapCache 只可用于应用程序的主线程。从其它线程访问会被忽略并返回故障。

另请参阅 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);
					

[static] bool QPixmapCache:: find (const QPixmapCache::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.

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

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

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

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

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

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

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

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

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

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

将缓存限制设为 n KB。

默认设置为 10240 KB。

另请参阅 cacheLimit ().