有没有什么好的QtScript教程不是关于槽或者从脚本访问c++值的?我所需要的是外部文件中的一个函数,它对数组值使用一些正则表达式,然后将输出发送回主程序。
我明白,这可以使用信号/插槽来完成,但它看起来需要开销,我相信有更简单的方法。
发布于 2010-01-08 09:56:27
听起来您想要做的是在定义函数的文件上使用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。示例中包含了一个demo:
发布于 2014-02-14 10:38:05
令人惊讶的是,像脚本这样中等复杂的问题是如何被专家搞得一团糟的,以至于没有人明白他们想要做什么。所有成功的教学规则都被违反了,从用户提出的问题来看,结果是一场灾难。
脚本是一种符号形式,它意味着特定的通信,在这种情况下,是要执行的操作。这个过程需要设计一个翻译字典,注意这是永远不会做的,只有魔法才会发生,将脚本翻译成预定义的结果。然而,脚本引擎的任务总是在获得任何信息之前对脚本进行评估。这显示了低能儿的教学。
在展示脚本引擎评估任何脚本之前,必须向学生展示如何教引擎执行评估。在我回顾的15个脚本示例中,从来没有这样做过。因此,Qt脚本必须按照您的定义执行魔法。除了心灵感应之外,别无他法。你已经把自己放进了一个肮脏的盒子里,所以我希望你知道现在会发生什么。
https://stackoverflow.com/questions/2009438
复制相似问题