QML ListView由Javascript填充 [英] QML ListView filled by Javascript
问题描述
我刚刚意识到(根据一些QML Bugreport),ListView的JSON代理缺失了。所以我有两个选择,用Javascript或C ++创建的模型来填充
I just realized that (according to some QML Bugreport) there is JSON Delegate for ListView missing. So I have two choices, fill it up by model created in Javascript or C++
特别需要从预定义的URL下载.json数据并将它们解析为ListView。
Specially I need to download .json data from predefined URL and parse them to ListView.
我试图在Javascript中创建对象数组并将关联数组推送到ListView作为模型,但它失败了。无论我如何修改代码。
I tried to create object array in Javascript and push assoc array to ListView as Model, but it failed. No matter how i modified the code.
那么只有C ++解决方案或我可以通过Javascript制作ListView模型吗?
So is there only C++ solution or I can make ListView model by Javascript?
谢谢
我试过的代码:
return [{"name":"value"}]
return {"name":"value"}
return [["name","value"]]
问题始终是: ReferenceError:找不到变量:name
推荐答案
根据mouli@irc.freenode.net#qt的建议,请执行以下操作:
Due to advice from mouli@irc.freenode.net#qt do this:
file:gui.qml
file: gui.qml
import "script.js" as Script
model: ListModel { id: list_model_id }
file:script.js
file: script.js
function makeList(id){
id.append({"name":"value1"});
id.append({"name":"value2"});
}
致电:
Script.makeList(list_model_id)
这篇关于QML ListView由Javascript填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!