<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- qqmlexpression.cpp --> <title>QQmlExpression Class | Qt QML 5.12.6</title> <link rel="stylesheet" type="text/css" href="style/offline-simple.css" /> <script type="text/javascript"> document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css"); // loading style sheet breaks anchors that were jumped to before // so force jumping to anchor again setTimeout(function() { var anchor = location.hash; // need to jump to different anchor first (e.g. none) location.hash = "#"; setTimeout(function() { location.hash = anchor; }, 0); }, 0); </script> </head> <body> <div class="header" id="qtdocheader"> <div class="main"> <div class="main-rounded"> <div class="navigationbar"> <table><tr> <td >Qt 5.12</td><td ><a href="qtqml-index.html">Qt QML</a></td><td ><a href="qtqml-module.html">C++ Classes</a></td><td >QQmlExpression</td></tr></table><table class="buildversion"><tr> <td id="buildversion" width="100%" align="right"><a href="qtqml-index.html">Qt 5.12.6 Reference Documentation</a></td> </tr></table> </div> </div> <div class="content"> <div class="line"> <div class="content mainContent"> <div class="sidebar"> <div class="toc"> <h3><a name="toc">Contents</a></h3> <ul> <li class="level1"><a href="#public-functions">Public Functions</a></li> <li class="level1"><a href="#signals">Signals</a></li> <li class="level1"><a href="#static-public-members">Static Public Members</a></li> <li class="level1"><a href="#details">Detailed Description</a></li> </ul> </div> <div class="sidebar-content" id="sidebar-content"></div></div> <h1 class="title">QQmlExpression Class</h1> <!-- $$$QQmlExpression-brief --> <p>The <a href="qqmlexpression.html">QQmlExpression</a> class evaluates JavaScript in a QML context. <a href="#details">More...</a></p> <!-- @@@QQmlExpression --> <div class="table"><table class="alignedsummary"> <tr><td class="memItemLeft rightAlign topAlign"> Header:</td><td class="memItemRight bottomAlign"> <span class="preprocessor">#include <QQmlExpression></span> </td></tr><tr><td class="memItemLeft rightAlign topAlign"> qmake:</td><td class="memItemRight bottomAlign"> QT += qml</td></tr><tr><td class="memItemLeft rightAlign topAlign"> Since:</td><td class="memItemRight bottomAlign"> Qt 5.0</td></tr></table></div><ul> <li><a href="qqmlexpression-members.html">List of all members, including inherited members</a></li> </ul> <a name="public-functions"></a> <h2 id="public-functions">Public Functions</h2> <div class="table"><table class="alignedsummary"> <tr><td class="memItemLeft rightAlign topAlign"> </td><td class="memItemRight bottomAlign"><b><a href="qqmlexpression.html#QQmlExpression">QQmlExpression</a></b>()</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> </td><td class="memItemRight bottomAlign"><b><a href="qqmlexpression.html#QQmlExpression-1">QQmlExpression</a></b>(QQmlContext *<i>ctxt</i>, QObject *<i>scope</i>, const QString &<i>expression</i>, QObject *<i>parent</i> = nullptr)</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> </td><td class="memItemRight bottomAlign"><b><a href="qqmlexpression.html#QQmlExpression-2">QQmlExpression</a></b>(const QQmlScriptString &<i>script</i>, QQmlContext *<i>ctxt</i> = nullptr, QObject *<i>scope</i> = nullptr, QObject *<i>parent</i> = nullptr)</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> virtual </td><td class="memItemRight bottomAlign"><b><a href="qqmlexpression.html#dtor.QQmlExpression">~QQmlExpression</a></b>() override</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qqmlexpression.html#clearError">clearError</a></b>()</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> int </td><td class="memItemRight bottomAlign"><b><a href="qqmlexpression.html#columnNumber">columnNumber</a></b>() const</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> QQmlContext *</td><td class="memItemRight bottomAlign"><b><a href="qqmlexpression.html#context">context</a></b>() const</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> QQmlEngine *</td><td class="memItemRight bottomAlign"><b><a href="qqmlexpression.html#engine">engine</a></b>() const</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> QQmlError </td><td class="memItemRight bottomAlign"><b><a href="qqmlexpression.html#error">error</a></b>() const</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> QVariant </td><td class="memItemRight bottomAlign"><b><a href="qqmlexpression.html#evaluate">evaluate</a></b>(bool *<i>valueIsUndefined</i> = nullptr)</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> QString </td><td class="memItemRight bottomAlign"><b><a href="qqmlexpression.html#expression">expression</a></b>() const</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> bool </td><td class="memItemRight bottomAlign"><b><a href="qqmlexpression.html#hasError">hasError</a></b>() const</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> int </td><td class="memItemRight bottomAlign"><b><a href="qqmlexpression.html#lineNumber">lineNumber</a></b>() const</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> bool </td><td class="memItemRight bottomAlign"><b><a href="qqmlexpression.html#notifyOnValueChanged">notifyOnValueChanged</a></b>() const</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> QObject *</td><td class="memItemRight bottomAlign"><b><a href="qqmlexpression.html#scopeObject">scopeObject</a></b>() const</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qqmlexpression.html#setExpression">setExpression</a></b>(const QString &<i>expression</i>)</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qqmlexpression.html#setNotifyOnValueChanged">setNotifyOnValueChanged</a></b>(bool <i>notifyOnChange</i>)</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qqmlexpression.html#setSourceLocation">setSourceLocation</a></b>(const QString &<i>url</i>, int <i>line</i>, int <i>column</i> = 0)</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> QString </td><td class="memItemRight bottomAlign"><b><a href="qqmlexpression.html#sourceFile">sourceFile</a></b>() const</td></tr> </table></div> <a name="signals"></a> <h2 id="signals">Signals</h2> <div class="table"><table class="alignedsummary"> <tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qqmlexpression.html#valueChanged">valueChanged</a></b>()</td></tr> </table></div> <a name="static-public-members"></a> <h2 id="static-public-members">Static Public Members</h2> <div class="table"><table class="alignedsummary"> <tr><td class="memItemLeft rightAlign topAlign"> const QMetaObject </td><td class="memItemRight bottomAlign"><b><a href="qqmlexpression.html#staticMetaObject-var">staticMetaObject</a></b></td></tr> </table></div> <a name="details"></a> <!-- $$$QQmlExpression-description --> <div class="descr"> <h2 id="details">Detailed Description</h2> <p>The <a href="qqmlexpression.html">QQmlExpression</a> class evaluates JavaScript in a QML context.</p> <p>For example, given a file <code>main.qml</code> like this:</p> <pre class="qml"> </pre> <p>The following code evaluates a JavaScript expression in the context of the above QML:</p> <pre class="cpp"> <span class="type"><a href="qqmlengine.html">QQmlEngine</a></span> <span class="operator">*</span>engine <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qqmlengine.html">QQmlEngine</a></span>; <span class="type"><a href="qqmlcomponent.html">QQmlComponent</a></span> component(engine<span class="operator">,</span> <span class="type">QUrl</span><span class="operator">::</span>fromLocalFile(<span class="string">"main.qml"</span>)); <span class="type">QObject</span> <span class="operator">*</span>myObject <span class="operator">=</span> component<span class="operator">.</span>create(); <span class="type"><a href="qqmlexpression.html#QQmlExpression">QQmlExpression</a></span> <span class="operator">*</span>expr <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qqmlexpression.html#QQmlExpression">QQmlExpression</a></span>(engine<span class="operator">-</span><span class="operator">></span>rootContext()<span class="operator">,</span> myObject<span class="operator">,</span> <span class="string">"width * 2"</span>); <span class="type">int</span> result <span class="operator">=</span> expr<span class="operator">-</span><span class="operator">></span>evaluate()<span class="operator">.</span>toInt(); <span class="comment">// result = 400</span> </pre> </div> <!-- @@@QQmlExpression --> <div class="func"> <h2>Member Function Documentation</h2> <!-- $$$QQmlExpression[overload1]$$$QQmlExpression --> <h3 class="fn" id="QQmlExpression"><a name="QQmlExpression"></a>QQmlExpression::<span class="name">QQmlExpression</span>()</h3> <p>Create an invalid <a href="qqmlexpression.html">QQmlExpression</a>.</p> <p>As the expression will not have an associated <a href="qqmlcontext.html">QQmlContext</a>, this will be a null expression object and its value will always be an invalid QVariant.</p> <!-- @@@QQmlExpression --> <!-- $$$QQmlExpression$$$QQmlExpressionQQmlContext*QObject*constQString&QObject* --> <h3 class="fn" id="QQmlExpression-1"><a name="QQmlExpression-1"></a>QQmlExpression::<span class="name">QQmlExpression</span>(<span class="type"><a href="qqmlcontext.html">QQmlContext</a></span> *<i>ctxt</i>, <span class="type">QObject</span> *<i>scope</i>, const <span class="type">QString</span> &<i>expression</i>, <span class="type">QObject</span> *<i>parent</i> = nullptr)</h3> <p>Create a <a href="qqmlexpression.html">QQmlExpression</a> object that is a child of <i>parent</i>.</p> <p>The <i>expression</i> JavaScript will be executed in the <i>ctxt</i> <a href="qqmlcontext.html">QQmlContext</a>. If specified, the <i>scope</i> object's properties will also be in scope during the expression's execution.</p> <!-- @@@QQmlExpression --> <!-- $$$QQmlExpression$$$QQmlExpressionconstQQmlScriptString&QQmlContext*QObject*QObject* --> <h3 class="fn" id="QQmlExpression-2"><a name="QQmlExpression-2"></a>QQmlExpression::<span class="name">QQmlExpression</span>(const <span class="type"><a href="qqmlscriptstring.html">QQmlScriptString</a></span> &<i>script</i>, <span class="type"><a href="qqmlcontext.html">QQmlContext</a></span> *<i>ctxt</i> = nullptr, <span class="type">QObject</span> *<i>scope</i> = nullptr, <span class="type">QObject</span> *<i>parent</i> = nullptr)</h3> <p>Create a <a href="qqmlexpression.html">QQmlExpression</a> object that is a child of <i>parent</i>.</p> <p>The <i>script</i> provides the expression to be evaluated, the context to evaluate it in, and the scope object to evaluate it with. If provided, <i>ctxt</i> and <i>scope</i> will override the context and scope object provided by <i>script</i>.</p> <p><b>See also </b><a href="qqmlscriptstring.html">QQmlScriptString</a>.</p> <!-- @@@QQmlExpression --> <!-- $$$~QQmlExpression[overload1]$$$~QQmlExpression --> <h3 class="fn" id="dtor.QQmlExpression"><a name="dtor.QQmlExpression"></a><code>[override virtual] </code>QQmlExpression::<span class="name">~QQmlExpression</span>()</h3> <p>Destroy the <a href="qqmlexpression.html">QQmlExpression</a> instance.</p> <!-- @@@~QQmlExpression --> <!-- $$$clearError[overload1]$$$clearError --> <h3 class="fn" id="clearError"><a name="clearError"></a><span class="type">void</span> QQmlExpression::<span class="name">clearError</span>()</h3> <p>Clear any expression errors. Calls to <a href="qqmlexpression.html#hasError">hasError</a>() following this will return false.</p> <p><b>See also </b><a href="qqmlexpression.html#hasError">hasError</a>() and <a href="qqmlexpression.html#error">error</a>().</p> <!-- @@@clearError --> <!-- $$$columnNumber[overload1]$$$columnNumber --> <h3 class="fn" id="columnNumber"><a name="columnNumber"></a><span class="type">int</span> QQmlExpression::<span class="name">columnNumber</span>() const</h3> <p>Returns the source file column number for this expression. The source location must have been previously set by calling <a href="qqmlexpression.html#setSourceLocation">setSourceLocation</a>().</p> <!-- @@@columnNumber --> <!-- $$$context[overload1]$$$context --> <h3 class="fn" id="context"><a name="context"></a><span class="type"><a href="qqmlcontext.html">QQmlContext</a></span> *QQmlExpression::<span class="name">context</span>() const</h3> <p>Returns the <a href="qqmlcontext.html">QQmlContext</a> this expression is associated with, or 0 if there is no association or the <a href="qqmlcontext.html">QQmlContext</a> has been destroyed.</p> <!-- @@@context --> <!-- $$$engine[overload1]$$$engine --> <h3 class="fn" id="engine"><a name="engine"></a><span class="type"><a href="qqmlengine.html">QQmlEngine</a></span> *QQmlExpression::<span class="name">engine</span>() const</h3> <p>Returns the <a href="qqmlengine.html">QQmlEngine</a> this expression is associated with, or 0 if there is no association or the <a href="qqmlengine.html">QQmlEngine</a> has been destroyed.</p> <!-- @@@engine --> <!-- $$$error[overload1]$$$error --> <h3 class="fn" id="error"><a name="error"></a><span class="type"><a href="qqmlerror.html">QQmlError</a></span> QQmlExpression::<span class="name">error</span>() const</h3> <p>Return any error from the last call to <a href="qqmlexpression.html#evaluate">evaluate</a>(). If there was no error, this returns an invalid <a href="qqmlerror.html">QQmlError</a> instance.</p> <p><b>See also </b><a href="qqmlexpression.html#hasError">hasError</a>() and <a href="qqmlexpression.html#clearError">clearError</a>().</p> <!-- @@@error --> <!-- $$$evaluate[overload1]$$$evaluatebool* --> <h3 class="fn" id="evaluate"><a name="evaluate"></a><span class="type">QVariant</span> QQmlExpression::<span class="name">evaluate</span>(<span class="type">bool</span> *<i>valueIsUndefined</i> = nullptr)</h3> <p>Evaulates the expression, returning the result of the evaluation, or an invalid QVariant if the expression is invalid or has an error.</p> <p><i>valueIsUndefined</i> is set to true if the expression resulted in an undefined value.</p> <p><b>See also </b><a href="qqmlexpression.html#hasError">hasError</a>() and <a href="qqmlexpression.html#error">error</a>().</p> <!-- @@@evaluate --> <!-- $$$expression[overload1]$$$expression --> <h3 class="fn" id="expression"><a name="expression"></a><span class="type">QString</span> QQmlExpression::<span class="name">expression</span>() const</h3> <p>Returns the expression string.</p> <p><b>See also </b><a href="qqmlexpression.html#setExpression">setExpression</a>().</p> <!-- @@@expression --> <!-- $$$hasError[overload1]$$$hasError --> <h3 class="fn" id="hasError"><a name="hasError"></a><span class="type">bool</span> QQmlExpression::<span class="name">hasError</span>() const</h3> <p>Returns true if the last call to <a href="qqmlexpression.html#evaluate">evaluate</a>() resulted in an error, otherwise false.</p> <p><b>See also </b><a href="qqmlexpression.html#error">error</a>() and <a href="qqmlexpression.html#clearError">clearError</a>().</p> <!-- @@@hasError --> <!-- $$$lineNumber[overload1]$$$lineNumber --> <h3 class="fn" id="lineNumber"><a name="lineNumber"></a><span class="type">int</span> QQmlExpression::<span class="name">lineNumber</span>() const</h3> <p>Returns the source file line number for this expression. The source location must have been previously set by calling <a href="qqmlexpression.html#setSourceLocation">setSourceLocation</a>().</p> <!-- @@@lineNumber --> <!-- $$$notifyOnValueChanged[overload1]$$$notifyOnValueChanged --> <h3 class="fn" id="notifyOnValueChanged"><a name="notifyOnValueChanged"></a><span class="type">bool</span> QQmlExpression::<span class="name">notifyOnValueChanged</span>() const</h3> <p>Returns true if the <a href="qqmlexpression.html#valueChanged">valueChanged</a>() signal is emitted when the expression's evaluated value changes.</p> <p><b>See also </b><a href="qqmlexpression.html#setNotifyOnValueChanged">setNotifyOnValueChanged</a>().</p> <!-- @@@notifyOnValueChanged --> <!-- $$$scopeObject[overload1]$$$scopeObject --> <h3 class="fn" id="scopeObject"><a name="scopeObject"></a><span class="type">QObject</span> *QQmlExpression::<span class="name">scopeObject</span>() const</h3> <p>Returns the expression's scope object, if provided, otherwise 0.</p> <p>In addition to data provided by the expression's <a href="qqmlcontext.html">QQmlContext</a>, the scope object's properties are also in scope during the expression's evaluation.</p> <!-- @@@scopeObject --> <!-- $$$setExpression[overload1]$$$setExpressionconstQString& --> <h3 class="fn" id="setExpression"><a name="setExpression"></a><span class="type">void</span> QQmlExpression::<span class="name">setExpression</span>(const <span class="type">QString</span> &<i>expression</i>)</h3> <p>Set the expression to <i>expression</i>.</p> <p><b>See also </b><a href="qqmlexpression.html#expression">expression</a>().</p> <!-- @@@setExpression --> <!-- $$$setNotifyOnValueChanged[overload1]$$$setNotifyOnValueChangedbool --> <h3 class="fn" id="setNotifyOnValueChanged"><a name="setNotifyOnValueChanged"></a><span class="type">void</span> QQmlExpression::<span class="name">setNotifyOnValueChanged</span>(<span class="type">bool</span> <i>notifyOnChange</i>)</h3> <p>Sets whether the <a href="qqmlexpression.html#valueChanged">valueChanged</a>() signal is emitted when the expression's evaluated value changes.</p> <p>If <i>notifyOnChange</i> is true, the <a href="qqmlexpression.html">QQmlExpression</a> will monitor properties involved in the expression's evaluation, and emit <a href="qqmlexpression.html#valueChanged">QQmlExpression::valueChanged</a>() if they have changed. This allows an application to ensure that any value associated with the result of the expression remains up to date.</p> <p>If <i>notifyOnChange</i> is false (default), the <a href="qqmlexpression.html">QQmlExpression</a> will not montitor properties involved in the expression's evaluation, and <a href="qqmlexpression.html#valueChanged">QQmlExpression::valueChanged</a>() will never be emitted. This is more efficient if an application wants a "one off" evaluation of the expression.</p> <p><b>See also </b><a href="qqmlexpression.html#notifyOnValueChanged">notifyOnValueChanged</a>().</p> <!-- @@@setNotifyOnValueChanged --> <!-- $$$setSourceLocation[overload1]$$$setSourceLocationconstQString&intint --> <h3 class="fn" id="setSourceLocation"><a name="setSourceLocation"></a><span class="type">void</span> QQmlExpression::<span class="name">setSourceLocation</span>(const <span class="type">QString</span> &<i>url</i>, <span class="type">int</span> <i>line</i>, <span class="type">int</span> <i>column</i> = 0)</h3> <p>Set the location of this expression to <i>line</i> and <i>column</i> of <i>url</i>. This information is used by the script engine.</p> <!-- @@@setSourceLocation --> <!-- $$$sourceFile[overload1]$$$sourceFile --> <h3 class="fn" id="sourceFile"><a name="sourceFile"></a><span class="type">QString</span> QQmlExpression::<span class="name">sourceFile</span>() const</h3> <p>Returns the source file URL for this expression. The source location must have been previously set by calling <a href="qqmlexpression.html#setSourceLocation">setSourceLocation</a>().</p> <!-- @@@sourceFile --> <!-- $$$valueChanged[overload1]$$$valueChanged --> <h3 class="fn" id="valueChanged"><a name="valueChanged"></a><code>[signal] </code><span class="type">void</span> QQmlExpression::<span class="name">valueChanged</span>()</h3> <p>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 <a href="qqmlexpression.html#evaluate">QQmlExpression::evaluate</a>()) before this signal will be emitted.</p> <!-- @@@valueChanged --> </div> </div> </div> </div> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2019 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners.<br/> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.<br/> 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. </p> </div> </body> </html>