PDA

View Full Version : QPixmapCache missing pixmaps



franki
16th December 2013, 16:08
Hi all,

I'm using QPixmapCache to store some icons (png), icons are then used to display proper pixmap for subclassed QGraphicsPixmapItem.
QPixmapCache is filled once during application start, then each time when I create new QGraphicsPixmapItem, inside constructor of that item I'm doing:


if(!QPixmapCache::find(QString::fromUtf8("icon_%1_state_1").arg(button_type),&pixStandard))
qWarning()<<"ButtonsPixmapItem::ButtonsPixmapItem no pixmap for icon_1_state_1";
if(!QPixmapCache::find(QString::fromUtf8("icon_%1_state2").arg(button_type),&pixShortPress))
qWarning()<<"ButtonsPixmapItem::ButtonsPixmapItem no pixmap for icon_1_state2";
if(!QPixmapCache::find(QString::fromUtf8("icon_%1_state3").arg(button_type),&pixLongPress))
qWarning()<<"ButtonsPixmapItem::ButtonsPixmapItem no pixmap for icon_1_state3";
if(!QPixmapCache::find(QString::fromUtf8("icon_%1_state4").arg(button_type),&pixDisabled))
qWarning()<<"ButtonsPixmapItem::ButtonsPixmapItem no pixmap for icon_1_state4";


After some time, when I'm deleting and creating 20 or 40 QGraphicsPixmapItems, QPixmapCache seems to remove image from the cache, next time I want to find item in cache, I got "false" from QPixmapCache::find and this message.
Is that possible that under some conditions QPixmapCache removes data? Can this be caused by deleted QGraphicsPixmapItems that used this pixmap, found earlier by QPixmapCache::find ?
I'm using Qt 4.8.1 on Linux

best regards
Marek

wysota
16th December 2013, 16:21
Is that possible that under some conditions QPixmapCache removes data?

Well... yeah.... it is a cache, not a dictionary. It has a limited capacity and if the capacity is exceeded, the least recently used data is removed before new data is added to the cache.

franki
16th December 2013, 16:31
Yes..., but... as well as I know, I don't insert anything more when app is running...

Anyway should I change code in such a way that when QPixmapCache::find returns false I'm again loading pixmap with the same key, just to be sure, all needed pixmaps are available?

best regards
Marek

wysota
17th December 2013, 07:38
Yes..., but... as well as I know, I don't insert anything more when app is running...
The cache is global for the whole application so you are sharing it with all Qt code. Try increasing capacity of the cache (which is 10MB on desktop and 2MB on embedded systems by default) and see if it changes anything.


Anyway should I change code in such a way that when QPixmapCache::find returns false I'm again loading pixmap with the same key, just to be sure, all needed pixmaps are available?

If you are using the cache then yes, you should be aware it can tell you it doesn't have the data you need. An alternative is to have a private dictionary for your icons based on QHash<QString,QPixmap>.