TEST(QPixmapCache, DemonstrateUnwantedCleanup) { QPixmapCache::setCacheLimit(20*1024); //20 MB QPixmapCache::clear(); { QPixmap p1(1000, 1000); //1000*1000*4 = 4 MB EXPECT_TRUE(QPixmapCache::insert("p1", p1)); //p1 goes out of scope but should be copied in QPixmapCache } { QPixmap p2(1000, 1000); //1000*1000*4 EXPECT_TRUE(QPixmapCache::insert("p2", p2)); //p2 goes out of scope but should be copied in QPixmapCache } QPixmap p; EXPECT_TRUE(QPixmapCache::find("p1", p)); EXPECT_TRUE(QPixmapCache::find("p2", p)); //Wait till 30 seconds for QPMCache::flushDetachedPixmaps(false) to be called and drop something from the cache EXPECT_LT(QPixmapCache::totalUsed(), QPixmapCache::cacheLimit()/2); //less than half the cache is used const int iPixmapsInCache = QPixmapCache::allPixmaps().size(); while (QPixmapCache::allPixmaps().size() >= iPixmapsInCache) { QCoreApplication::processEvents(QEventLoop::AllEvents, 50); //allow void QPMCache::timerEvent(QTimerEvent *) to be called } EXPECT_LT(QPixmapCache::totalUsed(), QPixmapCache::cacheLimit()/2); //less than half the cache is used EXPECT_FALSE(QPixmapCache::find("p1", p)); //somehow p1 got removed while there was still plenty of cache left, so why the cleanup ? EXPECT_TRUE(QPixmapCache::find("p2", p)); }