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  · 

QQmlExpression Class

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

 #include <QQmlExpression>

Inherits: QObject.

This class was introduced in Qt 5.0.

Public Functions

QQmlExpression()
QQmlExpression(QQmlContext * ctxt, QObject * scope, const QString & expression, QObject * parent = 0)
QQmlExpression(const QQmlScriptString & script, QObject * parent = 0)
virtual ~QQmlExpression()
void clearError()
int columnNumber() const
QQmlContext * context() const
QQmlEngine * engine() const
QQmlError error() const
QVariant evaluate(bool * valueIsUndefined = 0)
QString expression() const
bool hasError() const
int lineNumber() const
bool notifyOnValueChanged() const
QObject * scopeObject() const
void setExpression(const QString & expression)
void setNotifyOnValueChanged(bool notifyOnChange)
void setSourceLocation(const QString & url, int line, int column = 0)
QString sourceFile() const
  • 31 public functions inherited from QObject

Signals

void valueChanged()

Additional Inherited Members

  • 1 property inherited from QObject
  • 1 public slot inherited from QObject
  • 11 static public members inherited from QObject
  • 9 protected functions inherited from QObject

Detailed Description

The QQmlExpression class evaluates JavaScript in a QML context.

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

 import QtQuick 2.0

 Item {
     width: 200; height: 200
 }

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

 QQmlEngine *engine = new QQmlEngine;
 QQmlComponent component(engine, QUrl::fromLocalFile("main.qml"));

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

Note that the QtQuick 1 version is called QDeclarativeExpression.

Member Function Documentation

QQmlExpression::QQmlExpression()

Create an invalid QQmlExpression.

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

QQmlExpression::QQmlExpression(QQmlContext * ctxt, QObject * scope, const QString & expression, QObject * parent = 0)

Create a QQmlExpression object that is a child of parent.

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

QQmlExpression::QQmlExpression(const QQmlScriptString & script, QObject * parent = 0)

Create a QQmlExpression object that is a child of parent.

The script provides the expression to be evaluated, the context to evaluate it in, and the scope object to evaluate it with.

This constructor is functionally equivalent to the following, but in most cases is more efficient.

 QQmlExpression expression(script.context(), script.scopeObject(), script.script(), parent);

See also QQmlScriptString.

QQmlExpression::~QQmlExpression() [virtual]

Destroy the QQmlExpression instance.

void QQmlExpression::clearError()

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

See also hasError() and error().

int QQmlExpression::columnNumber() const

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

QQmlContext * QQmlExpression::context() const

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

QQmlEngine * QQmlExpression::engine() const

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

QQmlError QQmlExpression::error() const

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

See also hasError() and clearError().

QVariant QQmlExpression::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.

See also hasError() and error().

QString QQmlExpression::expression() const

Returns the expression string.

See also setExpression().

bool QQmlExpression::hasError() const

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

See also error() and clearError().

int QQmlExpression::lineNumber() const

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

bool QQmlExpression::notifyOnValueChanged() const

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

See also setNotifyOnValueChanged().

QObject * QQmlExpression::scopeObject() const

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

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

void QQmlExpression::setExpression(const QString & expression)

Set the expression to expression.

See also expression().

void QQmlExpression::setNotifyOnValueChanged(bool notifyOnChange)

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

If notifyOnChange is true, the QQmlExpression will monitor properties involved in the expression's evaluation, and emit QQmlExpression::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 QQmlExpression will not montitor properties involved in the expression's evaluation, and QQmlExpression::valueChanged() will never be emitted. This is more efficient if an application wants a "one off" evaluation of the expression.

See also notifyOnValueChanged().

void QQmlExpression::setSourceLocation(const QString & url, int line, int column = 0)

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

QString QQmlExpression::sourceFile() const

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

void QQmlExpression::valueChanged() [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 QQmlExpression::evaluate()) before this signal will be emitted.

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