We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
开发者_如何学C Improve this questionIs there any good QtScript tutorial that isn't about slots or accessing c++ values from script? All I need is one function in external file that uses some regexps on array values and then sends output back to main programm.
I understand, that it can be done using signals/slots, but it looks like overhead and I'm sure there's simplier way.
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
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.
精彩评论