The QQmlExpression class evaluates JavaScript in a QML context. 更多...
头: | #include <QQmlExpression> |
CMake: |
find_package(Qt6 COMPONENTS Qml REQUIRED)
target_link_libraries(mytarget PRIVATE Qt6::Qml) |
qmake: | QT += qml |
Since: | Qt 5.0 |
继承: | QObject |
QQmlExpression (const QQmlScriptString & script , QQmlContext * ctxt = nullptr, QObject * scope = nullptr, QObject * parent = nullptr) | |
QQmlExpression (QQmlContext * ctxt , QObject * scope , const QString & 表达式 , QObject * parent = nullptr) | |
QQmlExpression () | |
virtual | ~QQmlExpression () override |
void | clearError () |
int | columnNumber () const |
QQmlContext * | context () const |
QQmlEngine * | engine () const |
QQmlError | error () const |
QVariant | evaluate (bool * valueIsUndefined = nullptr) |
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 , int column = 0) |
QString | sourceFile () const |
void | valueChanged () |
For example, given a file
main.qml
像这样:
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
Create a QQmlExpression object that is a child of parent .
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 .
另请参阅 QQmlScriptString .
Create a QQmlExpression object that is a child of parent .
表达式 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 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 .
[signal]
void
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 QQmlExpression::evaluate ()) before this signal will be emitted.
[override virtual]
QQmlExpression::
~QQmlExpression
()
Destroy the QQmlExpression 实例。
Clear any expression errors. Calls to hasError () following this will return false.
Returns the source file column number for this expression. The source location must have been previously set by calling setSourceLocation ().
返回
QQmlContext
this expression is associated with, or
nullptr
if there is no association or the
QQmlContext
已经被销毁。
返回
QQmlEngine
this expression is associated with, or
nullptr
if there is no association or the
QQmlEngine
已经被销毁。
Return any error from the last call to evaluate (). If there was no error, this returns an invalid QQmlError 实例。
另请参阅 hasError () 和 clearError ().
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.
Returns the expression string.
另请参阅 setExpression ().
Returns true if the last call to evaluate () resulted in an error, otherwise false.
另请参阅 error () 和 clearError ().
Returns the source file line number for this expression. The source location must have been previously set by calling setSourceLocation ().
返回 true 若 valueChanged () signal is emitted when the expression's evaluated value changes.
另请参阅 setNotifyOnValueChanged ().
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.
Set the expression to 表达式 .
另请参阅 表达式 ().
Sets whether the valueChanged () signal is emitted when the expression's evaluated value changes.
若 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.
若 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.
另请参阅 notifyOnValueChanged ().
Set the location of this expression to line and column of url . This information is used by the script engine.
Returns the source file URL for this expression. The source location must have been previously set by calling setSourceLocation ().