gpt4 book ai didi

qt - 如何存储函数并稍后在 QML JavaScript 中调用它

转载 作者:行者123 更新时间:2023-12-04 03:13:17 27 4
gpt4 key购买 nike

部分代码如下:

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;
}
}
}
}

最佳答案

这对我来说看起来像是一个错误,它只是无法通过模型接口(interface)访问函数。或者可能是设计限制。就函数而言,它是 undefined

你可以像这样解决它:

  property var mod :
[["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")}]]

Column {
Repeater {
id: rep
model: mod
delegate: Button {
text: modelData[0]
onClicked: mod[index][1]()
}
}
}

关于qt - 如何存储函数并稍后在 QML JavaScript 中调用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43175574/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com