以下是代码的一部分:
Repeater{
model: [["Text A", function(){console.log("hello A")}],
["Text B", function(){console.log("hello B")}],
["Text C", function(){console.log("hello C")}],
["Text D", function(){console.log("hello D")}]]
delegate: Button{
text: modelData[0]
onClicked: modelData[1](); // Type Error
}
}
我希望为每个按钮提供不同的行为。我认为它应该与原生JavaScript相同。
var func = function(){
//...
}
func();
如何在QML JavaScript中实现此功能?
顺便提一下,我的解决方案是:
Repeater{
model: ["Text A",
"Text B",
"Text C",
"Text D"]
delegate: Button{
text: modelData
onClicked: {
switch(index)
{
cast 0:
console.log("hello A")
break;
cast 1:
console.log("hello B")
break;
cast 2:
console.log("hello C")
break;
cast 3:
console.log("hello D")
break;
}
}
}
}