本页内容

QPixmapCache Class

QPixmapCache 类为位图提供了全局缓存。更多内容...

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

公共类型

class Key

静态公共成员

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

详细说明

该类是用于通过QPixmap 进行优化绘制的工具。您可以使用它来存储生成成本较高的临时位图,同时不会占用比cacheLimit()更多的存储空间。请使用insert()插入位图,使用find()查找位图,并使用clear()清空缓存。

QPixmapCache 不包含任何成员数据,仅提供用于访问全局位图缓存的静态函数。它会创建一个内部的QCache 对象来缓存位图。

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

如果使用相同的键将两个像素图插入缓存,则最后插入的像素图将替换缓存中先前的像素图。这遵循了QHash 和QCache 类的行为。

当缓存中所有像素图的总大小超过cacheLimit() 指定的值时,缓存即告满。初始缓存限制为 10240 KB(10 MB);您可以通过调用setCacheLimit() 并传入所需值来更改此限制。一个像素图大约占用 (宽度*高度*深度)/8 字节的内存。

《Qt Quarterly》杂志上的文章《使用 QPixmapCache 进行优化》详细介绍了如何利用 QPixmapCache 缓存绘制结果来提升应用程序的运行速度。

注意:QPixmapCache 仅可在应用程序的主线程中使用。来自其他线程的访问将被忽略并返回失败。

另请参阅 QCache 和QPixmap 。

成员函数文档

[static] int QPixmapCache::cacheLimit()

返回缓存限制(单位为千字节)。

默认缓存限制为 10240 KB。

另请参阅 setCacheLimit()。

[static] void QPixmapCache::clear()

从缓存中移除所有像素图。

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

在缓存中查找与给定的key 关联的缓存像素图。如果找到了该像素图,该函数将pixmap 设置为该像素图,并返回true ;否则,它保持pixmap 不变,并返回false 。如果未找到该像素图,则说明key 已失效,因此将释放该空间以供下次插入使用。

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

在缓存中查找与给定的key 关联的缓存像素图。如果找到了该像素图,该函数将pixmap 设置为该像素图,并返回true ;否则,它保持pixmap 不变,并返回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] QPixmapCache::Key QPixmapCache::insert(const QPixmap &pixmap)

将给定的pixmap 的副本插入缓存,并返回一个可用于检索该 的键。

当插入一个像素图时,如果缓存即将超过其上限,则会移除像素图,直到有足够的空间来插入该像素图为止。

当需要更多空间时,最旧的位图(在缓存中最近访问次数最少的)将被删除。

另请参阅 setCacheLimit()。

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

将与key 关联的位图pixmap 的副本插入缓存。

Qt 库插入的所有位图的键名都以“$qt”开头,因此您自己的位图键名绝不能以“$qt”开头。

当插入一个像素图且缓存即将超过其限制时,系统会删除像素图,直到有足够的空间来插入该像素图为止。

当需要更多空间时,最旧的位图(在缓存中最近访问次数最少的)将被删除。

如果对象已插入缓存,该函数返回true ;否则返回false 。

另请参阅 setCacheLimit()。

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

从缓存中移除与 `key ` 关联的像素图,并释放该键以便将来插入。

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

从缓存中删除与key 关联的像素图。

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

将缓存限制设置为n 千字节。

默认设置为 10240 KB。

另请参阅 cacheLimit()。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.