|  | Home · All Classes · Modules

# QDeclarativeExpression Class Reference
[QtDeclarative module]

The QDeclarativeExpression class evaluates JavaScript in a QML context. More...

Inherits QObject.

### Methods

- __init__ (self)
- __init__ (self, QDeclarativeContext, QObject, QString, QObject parent = None)
- clearError (self)
- QDeclarativeContext context (self)
- QDeclarativeEngine engine (self)
- QDeclarativeError error (self)
- (QVariant, bool valueIsUndefined) evaluate (self)
- QString expression (self)
- bool hasError (self)
- int lineNumber (self)
- bool notifyOnValueChanged (self)
- QObject scopeObject (self)
- setExpression (self, QString)
- setNotifyOnValueChanged (self, bool)
- setSourceLocation (self, QString fileName, int line)
- QString sourceFile (self)

### Qt Signals

- void valueChanged ()

## Detailed Description

The QDeclarativeExpression class evaluates JavaScript in a QML context.

For example, given a file `main.qml` like this:

     import Qt 4.7

     Item {
         width: 200; height: 200
     }

The following code evaluates a JavaScript expression in the context of the above QML:

     QDeclarativeEngine *engine = new QDeclarativeEngine;
     QDeclarativeComponent component(engine, QUrl.fromLocalFile("main.qml"));

     QObject *myObject = component.create();
     QDeclarativeExpression *expr = new QDeclarativeExpression(engine->rootContext(), myObject, "width * 2");
     int result = expr->evaluate().toInt();  // result = 400

## Method Documentation

### QDeclarativeExpression.__init__ (self)

Create an invalid QDeclarativeExpression.

As the expression will not have an associated QDeclarativeContext, this will be a null expression object and its value will always be an invalid QVariant.

### QDeclarativeExpression.__init__ (self, QDeclarativeContext, QObject, QString, QObject parent = None)

The parent argument, if not None, causes self to be owned by Qt instead of PyQt.

Create a QDeclarativeExpression object that is a child of parent.

The expression JavaScript will be executed in the ctxt QDeclarativeContext. If specified, the scope object's properties will also be in scope during the expression's execution.

### QDeclarativeExpression.clearError (self)

Clear any expression errors. Calls to hasError() following this will return false.

See also hasError() and error().

### QDeclarativeContext QDeclarativeExpression.context (self)

Returns the QDeclarativeContext this expression is associated with, or 0 if there is no association or the QDeclarativeContext has been destroyed.

### QDeclarativeEngine QDeclarativeExpression.engine (self)

Returns the QDeclarativeEngine this expression is associated with, or 0 if there is no association or the QDeclarativeEngine has been destroyed.

### QDeclarativeError QDeclarativeExpression.error (self)

Return any error from the last call to evaluate(). If there was no error, this returns an invalid QDeclarativeError instance.

See also hasError() and clearError().

### (QVariant, bool valueIsUndefined) QDeclarativeExpression.evaluate (self)

Evaulates the expression, returning the result of the evaluation, or an invalid QVariant if the expression is invalid or has an error.

valueIsUndefined is set to true if the expression resulted in an undefined value.

See also hasError() and error().

### QString QDeclarativeExpression.expression (self)

Returns the expression string.

See also setExpression().

### bool QDeclarativeExpression.hasError (self)

Returns true if the last call to evaluate() resulted in an error, otherwise false.

See also error() and clearError().

### int QDeclarativeExpression.lineNumber (self)

Returns the source file line number for this expression. The source location must have been previously set by calling setSourceLocation().

### bool QDeclarativeExpression.notifyOnValueChanged (self)

Returns true if the valueChanged() signal is emitted when the expression's evaluated value changes.

See also setNotifyOnValueChanged().

### QObject QDeclarativeExpression.scopeObject (self)

Returns the expression's scope object, if provided, otherwise 0.

In addition to data provided by the expression's QDeclarativeContext, the scope object's properties are also in scope during the expression's evaluation.

### QDeclarativeExpression.setExpression (self, QString)

Set the expression to expression.

See also expression().

### QDeclarativeExpression.setNotifyOnValueChanged (self, bool)

Sets whether the valueChanged() signal is emitted when the expression's evaluated value changes.

If notifyOnChange is true, the QDeclarativeExpression will monitor properties involved in the expression's evaluation, and emit QDeclarativeExpression.valueChanged() if they have changed. This allows an application to ensure that any value associated with the result of the expression remains up to date.

If notifyOnChange is false (default), the QDeclarativeExpression will not montitor properties involved in the expression's evaluation, and QDeclarativeExpression.valueChanged() will never be emitted. This is more efficient if an application wants a "one off" evaluation of the expression.

See also notifyOnValueChanged().

### QDeclarativeExpression.setSourceLocation (self, QString fileName, int line)

Set the location of this expression to line of url. This information is used by the script engine.

### QString QDeclarativeExpression.sourceFile (self)

Returns the source file URL for this expression. The source location must have been previously set by calling setSourceLocation().

## Qt Signal Documentation

### void valueChanged ()

This is the default overload of this signal.

Emitted each time the expression value changes from the last time it was evaluated. The expression must have been evaluated at least once (by calling QDeclarativeExpression.evaluate()) before this signal will be emitted.