QML在ListView中显示的相机照片被破坏

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了QML在ListView中显示的相机照片被破坏相关的知识,希望对你有一定的参考价值。

我用Camera组件拍照并将它们添加到ListView中。问题是,从一定数量的照片中,在我的测试中大约10张,列表中的一些照片被破坏,我收到以下错误:

QML Image: Failed to get image from provider: image://camera/preview_7
QML Image: Failed to get image from provider: image://camera/preview_4

您可以在图片上看到,与预览4和7相对应的索引为3和6的图像不会显示,但是当创建包含索引的文本元素时,委托仍然存在。

enter image description here

这是代码:

//the Camera
Camera{
  id:camera
  imageCapture {
    onImageCaptured: {
      addPhotoInModel(preview);
    }
  }
}

VideoOutput {
  source: camera
  anchors.fill: parent
}

//the ListView
function addPhotoInModel(image_url)
{
  var imgId= calculateRandomId();

  imagesListModel.append({"photoId": imgId, 
  "value":Qt.resolvedUrl(image_url)});
}

ListView{
  ....
  model:imagesListModel

  delegate:
  Rectangle{
    width:311; height:175;
    Image{
      width:parent.width; height:parent.height
      source:model.value
    }
    Text{anchors.centerIn:parent; text:index}
  }
}

如果我使用本地图像而不是相机中的照片,我没有问题所以我想这可能是缓存问题,但为什么呢?

谢谢。

答案

我使用camera.imageCapture保存在磁盘上的图像解决了它:

Camera{
  id:camera
  imageCapture {
    onImageSaved: {
      addPhotoInModel("file:///"+camera.imageCapture.capturedImagePath);
    }
  }
}

以上是关于QML在ListView中显示的相机照片被破坏的主要内容,如果未能解决你的问题,请参考以下文章

Qt中使用QML和Listview的问题

在 Android 中运行的 QML 相机死了

Phonegap:显示相机胶卷中的照片列表?

QML Listview 选中的项目在点击时突出显示

Qt ListView 不显示 C++ 模型内容

如何从QML中的GridView或ListView获取实例化的委托组件