在编写QML中发现一个问题,在多次调用
image.souce = "image://imageprovider"
中,
ImageProvider 类中的
requestImage
() 只执行一次。查官网后发现如下解释。
Images returned by a QQuickImageProvider are automatically cached, similar to any image loaded by the QML engine. When an image with a "image://" prefix is loaded from cache, requestImage() and requestPixmap() will not be called for the relevant image provider. If an image should always be fetched from the image provider, and should not be cached at all, set the cache
property to false
for the relevant Image, BorderImage or AnimatedImage object.
意思是,QQuickImageProvider 返回的类是默认缓存的,如果已经载入图片后, requestImage() 不会再被调用。所以如果图像确定总是要从image provider 中获取,需要将QML中 Image 的 cache 属性至为 false。更新图片需要如下两行语句:
image.source = "";
image.source = "image://images";