set the initial cache limit of QPixmapCache by passing it to the QCache constructor

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2024-03-28 10:57:05 +02:00
parent cf1b19860d
commit f99b0e1adc

View file

@ -49,10 +49,9 @@ QT_BEGIN_NAMESPACE
behavior of the QHash and QCache classes. behavior of the QHash and QCache classes.
The cache becomes full when the total size of all pixmaps in the The cache becomes full when the total size of all pixmaps in the
cache exceeds cacheLimit(). The initial cache limit is cache exceeds cacheLimit(). The initial cache limit is 10240 KB
2048 KB (2 MB) on embedded platforms, 10240 KB (10 MB) on desktop (10 MB) on desktop, the value can be changed by calling
platforms; you can change this by calling setCacheLimit() with the setCacheLimit() with the required value.
required value.
A pixmap takes roughly (\e{width} * \e{height} * \e{depth})/8 bytes of A pixmap takes roughly (\e{width} * \e{height} * \e{depth})/8 bytes of
memory. memory.
@ -64,10 +63,8 @@ QT_BEGIN_NAMESPACE
\sa QCache, QPixmap \sa QCache, QPixmap
*/ */
static int cache_limit = 10240; // 10 MB cache limit for desktop
typedef QCache<QByteArray, QPixmap> PixmapCacheType; typedef QCache<QByteArray, QPixmap> PixmapCacheType;
Q_GLOBAL_STATIC(PixmapCacheType, pm_cache) Q_GLOBAL_STATIC_WITH_ARGS(PixmapCacheType, pm_cache, (10240))
/*! /*!
\obsolete \obsolete
@ -84,13 +81,11 @@ Q_GLOBAL_STATIC(PixmapCacheType, pm_cache)
Example: Example:
\snippet doc/src/snippets/code/src_gui_image_qpixmapcache.cpp 0 \snippet doc/src/snippets/code/src_gui_image_qpixmapcache.cpp 0
*/ */
QPixmap *QPixmapCache::find(const QByteArray &key) QPixmap *QPixmapCache::find(const QByteArray &key)
{ {
return pm_cache()->object(key); return pm_cache()->object(key);
} }
/*! /*!
\obsolete \obsolete
\fn QPixmapCache::find(const QByteArray &key, QPixmap& pixmap) \fn QPixmapCache::find(const QByteArray &key, QPixmap& pixmap)
@ -108,7 +103,6 @@ QPixmap *QPixmapCache::find(const QByteArray &key)
Example: Example:
\snippet doc/src/snippets/code/src_gui_image_qpixmapcache.cpp 1 \snippet doc/src/snippets/code/src_gui_image_qpixmapcache.cpp 1
*/ */
bool QPixmapCache::find(const QByteArray &key, QPixmap* pixmap) bool QPixmapCache::find(const QByteArray &key, QPixmap* pixmap)
{ {
QPixmap *ptr = pm_cache()->object(key); QPixmap *ptr = pm_cache()->object(key);
@ -121,8 +115,8 @@ bool QPixmapCache::find(const QByteArray &key, QPixmap* pixmap)
Inserts a copy of the pixmap \a pixmap associated with the \a key into Inserts a copy of the pixmap \a pixmap associated with the \a key into
the cache. the cache.
All pixmaps inserted by the Qt library have a key starting with All pixmaps inserted by the Katie library have a key starting with
"$qt", so your own pixmap keys should never begin "$qt". "qt_", so your own pixmap keys should never begin "qt_".
When a pixmap is inserted and the cache is about to exceed its When a pixmap is inserted and the cache is about to exceed its
limit, it removes pixmaps until there is enough room for the limit, it removes pixmaps until there is enough room for the
@ -136,7 +130,6 @@ bool QPixmapCache::find(const QByteArray &key, QPixmap* pixmap)
\sa setCacheLimit() \sa setCacheLimit()
*/ */
bool QPixmapCache::insert(const QByteArray &key, const QPixmap &pixmap) bool QPixmapCache::insert(const QByteArray &key, const QPixmap &pixmap)
{ {
return pm_cache()->insert(key, new QPixmap(pixmap)); return pm_cache()->insert(key, new QPixmap(pixmap));
@ -187,10 +180,9 @@ bool QPixmapCache::replace(const QByteArray &key, const QPixmap &pixmap)
\sa setCacheLimit() \sa setCacheLimit()
*/ */
int QPixmapCache::cacheLimit() int QPixmapCache::cacheLimit()
{ {
return cache_limit; return pm_cache()->maxCost();
} }
/*! /*!
@ -201,11 +193,9 @@ int QPixmapCache::cacheLimit()
\sa cacheLimit() \sa cacheLimit()
*/ */
void QPixmapCache::setCacheLimit(int n) void QPixmapCache::setCacheLimit(int n)
{ {
cache_limit = n; pm_cache()->setMaxCost(n);
pm_cache()->setMaxCost(cache_limit);
} }
/*! /*!
@ -219,7 +209,6 @@ void QPixmapCache::remove(const QByteArray &key)
/*! /*!
Removes all pixmaps from the cache. Removes all pixmaps from the cache.
*/ */
void QPixmapCache::clear() void QPixmapCache::clear()
{ {
pm_cache()->clear(); pm_cache()->clear();