QDeclarativeExpression Class

The QDeclarativeExpression class evaluates JavaScript in a QML context. 更多...

头: #include <QDeclarativeExpression>
Since: Qt 4.7
继承: QObject

公共函数

QDeclarativeExpression ()
QDeclarativeExpression (QDeclarativeContext * ctxt , QObject * scope , const QString & 表达式 , QObject * parent = 0)
virtual ~QDeclarativeExpression ()
void clearError ()
QDeclarativeContext * context () const
QDeclarativeEngine * engine () const
QDeclarativeError error () const
QVariant evaluate (bool * valueIsUndefined = 0)
QString 表达式 () const
bool hasError () const
int lineNumber () const
bool notifyOnValueChanged () const
QObject * scopeObject () const
void setExpression (const QString & 表达式 )
void setNotifyOnValueChanged (bool notifyOnChange )
void setSourceLocation (const QString & url , int line )
QString sourceFile () const

信号

void valueChanged ()

额外继承成员

详细描述

The QDeclarativeExpression class evaluates JavaScript in a QML context.

For example, given a file main.qml 像这样:

import QtQuick 1.0
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
					

成员函数文档编制

QDeclarativeExpression:: QDeclarativeExpression ()

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:: QDeclarativeExpression ( QDeclarativeContext * ctxt , QObject * scope , const QString & 表达式 , QObject * parent = 0)

创建 QDeclarativeExpression object that is a child of parent .

The 表达式 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:: ~QDeclarativeExpression ()

Destroy the QDeclarativeExpression 实例。

void QDeclarativeExpression:: clearError ()

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

另请参阅 hasError () 和 error ().

QDeclarativeContext * QDeclarativeExpression:: context () const

返回 QDeclarativeContext this expression is associated with, or 0 if there is no association or the QDeclarativeContext 已经被销毁。

QDeclarativeEngine * QDeclarativeExpression:: engine () const

返回 QDeclarativeEngine this expression is associated with, or 0 if there is no association or the QDeclarativeEngine 已经被销毁。

QDeclarativeError QDeclarativeExpression:: error () const

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

另请参阅 hasError () 和 clearError ().

QVariant QDeclarativeExpression:: evaluate ( bool * valueIsUndefined = 0)

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.

另请参阅 hasError () 和 error ().

QString QDeclarativeExpression:: 表达式 () const

Returns the expression string.

另请参阅 setExpression ().

bool QDeclarativeExpression:: hasError () const

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

另请参阅 error () 和 clearError ().

int QDeclarativeExpression:: lineNumber () const

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

bool QDeclarativeExpression:: notifyOnValueChanged () const

返回 true 若 valueChanged () signal is emitted when the expression's evaluated value changes.

另请参阅 setNotifyOnValueChanged ().

QObject * QDeclarativeExpression:: scopeObject () const

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.

void QDeclarativeExpression:: setExpression (const QString & 表达式 )

Set the expression to 表达式 .

另请参阅 表达式 ().

void QDeclarativeExpression:: setNotifyOnValueChanged ( bool notifyOnChange )

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

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.

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.

另请参阅 notifyOnValueChanged ().

void QDeclarativeExpression:: setSourceLocation (const QString & url , int line )

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

QString QDeclarativeExpression:: sourceFile () const

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

[signal] void QDeclarativeExpression:: valueChanged ()

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.