Obsolete Members for QScriptEngine

The following members of class QScriptEngine are obsolete. They are provided to keep old source code working. We strongly advise against using them in new code.

Public Functions

(obsolete) bool canEvaluate(const QString &program) const
  • 2 public functions inherited from QObject

Related Non-Members

(obsolete) QScriptValue qScriptValueFromQMetaObject(QScriptEngine *engine)
(obsolete) QScriptValue qScriptValueFromValue(QScriptEngine *engine, const T &value)
(obsolete) T qScriptValueToValue(const QScriptValue &value)

Member Function Documentation

bool QScriptEngine::canEvaluate(const QString &program) const

Returns true if program can be evaluated; i.e. the code is sufficient to determine whether it appears to be a syntactically correct program, or contains a syntax error.

This function returns false if program is incomplete; i.e. the input is syntactically correct up to the point where the input is terminated.

Note that this function only does a static check of program; e.g. it does not check whether references to variables are valid, and so on.

A typical usage of canEvaluate() is to implement an interactive interpreter for Qt Script. The user is repeatedly queried for individual lines of code; the lines are concatened internally, and only when canEvaluate() returns true for the resulting program is it passed to evaluate().

The following are some examples to illustrate the behavior of canEvaluate(). (Note that all example inputs are assumed to have an explicit newline as their last character, since otherwise the Qt Script parser would automatically insert a semi-colon character at the end of the input, and this could cause canEvaluate() to produce different results.)

Given the input

if (hello && world)
    print("hello world");

canEvaluate() will return true, since the program appears to be complete.

Given the input

if (hello &&

canEvaluate() will return false, since the if-statement is not complete, but is syntactically correct so far.

Given the input

0 = 0

canEvaluate() will return true, but evaluate() will throw a SyntaxError given the same input.

Given the input

./test.js

canEvaluate() will return true, even though the code is clearly not syntactically valid Qt Script code. evaluate() will throw a SyntaxError when this code is evaluated.

Given the input

foo["bar"]

canEvaluate() will return true, but evaluate() will throw a ReferenceError if foo is not defined in the script environment.

See also evaluate() and checkSyntax().

Related Non-Members

QScriptValue qScriptValueFromQMetaObject(QScriptEngine *engine)

Uses engine to create a QScriptValue that represents the Qt class T.

This function is equivalent to QScriptEngine::scriptValueFromQMetaObject().

Note: This function was provided as a workaround for MSVC 6 which did not support member template functions. It is advised to use the other form in new code.

This function was introduced in Qt 4.3.

See also QScriptEngine::newQMetaObject().

QScriptValue qScriptValueFromValue(QScriptEngine *engine, const T &value)

Creates a QScriptValue using the given engine with the given value of template type T.

This function is equivalent to QScriptEngine::toScriptValue().

Note: This function was provided as a workaround for MSVC 6 which did not support member template functions. It is advised to use the other form in new code.

This function was introduced in Qt 4.3.

See also QScriptEngine::toScriptValue() and qscriptvalue_cast().

T qScriptValueToValue(const QScriptValue &value)

Returns the given value converted to the template type T.

This function is equivalent to QScriptEngine::fromScriptValue().

Note: This function was provided as a workaround for MSVC 6 which did not support member template functions. It is advised to use the other form in new code.

This function was introduced in Qt 4.3.

See also QScriptEngine::fromScriptValue().

© 2017 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.