QJSEngine ClassThe QJSEngine class provides an environment for evaluating JavaScript code. More... #include <QJSEngine> Inherits: QObject. Inherited by: QQmlEngine. This class was introduced in Qt 5.0. Public Functions
Protected Functions
Additional Inherited Members
Detailed DescriptionThe QJSEngine class provides an environment for evaluating JavaScript code. Evaluating ScriptsUse evaluate() to evaluate script code. evaluate() returns a QJSValue that holds the result of the evaluation. The QJSValue class provides functions for converting the result to various C++ types (e.g. QJSValue::toString() and QJSValue::toNumber()). The following code snippet shows how a script function can be defined and then invoked from C++ using QJSValue::call(): As can be seen from the above snippets, a script is provided to the engine in the form of a string. One common way of loading scripts is by reading the contents of a file and passing it to evaluate(): Here we pass the name of the file as the second argument to evaluate(). This does not affect evaluation in any way; the second argument is a general-purpose string that is used to identify the script for debugging purposes. Engine ConfigurationThe globalObject() function returns the Global Object associated with the script engine. Properties of the Global Object are accessible from any script code (i.e. they are global variables). Typically, before evaluating "user" scripts, you will want to configure a script engine by adding one or more properties to the Global Object: Adding custom properties to the scripting environment is one of the standard means of providing a scripting API that is specific to your application. Usually these custom properties are objects created by the newQObject() or newObject() functions. Script Exceptionsevaluate() can throw a script exception (e.g. due to a syntax error); in that case, the return value is the value that was thrown (typically an Error object). You can check whether the evaluation caused an exception by calling isError() on the return value. If isError() returns true, you can call toString() on the error object to obtain an error message. Script Object CreationUse newObject() to create a JavaScript object; this is the C++ equivalent of the script statement new Object(). You can use the object-specific functionality in QJSValue to manipulate the script object (e.g. QJSValue::setProperty()). Similarly, use newArray() to create a JavaScript array object. QObject IntegrationUse newQObject() to wrap a QObject (or subclass) pointer. newQObject() returns a proxy script object; properties, children, and signals and slots of the QObject are available as properties of the proxy object. No binding code is needed because it is done dynamically using the Qt meta object system. See also QJSValue and Making Applications Scriptable. Member Function Documentation
|