QQmlExpression

The QQmlExpression class evaluates JavaScript in a QML context. More

Inheritance diagram of PySide2.QtQml.QQmlExpression

Synopsis

Functions

Signals

Detailed Description

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

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
class PySide2.QtQml.QQmlExpression

PySide2.QtQml.QQmlExpression(arg__1, arg__2, arg__3[, arg__4=None])

PySide2.QtQml.QQmlExpression(arg__1[, arg__2=None[, arg__3=None[, arg__4=None]]])

param arg__1

PySide2.QtQml.QQmlContext

param arg__2

PySide2.QtCore.QObject

param arg__3

str

param arg__4

PySide2.QtCore.QObject

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 .

PySide2.QtQml.QQmlExpression.clearError()

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

See also

hasError() error()

PySide2.QtQml.QQmlExpression.columnNumber()
Return type

int

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

PySide2.QtQml.QQmlExpression.context()
Return type

PySide2.QtQml.QQmlContext

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

PySide2.QtQml.QQmlExpression.engine()
Return type

PySide2.QtQml.QQmlEngine

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

PySide2.QtQml.QQmlExpression.error()
Return type

PySide2.QtQml.QQmlError

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

PySide2.QtQml.QQmlExpression.evaluate()
Return type

PyTuple

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() error()

PySide2.QtQml.QQmlExpression.expression()
Return type

str

Returns the expression string.

See also

setExpression()

PySide2.QtQml.QQmlExpression.hasError()
Return type

bool

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

PySide2.QtQml.QQmlExpression.lineNumber()
Return type

int

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

PySide2.QtQml.QQmlExpression.notifyOnValueChanged()
Return type

bool

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

PySide2.QtQml.QQmlExpression.scopeObject()
Return type

PySide2.QtCore.QObject

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.

PySide2.QtQml.QQmlExpression.setExpression(arg__1)
Parameters

arg__1 – str

Set the expression to expression .

See also

expression()

PySide2.QtQml.QQmlExpression.setNotifyOnValueChanged(arg__1)
Parameters

arg__1 – bool

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 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 valueChanged() will never be emitted. This is more efficient if an application wants a “one off” evaluation of the expression.

PySide2.QtQml.QQmlExpression.setSourceLocation(fileName, line[, column=0])
Parameters
  • fileName – str

  • line – int

  • column – int

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

PySide2.QtQml.QQmlExpression.sourceFile()
Return type

str

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

PySide2.QtQml.QQmlExpression.valueChanged()