【发布时间】:2015-08-25 14:11:49
【问题描述】:
考虑这个 C++ 语句(例如 from docs):
QTimer::singleShot(600000, &app, SLOT(quit()));
如何在 .qml JavaScript 中做同样的事情,像这样的 QML:
Rectangle {
property int counter: 0
onCounterChanged: {
if (counter > 42) {
// do equivalent of above C++ statement here
}
}
// more code, which actually manipulates counter
}
有一个明显的解决方案是拥有单独的Timer,然后由这段 JavaScript 代码启动,如果单行不可行,我会接受它作为答案。是吗?
【问题讨论】:
-
相当于Timer。设置
repeat: false以获得单发行为。 -
repeat实际上默认是 false。 -
好问题。
setTimeout/setInterval是不可能的,因为它们无法使用。您可以将this answer 视为一种 - 相当老套的 - 方法。通过在函数签名中设置参数,您可以将使用减少到一行,例如delay(/*repeat*/ true, 12000, functionName).