本文介绍了QML ListModel追加损坏为包含数组的对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ListModel会产生意外且几乎不完整的结果:

ListModel produces unexpected and pretty much broken results when trying to insert a JS object which contains an array:

property ListModel model : ListModel {}

Component.onCompleted: {
  var a = [1, 2, 3, 4, 5]
  console.log(a)
  model.append({"a" : a})
  console.log(model.get(model.count - 1))
  console.log(model.get(model.count - 1).a)

输出为:

qml: [1,2,3,4,5]
qml: QObject(0x3cccd58)
qml: QQmlListModel(0x3cd0978)

但是,如果将数组连接到字符串中,它将按预期工作:

However, if the array is joined into a string, it works as expected:

  console.log(a)
  a = a.join(",")
  model.append({"a" : a})
  console.log(model.get(model.count - 1))
  console.log(model.get(model.count - 1).a)

qml: [1,2,3,4,5]
qml: QObject(0x3d5da60)
qml: 1,2,3,4,5

一些观察-似乎数组已以某种方式转换"为QQmlListModel,并且它是另一个列表模型实例,而不是附加到该实例的实例.同样,最初我虽然确实可以进行一些自动转换,并且希望列表模型包含五个数字,并且实际上count是5,但是get(0)返回一个undefined.因此,尽管大小与数组大小匹配,但没有任何有效内容.

A few observations - it seems like the array is somehow "converted" to a QQmlListModel, and it is another list model instance, not the one that's being appended to. Also, initially I though this might indeed be some auto conversion and expected that list model to contain the five numbers and indeed count is 5, however get(0) returns an undefined. So while the size matches that of the array, there isn't any valid content whatsoever.

我非常确定这是一个错误,但是在提交错误报告之前,我还是要问是否有人知道发生了什么.

I am pretty sure it is a bug, but nevertheless I'd ask if someone knows what is going on before filing a bug report.

推荐答案

来自 ListModel文档:

如果您随后转到文档> :

这篇关于QML ListModel追加损坏为包含数组的对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 01:22