有好的 QtScript 教程吗?

发布于 2024-08-16 18:24:14 字数 1539 浏览 7 评论 0原文

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

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

发布评论

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

评论(2

メ斷腸人バ 2024-08-23 18:24:14

听起来你想做的是使用 QScriptEngine::evaluate() 在定义函数的文件上(作为脚本文本传递),然后使用 QScriptEngine 调用它::call()。无需信号或槽。

沿着这些思路(未经测试):

QScriptEngine engine;

// Use evaluate to get a QScriptValue that holds the function
QScriptValue functionSV = engine.evaluate(
    "function cube(x) { return x * x * x; }"
);

// Build an argument list of QScriptValue types to proxy the C++
// types into a format that the script engine can understand
QScriptValueList argsSV;
argsSV << 3;

// Make an empty script value for "this" object when we invoke
// cube(), since it's a global function
QScriptValue thisSV ();

// Call the function, getting back a ScriptValue
QScriptValue resultSV = functionSV.call(thisSV, argsSV);

if (engine.hasUncaughtException() || !resultSV.isNumber()) {
    // The code had an uncaught exception or didn't return
    // the type you were expecting.

    // (...error handling...)

} else {
    // Convert the result to a C++ type
    int result = resultSv.toInt();

    // (...do whatever you want to do w/the result...)
}

请注意,您必须进行大量来回转换。如果您只需要 Qt 中的正则表达式,它们已经存在:QRegExp。示例中包含一个演示:

http://doc.trolltech.com/4.2/工具-regexp.html

It sounds like what you want to do is to use QScriptEngine::evaluate() on that file defining the function (passed in as script text) and then invoke it with QScriptEngine::call(). No signals or slots necessary.

Something along these lines (untested):

QScriptEngine engine;

// Use evaluate to get a QScriptValue that holds the function
QScriptValue functionSV = engine.evaluate(
    "function cube(x) { return x * x * x; }"
);

// Build an argument list of QScriptValue types to proxy the C++
// types into a format that the script engine can understand
QScriptValueList argsSV;
argsSV << 3;

// Make an empty script value for "this" object when we invoke
// cube(), since it's a global function
QScriptValue thisSV ();

// Call the function, getting back a ScriptValue
QScriptValue resultSV = functionSV.call(thisSV, argsSV);

if (engine.hasUncaughtException() || !resultSV.isNumber()) {
    // The code had an uncaught exception or didn't return
    // the type you were expecting.

    // (...error handling...)

} else {
    // Convert the result to a C++ type
    int result = resultSv.toInt();

    // (...do whatever you want to do w/the result...)
}

Note that there's a lot of converting back and forth you'll have to do. If you just want regular expressions in Qt, they already exist: QRegExp. There's a demo included in the samples:

http://doc.trolltech.com/4.2/tools-regexp.html

々眼睛长脚气 2024-08-23 18:24:14

令人惊讶的是,脚本编写这样一个中等复杂的问题被专家们搞得一团糟,以至于没有人理解他们想要做什么。所有成功的教学规则都被违反了,根据用户提出的问题来判断,结果是一场灾难。

脚本是一种表示法,表示特定的通信,在本例中是要执行的操作。该过程需要设计一个翻译词典,请注意,这永远不会完成,只有魔法才会发生,将脚本翻译成预定义的结果。然而,脚本引擎始终需要在获得任何信息之前评估脚本。这显示了白痴的教导。

在展示评估任何脚本的脚本引擎之前,必须向学生展示如何教导引擎执行评估。在我审查过的 15 个脚本示例中,从来没有这样做过。因此,Qt Script 必须按照您的定义发挥作用。除了心灵感应之外,没有其他可能。你已经把自己置于一个令人讨厌的盒子里,所以我希望你知道现在将会发生什么。

It's amazing how such a medium complex issue as scripting has been muddled up by the experts so that no one else understands what they are trying to do. All the successful rules of teaching are violated and the results are a disaster to judge by the questions from users that lead here.

A script is a form of notation that implies a specific communication, in this case, an action to be performed. The process requires that a translation dictionary be designed, note this is never done only magic is supposed to happen, to translate the script into predefined results. Yet, the script engine is always tasked to evaluate the script before it has any information to do so. This displays teaching by morons.

Before you ever show a script engine evaluating any script the student must be shown how to teach the engine to perform the evaluation. This is never done in any of the fifteen examples of scripting that I have reviewed. So, Qt Script must perform magic by your definition. There is no other possibility aside from mental telepathy. You have set yourself inside a nasty box so I hope you know what is coming toward you now.

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