QPixmapCache Class

QPixmapCache 类为像素图提供了一个应用程序范围的缓存。更多

Header: #include <QPixmapCache>
CMake.QPixmapCache 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 对于一对一的对象到像素图映射非常高效和方便,在这种情况下,可以将键作为对象的成员存储。

如果使用相等的键将两个像素图插入缓存,那么最后一个像素图将取代缓存中的第一个像素图。这与QHashQCache 类的行为一致。

当缓存中所有像素图的总大小超过cacheLimit() 时,缓存就会满。初始缓存限制为 10240 KB(10 MB);您可以通过调用setCacheLimit() 并设置所需数值来更改缓存限制。一个像素图大约占用(宽**)/8 字节内存。

Qt 季刊文章《使用 QPixmapCache 进行优化》解释了如何使用 QPixmapCache 通过缓存绘制结果来加快应用程序的速度。

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

另请参阅 QCacheQPixmap

成员函数文档

[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() 和replace()。

[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 KB。

默认设置为 10240 KB。

另请参阅 cacheLimit().

© 2025 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.