问题:
在iconmodel下显示带图片的item,当item数量过多时就不会有图片显示了
这个widget图片过多还会导致其他页面的图片显示不出来
断点分析:
在加载第300多张图片的时候,pixmap.loadFromData就是空了
解密函数返回的数据是正常的,那么就是在loadFromData这一步出的问题
尝试方案:
我放大加载图片的尺寸,发现可见得图片更少了
于是猜测这个LoadFromData申请的buffer的尺寸是一定的,而不是可以无限大
这时可以去Qt文档里找找看有没有相关内容
阅读QT文档时看到这样一句话
Note that QPixmaps are automatically added to the QPixmapCache when loaded from a file in main thread; the key used is internal and cannot be acquired.
这么看来Qt应该是专为Qpixmap提供了缓冲区来存储临时的像素图
继续跟踪文档查看QPixmapCache相关解释
里面有这么一句话:
The cache becomes full when the total size of all pixmaps in the cache exceeds cacheLimit(). The initial cache limit is 10240 KB (10 MB); you can change this by calling setCacheLimit() with the required value. A pixmap takes roughly (width * height * depth)/8 bytes of memory.
初始缓存限制是10240 KB (10 MB),那么超过这个限制应该就会导致接下来的图片无法显示
查看这个类发现可以调整缓冲区大小的静态函数:
[static] void QPixmapCache::setCacheLimit(int n)
Sets the cache limit to n kilobytes.
The default setting is 10240 KB.
See also cacheLimit().
让我们来把缓冲区扩大10倍,然后再来看一下结果
当然,这个限制是不能随便设置的,需要根据机器的显存与内存来定
如何获取机器的可用资源信息并设置理想的QPixmapCache :: cacheLimit,有时间我会再来研究这个问题
QPixmapCache::setCacheLimit(102400);
很好,问题依旧没有解决
看来问题没有我想的那么简单,我还是 naive了
试了网上说的方法用QImage替代QPixmap加载图片,问题依旧
QImage image;
image.loadFromData(Decrypt_file2char(model.imagePath));
QPainter painter(this);
QPixmap pixmapToShow = QPixmap::fromImage( image.scaled(size(), Qt::KeepAspectRatio));
painter.drawPixmap(0,0, pixmapToShow);
绝望,等我有头绪再来更新把
参考文章:
https://blog.csdn.net/jiong_1988/article/details/7472159
https://www.wenjiangs.com/doc/ckzx5y0u
http://blog.chinaunix.net/uid-22674777-id-333550.html