Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Classes principales  ·  Annotées  ·  Classes groupées  ·  Modules  ·  Fonctions  · 

QJSEngine Class

The 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

QJSEngine()
QJSEngine(QObject * parent)
virtual ~QJSEngine()
void collectGarbage()
QJSValue evaluate(const QString & program, const QString & fileName = QString(), int lineNumber = 1)
T fromScriptValue(const QJSValue & value)
QJSValue globalObject() const
QJSValue newArray(uint length = 0)
QJSValue newObject()
QJSValue newQObject(QObject * object)
QJSValue toScriptValue(const T & value)
  • 31 public functions inherited from QObject

Protected Functions

QJSEngine(QJSEnginePrivate & dd, QObject * parent = 0)
  • 9 protected functions inherited from QObject

Additional Inherited Members

  • 1 property inherited from QObject
  • 1 public slot inherited from QObject
  • 2 signal inherited from QObject
  • 11 static public members inherited from QObject

Detailed Description

The QJSEngine class provides an environment for evaluating JavaScript code.

Evaluating Scripts

Use 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 Configuration

The 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 Exceptions

evaluate() 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 Creation

Use 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 Integration

Use 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

QJSEngine::QJSEngine()

Constructs a QJSEngine object.

The globalObject() is initialized to have properties as described in ECMA-262, Section 15.1.

QJSEngine::QJSEngine(QObject * parent)

Constructs a QJSEngine object with the given parent.

The globalObject() is initialized to have properties as described in ECMA-262, Section 15.1.

QJSEngine::QJSEngine(QJSEnginePrivate & dd, QObject * parent = 0) [protected]

QJSEngine::~QJSEngine() [virtual]

Destroys this QJSEngine.

void QJSEngine::collectGarbage()

Runs the garbage collector.

The garbage collector will attempt to reclaim memory by locating and disposing of objects that are no longer reachable in the script environment.

Normally you don't need to call this function; the garbage collector will automatically be invoked when the QJSEngine decides that it's wise to do so (i.e. when a certain number of new objects have been created). However, you can call this function to explicitly request that garbage collection should be performed as soon as possible.

QJSValue QJSEngine::evaluate(const QString & program, const QString & fileName = QString(), int lineNumber = 1)

Evaluates program, using lineNumber as the base line number, and returns the result of the evaluation.

The script code will be evaluated in the current context.

The evaluation of program can cause an exception in the engine; in this case the return value will be the exception that was thrown (typically an Error object). You can call isError() on the return value to determine whether an exception occurred.

lineNumber is used to specify a starting line number for program; line number information reported by the engine that pertain to this evaluation will be based on this argument. For example, if program consists of two lines of code, and the statement on the second line causes a script exception, the exception line number would be lineNumber plus one. When no starting line number is specified, line numbers will be 1-based.

fileName is used for error reporting. For example in error objects the file name is accessible through the "fileName" property if it's provided with this function.

T QJSEngine::fromScriptValue(const QJSValue & value)

Returns the given value converted to the template type T.

See also toScriptValue().

QJSValue QJSEngine::globalObject() const

Returns this engine's Global Object.

By default, the Global Object contains the built-in objects that are part of ECMA-262, such as Math, Date and String. Additionally, you can set properties of the Global Object to make your own extensions available to all script code. Non-local variables in script code will be created as properties of the Global Object, as well as local variables in global code.

QJSValue QJSEngine::newArray(uint length = 0)

Creates a JavaScript object of class Array with the given length.

See also newObject().

QJSValue QJSEngine::newObject()

Creates a JavaScript object of class Object.

The prototype of the created object will be the Object prototype object.

See also newArray() and QJSValue::setProperty().

QJSValue QJSEngine::newQObject(QObject * object)

Creates a JavaScript object that wraps the given QObject object, using JavaScriptOwnership. The given options control various aspects of the interaction with the resulting script object.

Signals and slots, properties and children of object are available as properties of the created QJSValue.

If object is a null pointer, this function returns a null value.

If a default prototype has been registered for the object's class (or its superclass, recursively), the prototype of the new script object will be set to be that default prototype.

If the given object is deleted outside of the engine's control, any attempt to access the deleted QObject's members through the JavaScript wrapper object (either by script code or C++) will result in a script exception.

See also QJSValue::toQObject().

QJSValue QJSEngine::toScriptValue(const T & value)

Creates a QJSValue with the given value.

See also fromScriptValue().

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 5.0-snapshot
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web