Clear QML image cache

2019-06-07 11:43发布

I have a QML Flickable with 3 QML image elements that cycle through 8 image files. My problem is that this uses a lot of memory on a Symbian device with very little. So when someone minimizes the app there isn't a lot of memory left for other apps. I want to know what function I can use on the focus lost event to release the cache. Otherwise Symbian closes my app when the phone runs out of memory.

2条回答
冷血范
2楼-- · 2019-06-07 12:25

Have tried just setting the image element cache property to false ? Or maybe bind the image element cache property to somekind of focus/active property of your application ?

查看更多
可以哭但决不认输i
3楼-- · 2019-06-07 12:43

I just ran into a very similar situation (literally). Apparently the QML cache is hardcoded deep inside the Qt source. I found a relevant posting at http://lists.qt.nokia.com/pipermail/qt-qml/2011-June/002743.html.

Unfortunately, a solution deriving from that link requires modifying your Qt build. I don't know if that's appropriate for your situation.

My current thinking is that the four constants defined at the top of src/declarative/util/qdeclarativepixmapcache.cpp file (lines 66-69, referenced from the above link) control when and why the QML image cache is modified. I'm setting the CACHE_EXPIRE_TIME constant to 0, as further logic inside that file indicates that with the constant set to 0, items in the cache expire immediately, and will therefore be removed. I'll update this answer as things develop.

Edit:

My proposed solution did work. In the QDeclarativePixmapCache.cpp file, change line 68 from

#define CACHE_EXPIRE_TIME 30

to

#define CACHE_EXPIRE_TIME 0

Rebuild Qt and your application, and images will not be cached (or at least, the behavior I'm seeing would indicate that images are no longer being cached). There are a couple of downsides to this solution:

  1. Requires modification to Qt source tree
  2. Unknown performance hit to the overall application - not an issue for me but something to consider.

This solution worked with Qt version 4.7. Good luck!

查看更多
登录 后发表回答