Javascript 匿名qt脚本函数的上下文? < >我想从C++中执行匿名Qt脚本函数,但不能计算要使用的QScript上下文。

Javascript 匿名qt脚本函数的上下文? < >我想从C++中执行匿名Qt脚本函数,但不能计算要使用的QScript上下文。,javascript,qt,qtscript,Javascript,Qt,Qtscript,以下是脚本: { otherObject.Text = "Hello World"; setTimeout(function(otherObject) { otherObject.Text = "Goodbye World"; }, 9000 ); } 下面是c++中的setTimeout方法: QScriptValue setTimeout( QScriptContext* ctx, QScriptEngine* eng ) { // How can I obtain the c

以下是脚本:

{
  otherObject.Text = "Hello World";
  setTimeout(function(otherObject) { otherObject.Text = "Goodbye World"; }, 9000 );
}
下面是c++中的setTimeout方法:

QScriptValue setTimeout( QScriptContext* ctx, QScriptEngine* eng )
{
  // How can I obtain the correct context and arg list for the anonymous function here?
}
QScript值对象的调用方法需要上下文和参数列表:

call( ctx->thisObject(), ctx->argumentsObject() );

编辑:上下文可以是全局上下文,但构建参数列表以调用函数似乎是问题的关键。我没有看到任何解释如何从C++构建一个“参数对象”。有一个名为“arguments”的属性,但它似乎没有填写,或者我还不知道如何使用它。

如果我们只看下面的javascript代码:

{
    otherObject.Text = "Hello World";
    setTimeout(function(otherObject) { otherObject.Text = "Goodbye World"; }, 9000 );
}
setTimeout被告知等待9000,然后调用匿名函数。但是,问题是匿名函数有一个参数。setTimeout函数不知道传递给函数的参数是什么

<html>
<script>
var otherObject = 'Hello World';
setTimeout(function(otherObject){alert(otherObject);}, 1000);
</script>
</html>

var otherObject='Hello World';
setTimeout(函数(其他对象){alert(其他对象);},1000);
如果您在chrome中尝试此代码,它将警告undefined,因为setTimeout函数不知道向匿名函数传递什么,它只是不传递任何参数

但如果你尝试这样做:

<html>
<script>
var otherObject = 'Hello World';
setTimeout(function(){alert(otherObject);}, 1000);
</script>
</html>

var otherObject='Hello World';
setTimeout(函数(){alert(otherObject);},1000);
它将提醒Hello world,因为现在otherObject不再是匿名函数的参数,而是与匿名函数形成闭包的局部变量

<html>
<script>
var otherObject = 'Hello World';
setTimeout(function(otherObject){alert(otherObject);}, 1000);
</script>
</html>
因此,为了使代码正常工作,setTimeout函数应该与浏览器的setTimeout函数不同。如果您想从C++侧设置匿名函数的参数,这里可以做如下:

QScriptValue setTimeout( QScriptContext* ctx, QScriptEngine* eng )
{
    QScriptValue anonymous_callback = ctx->argument(0);
    QScriptValue time = ctx->argument(1);
    QThread::sleep(time.toInt32())
    // Then Invoke the anonymous function:
    QScriptValueList args;
    otherObjectProto * otherObjectQtSide = new otherObjectProto();
    otherObjectQtSide.setProperty("Text","Goodbye World!");
    QScriptValue otherObject = engine->newQObject(otherObjectQtSide);// engine is pointer to the QScriptEngine instance
    args << otherObject;
    anonymous_callback.call(QScriptValue(),args);
}
QScriptValue setTimeout(QScriptContext*ctx,QScriptEngine*eng)
{
QScriptValue匿名_callback=ctx->参数(0);
QScript值时间=ctx->参数(1);
QThread::sleep(time.toInt32())
//然后调用匿名函数:
QScriptValueList参数;
otherObjectProto*otherObjectQtSide=新的otherObjectProto();
setProperty(“文本”,“再见世界!”);
QScriptValue otherObject=engine->newQObject(otherObjectQtSide);//engine是指向QScriptEngine实例的指针

args我通过为每个setTimeout调用创建一个子对象来实现计时器函数。在构造函数中,它调用startTimer(delay)。在计时器事件处理程序中,它调用匿名函数。完成后,它调用killTimer()和deleteLater().我想这会让我有我想要的计时器,而且会自己清理。写得很好。太好了!