QJSEngine
QJSEngine
#include <QJSEngine>
Public Types
enum | Extension { TranslationExtension, ConsoleExtension, GarbageCollectionExtension, AllExtensions } |
flags | Extensions |
Public Functions
QJSEngine() | |
QJSEngine(QObject *parent) | |
virtual | ~QJSEngine() override |
void | collectGarbage() |
QJSValue | evaluate(const QString &program, const QString &fileName = QString(), int lineNumber = 1) |
T | fromScriptValue(const QJSValue &value) |
QJSValue | globalObject() const |
void | installExtensions(QJSEngine::Extensions extensions, const QJSValue &object = QJSValue()) |
QJSValue | newArray(uint length = 0) |
QJSValue | newObject() |
QJSValue | newQMetaObject(const QMetaObject *metaObject) |
QJSValue | newQMetaObject() |
QJSValue | newQObject(QObject *object) |
QJSValue | toScriptValue(const T &value) |
详细说明
QJSEngine类提供了一个用于评估JavaScript代码的环境。
案例1:
QJSEngine myEngine; QJSValue three = myEngine.evaluate("1 + 2");
案例2:
QJSValue fun = myEngine.evaluate("(function(a, b) { return a + b; })"); QJSValueList args; args << 1 << 2; QJSValue threeAgain = fun.call(args);
案例3:
QString fileName = "helloworld.qs"; QFile scriptFile(fileName); if (!scriptFile.open(QIODevice::ReadOnly)) // handle error QTextStream stream(&scriptFile); QString contents = stream.readAll(); scriptFile.close(); myEngine.evaluate(contents, fileName);
##############################
QQ 3087438119