QtScript:如何重新加载当前脚本?
QScriptEngine 有evaluate() 方法,可用于加载脚本、执行脚本以及从已加载的脚本运行指定的函数。但是如何清除当前脚本并加载新脚本呢?例如,我使用评估()从文件加载脚本,然后使用评估()获取脚本函数并调用它们。但是我可以做什么来清除当前脚本并从不同的文件加载新脚本?删除和创建 QScriptEngine 似乎是一个解决方案,但它喜欢在 GUI 线程中创建(由于 QScriptEngineDebugger),而所有脚本操作都在单独的线程中执行。那么有没有办法在不重新创建 QScriptEngine 对象的情况下清除当前脚本?
QScriptEngine has evaluate() method that can be used to load script, execute it and to run a specified function from already loaded script. But how to clear current script and load a new one? For example, i use evaluate() to load a script from file and then evaluate() to get a script functions and call them. But what can i do to clear current script and load a new one from a different file? Deleting and creating QScriptEngine seems like a solution, but it likes to be created in GUI thread (due to QScriptEngineDebugger) while all script operations are performed in separate thread. So is it any way to clear current script without re-creating QScriptEngine object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在评估脚本之前调用pushContext()并在评估新脚本之前调用popContext()将有效地清除所有脚本数据。
Calling pushContext() before evaluating a script and calling popContext() before evaluating a new script will effectively clear all script data.
我遇到了这个问题,并希望改进地狱之眼的答案(顺便说一句,谢谢!),因为它遗漏了一个重要的细节。
我将使用问题的简化版本,其中我重用了 QScriptEngine 对象,并希望确保在评估之间不会留下任何内容。特别是,我想确保
"RC Helicopter Controller"
实体不会调用onEquipped
函数,因为它在装备时不会更改其精灵,并且因此在其脚本文件中没有定义onEquipped
函数。简单地使用pushContext()
和popContext()
根本不会调用任何内容:函数调用似乎发生在原始上下文中,而不是当前上下文中。在查看 QScriptEngine::pushContext() 文档时,我看到您需要显式使用从它返回的上下文,最重要的是,您必须使用 QScriptEngine::activationContext() 访问任何变量:
I ran into this problem, and would like to improve Eye of Hell's answer (thanks, by the way!), as it leaves out an important detail.
I'll use a cut down version of my problem, where I am reusing a
QScriptEngine
object and wanted to ensure that nothing was left behind between evaluations. In particular, I wanted to ensure that theonEquipped
function was not called for the"RC Helicopter Controller"
entity, as it doesn't change its sprite when it is equipped and hence doesn't define anonEquipped
function in its script file. Simply usingpushContext()
andpopContext()
results in nothing being called at all:The function call seems to happen on the original context, not the current one. Upon looking at QScriptEngine::pushContext() documentation, I saw that you need to explicitly use the context returned from it, and on top of that, you must use QScriptEngine::activationContext() to access any variables:
您可以尝试将空对象设置为 http://qt.nokia.com /doc/4.6/qscriptengine.html#setGlobalObject
也许可行。
You could try to set an empty object to http://qt.nokia.com/doc/4.6/qscriptengine.html#setGlobalObject
Maybe that works.