QtScript 中变量影响的问题

发布于 2024-11-02 11:54:03 字数 290 浏览 0 评论 0原文

我试图在 C++ 端获得脚本简单操作的结果。

我创建一个 QScriptValue (myvar) 并调用 engine.globalObject().setProperty("result", myvar); 然后我评估“result = anothervar + 7;”评估方法返回值正常,但变量结果不正常。 如果脚本是“result = anothervar + 7; a=1”,则结果值正常。

它看起来太愚蠢了,不可能是 Qt bug 那么我错过了什么?

谢谢杰夫

I'm trying to get the result of a script simple operation on the C++ side.

I create a QScriptValue (myvar) and call engine.globalObject().setProperty("result", myvar);
Then I evaluate "result = anothervar + 7;" The evaluate method return value is OK but the variable result is not OK.
If the script is "result = anothervar + 7; a=1" then the result value is OK.

It looks too stupid to be a Qt bug so what did I miss ?

Thanks

Jeff

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不必你懂 2024-11-09 11:54:03

根据您对我的评论的回答,我假设您保留了 QScriptValue myvar 实例,并在调用 evaluate() 后查看它:

QScriptEngine e;
QScriptValue myvar( 1.0 );
e.globalObject().setProperty( "result", myvar );
e.globalObject().setProperty( "anotherVar", QScriptValue( 14 ) );
const QScriptValue s = e.evaluate( "result = anotherVar + 7;" );
qDebug() << s.toVariant();
qDebug() << e.globalObject().property("result").toVariant();
qDebug() << myvar.toVariant();

这将打印 2x " QVariant(double,21)”和一次“QVariant(double,1)”。这是预期的行为,原因如下:

在 JavaScript 中,一切都是对象,并且您只处理对对象的引用,而不是对象本身(如果您了解 Java,这类似于 intInteger)。因此,赋值 result = anotherVar + 7; 的作用是将 myvar 表示的对象替换为全局对象的“result”属性,并使用表达式 < 产生的对象。代码>另一个Var + 7。同时,QScriptValue myvar 仍然引用(旧)对象,否则此时该对象将被垃圾收集器抓取。

关于添加 a=1 来解决问题:我无法在此处重现该问题。当然,第一个调试语句打印 a 的值,但第二个和第三个没有改变。

因此,问题的解决方案是在需要时始终从引擎重新获取“结果”属性(使用 engine.globalObject().property("result")),或者——换句话说——QScriptValue 不跟踪分配。

如果您确实想要跟踪分配,则需要将其转换为方法调用:将result实现为带有assign()的自定义类方法,并将赋值 (=) 替换为 result.assign( anotherVal + 7 );

From your answer to my comment, I'm assuming you're keeping the QScriptValue myvar instance around, and look at it after calling evaluate():

QScriptEngine e;
QScriptValue myvar( 1.0 );
e.globalObject().setProperty( "result", myvar );
e.globalObject().setProperty( "anotherVar", QScriptValue( 14 ) );
const QScriptValue s = e.evaluate( "result = anotherVar + 7;" );
qDebug() << s.toVariant();
qDebug() << e.globalObject().property("result").toVariant();
qDebug() << myvar.toVariant();

This will print 2x "QVariant(double,21)" and once "QVariant(double,1)". That is expected behaviour, here's why:

In JavaScript, everything is an object, and you are only dealing with references to objects, not the objects themselves (if you know Java, this is similar to int vs. Integer). So, what the assignment result = anotherVar + 7; does is replace the object represented by myvar as the global object's "result" property with the object that results from the expression anotherVar + 7. Meanwhile, the QScriptValue myvar still references the (old) object, which otherwise would be up for grabs by the garbage-collecter at this point.

About the addition of a=1 to fix the problem: I can't reproduce that here. The first debug statement prints the value of a, of course, but the second and third are unchanged.

The solution to your problem, therefore, is to always re-get the "result" property from the engine whenever you need it (using engine.globalObject().property("result")), or—in other words—QScriptValue doesn't track assigments.

If you do want to track assignment, you need to turn it into a method call: Implement result as a custom class with an assign() method, and replace assignment (=) with result.assign( anotherVal + 7 );.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文