QQmlExpression Class

QQmlExpression 类在 QML 上下文中评估 JavaScript。更多

头文件: #include <QQmlExpression>
CMake: find_package(Qt6 REQUIRED COMPONENTS Qml)
target_link_libraries(mytarget PRIVATE Qt6::Qml)
qmake: QT += qml
继承: QObject

公共函数

QQmlExpression()
QQmlExpression(QQmlContext *ctxt, QObject *scope, const QString &expression, QObject *parent = nullptr)
QQmlExpression(const QQmlScriptString &script, QQmlContext *ctxt = nullptr, QObject *scope = nullptr, QObject *parent = nullptr)
virtual ~QQmlExpression() override
void clearError()
int columnNumber() const
QQmlContext *context() const
QQmlEngine *engine() const
QQmlError error() const
QVariant evaluate(bool *valueIsUndefined = nullptr)
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

信号

void valueChanged()

详细说明

例如,给定的文件main.qml 是这样的:

import QtQuick 2.0

Item {
    width: 200; height: 200
}

以下代码将在上述 QML 的上下文中评估 JavaScript 表达式:

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

成员函数文档

QQmlExpression::QQmlExpression()

创建无效的 QQmlExpression。

由于表达式没有关联的QQmlContext ,因此这将是一个空表达式对象,其值将始终是一个无效的QVariant

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

创建一个作为parent 子对象的 QQmlExpression 对象。

expression JavaScript 将在ctxt QQmlContext 中执行。如果指定,在表达式执行期间,scope 对象的属性也将处于作用域中。

[explicit] QQmlExpression::QQmlExpression(const QQmlScriptString &script, QQmlContext *ctxt = nullptr, QObject *scope = nullptr, QObject *parent = nullptr)

创建一个 QQmlExpression 对象,它是parent 的子对象。

script 提供了要求值的表达式、求值的上下文以及求值的作用域对象。如果提供,ctxtscope 将覆盖script 提供的上下文和作用域对象。

另请参见 QQmlScriptString

[override virtual noexcept] QQmlExpression::~QQmlExpression()

销毁QQmlExpression 实例。

void QQmlExpression::clearError()

清除所有表达式错误。在此之后调用hasError() 将返回 false。

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

int QQmlExpression::columnNumber() const

返回此表达式的源文件列号。源文件位置必须已通过调用setSourceLocation() 设置。

QQmlContext *QQmlExpression::context() const

返回此表达式关联的QQmlContext ,如果没有关联或QQmlContext 已被销毁,则返回nullptr

QQmlEngine *QQmlExpression::engine() const

返回此表达式关联的QQmlEngine ,如果没有关联或QQmlEngine 已被销毁,则返回nullptr

QQmlError QQmlExpression::error() const

返回上次调用evaluate() 时出现的任何错误。如果没有错误,则返回一个无效的QQmlError 实例。

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

QVariant QQmlExpression::evaluate(bool *valueIsUndefined = nullptr)

计算表达式,返回计算结果,如果表达式无效或有错误,则返回无效的QVariant

valueIsUndefined 如果表达式的结果是未定义的值,则将 "true "设置为 "true"。

另请参见 hasError() 和error()。

QString QQmlExpression::expression() const

返回表达式字符串。

另请参见 setExpression()。

bool QQmlExpression::hasError() const

如果最后一次调用evaluate() 导致错误,则返回 true,否则返回 false。

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

int QQmlExpression::lineNumber() const

返回该表达式的源文件行号。源文件位置必须已通过调用setSourceLocation() 设置。

bool QQmlExpression::notifyOnValueChanged() const

如果表达式的求值发生变化时发出valueChanged() 信号,则返回 true。

另请参见 setNotifyOnValueChanged()。

QObject *QQmlExpression::scopeObject() const

返回表达式的作用域对象(如果提供),否则返回 0。

在表达式的求值过程中,除了表达式QQmlContext 提供的数据外,作用域对象的属性也在作用域内。

void QQmlExpression::setExpression(const QString &expression)

将表达式设置为expression

另请参阅 expression() 。

void QQmlExpression::setNotifyOnValueChanged(bool notifyOnChange)

设置表达式的求值发生变化时是否发出valueChanged() 信号。

如果notifyOnChange 为 "true",则QQmlExpression 将监控表达式求值过程中涉及的属性,并在这些属性发生变化时发出QQmlExpression::valueChanged() 信号。这样,应用程序就能确保与表达式结果相关的任何值都是最新的。

如果notifyOnChange 为 false(默认值),则QQmlExpression 不会监控表达式求值过程中涉及的属性,也不会发送QQmlExpression::valueChanged() 。如果应用程序希望对表达式进行 "一次性 "评估,这样做会更有效。

另请参见 notifyOnValueChanged()。

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

将此表达式的位置设置为lineurlcolumn 。脚本引擎会使用这些信息。

QString QQmlExpression::sourceFile() const

返回此表达式的源文件 URL。源文件位置必须已通过调用setSourceLocation() 设置。

[signal] void QQmlExpression::valueChanged()

每次表达式值与上次求值相比发生变化时发出。表达式必须至少被求值过一次(通过调用QQmlExpression::evaluate() ),才会发出该信号。

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.