QQmlExpression#

The QQmlExpression class evaluates JavaScript in a QML context. More

Inheritance diagram of PySide6.QtQml.QQmlExpression

Synopsis#

Functions#

Signals#

Note

This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE

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 PySide6.QtQml.QQmlExpression#

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

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

Parameters:

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 .

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.

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. If provided, ctxt and scope will override the context and scope object provided by script.

See also

QQmlScriptString

PySide6.QtQml.QQmlExpression.clearError()#

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

See also

hasError() error()

PySide6.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() .

PySide6.QtQml.QQmlExpression.context()#
Return type:

PySide6.QtQml.QQmlContext

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

PySide6.QtQml.QQmlExpression.engine()#
Return type:

PySide6.QtQml.QQmlEngine

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

PySide6.QtQml.QQmlExpression.error()#
Return type:

PySide6.QtQml.QQmlError

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

PySide6.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()

PySide6.QtQml.QQmlExpression.expression()#
Return type:

str

Returns the expression string.

See also

setExpression()

PySide6.QtQml.QQmlExpression.hasError()#
Return type:

bool

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

PySide6.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() .

PySide6.QtQml.QQmlExpression.notifyOnValueChanged()#
Return type:

bool

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

PySide6.QtQml.QQmlExpression.scopeObject()#
Return type:

PySide6.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.

PySide6.QtQml.QQmlExpression.setExpression(arg__1)#
Parameters:

arg__1 – str

Set the expression to expression.

See also

expression()

PySide6.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.

PySide6.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.

PySide6.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() .

PySide6.QtQml.QQmlExpression.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 evaluate() ) before this signal will be emitted.