Sophie

Sophie

distrib > Mageia > 7 > i586 > media > core-updates > by-pkgid > 6e2327ca1c896c6d674ae53117299f21 > files > 60

qtdeclarative5-doc-5.12.6-1.mga7.noarch.rpm

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- qjsengine.cpp -->
  <title>QJSEngine 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 >QJSEngine</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-types">Public Types</a></li>
<li class="level1"><a href="#public-functions">Public Functions</a></li>
<li class="level1"><a href="#static-public-members">Static Public Members</a></li>
<li class="level1"><a href="#related-non-members">Related Non-Members</a></li>
<li class="level1"><a href="#details">Detailed Description</a></li>
<li class="level2"><a href="#evaluating-scripts">Evaluating Scripts</a></li>
<li class="level2"><a href="#engine-configuration">Engine Configuration</a></li>
<li class="level2"><a href="#script-exceptions">Script Exceptions</a></li>
<li class="level2"><a href="#script-object-creation">Script Object Creation</a></li>
<li class="level2"><a href="#qobject-integration">QObject Integration</a></li>
<li class="level2"><a href="#extensions">Extensions</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">QJSEngine Class</h1>
<!-- $$$QJSEngine-brief -->
<p>The <a href="qjsengine.html">QJSEngine</a> class provides an environment for evaluating JavaScript code. <a href="#details">More...</a></p>
<!-- @@@QJSEngine -->
<div class="table"><table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> Header:</td><td class="memItemRight bottomAlign">   <span class="preprocessor">#include &lt;QJSEngine&gt;</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><tr><td class="memItemLeft rightAlign topAlign"> Inherited By:</td><td class="memItemRight bottomAlign"> <p><a href="qqmlengine.html">QQmlEngine</a></p>
</td></tr></table></div><ul>
<li><a href="qjsengine-members.html">List of all members, including inherited members</a></li>
<li><a href="qjsengine-obsolete.html">Obsolete members</a></li>
</ul>
<p><b>Note:</b> All functions in this class are reentrant.</p>
<a name="public-types"></a>
<h2 id="public-types">Public Types</h2>
<div class="table"><table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> enum </td><td class="memItemRight bottomAlign"><b><a href="qjsengine.html#Extension-enum">Extension</a></b> { TranslationExtension, ConsoleExtension, GarbageCollectionExtension, AllExtensions }</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> flags </td><td class="memItemRight bottomAlign"><b><a href="qjsengine.html#Extension-enum">Extensions</a></b></td></tr>
</table></div>
<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="qjsengine.html#QJSEngine">QJSEngine</a></b>()</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> </td><td class="memItemRight bottomAlign"><b><a href="qjsengine.html#QJSEngine-1">QJSEngine</a></b>(QObject *<i>parent</i>)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> virtual </td><td class="memItemRight bottomAlign"><b><a href="qjsengine.html#dtor.QJSEngine">~QJSEngine</a></b>() override</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qjsengine.html#collectGarbage">collectGarbage</a></b>()</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> QJSValue </td><td class="memItemRight bottomAlign"><b><a href="qjsengine.html#evaluate">evaluate</a></b>(const QString &amp;<i>program</i>, const QString &amp;<i>fileName</i> = QString(), int <i>lineNumber</i> = 1)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> T </td><td class="memItemRight bottomAlign"><b><a href="qjsengine.html#fromScriptValue">fromScriptValue</a></b>(const QJSValue &amp;<i>value</i>)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> QJSValue </td><td class="memItemRight bottomAlign"><b><a href="qjsengine.html#globalObject">globalObject</a></b>() const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> QJSValue </td><td class="memItemRight bottomAlign"><b><a href="qjsengine.html#importModule">importModule</a></b>(const QString &amp;<i>fileName</i>)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qjsengine.html#installExtensions">installExtensions</a></b>(QJSEngine::Extensions <i>extensions</i>, const QJSValue &amp;<i>object</i> = QJSValue())</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> QJSValue </td><td class="memItemRight bottomAlign"><b><a href="qjsengine.html#newArray">newArray</a></b>(uint <i>length</i> = 0)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> QJSValue </td><td class="memItemRight bottomAlign"><b><a href="qjsengine.html#newErrorObject">newErrorObject</a></b>(QJSValue::ErrorType <i>errorType</i>, const QString &amp;<i>message</i> = QString())</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> QJSValue </td><td class="memItemRight bottomAlign"><b><a href="qjsengine.html#newObject">newObject</a></b>()</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> QJSValue </td><td class="memItemRight bottomAlign"><b><a href="qjsengine.html#newQMetaObject">newQMetaObject</a></b>(const QMetaObject *<i>metaObject</i>)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> QJSValue </td><td class="memItemRight bottomAlign"><b><a href="qjsengine.html#newQMetaObject-1">newQMetaObject</a></b>()</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> QJSValue </td><td class="memItemRight bottomAlign"><b><a href="qjsengine.html#newQObject">newQObject</a></b>(QObject *<i>object</i>)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qjsengine.html#throwError">throwError</a></b>(const QString &amp;<i>message</i>)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qjsengine.html#throwError-1">throwError</a></b>(QJSValue::ErrorType <i>errorType</i>, const QString &amp;<i>message</i> = QString())</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> QJSValue </td><td class="memItemRight bottomAlign"><b><a href="qjsengine.html#toScriptValue">toScriptValue</a></b>(const T &amp;<i>value</i>)</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="qjsengine.html#staticMetaObject-var">staticMetaObject</a></b></td></tr>
</table></div>
<a name="related-non-members"></a>
<h2 id="related-non-members">Related Non-Members</h2>
<div class="table"><table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> QJSEngine *</td><td class="memItemRight bottomAlign"><b><a href="qjsengine.html#qjsEnginex">qjsEngine</a></b>(const QObject *<i>object</i>)</td></tr>
</table></div>
<a name="details"></a>
<!-- $$$QJSEngine-description -->
<div class="descr">
<h2 id="details">Detailed Description</h2>
<p>The <a href="qjsengine.html">QJSEngine</a> class provides an environment for evaluating JavaScript code.</p>
<a name="evaluating-scripts"></a>
<h3 id="evaluating-scripts">Evaluating Scripts</h3>
<p>Use <a href="qjsengine.html#evaluate">evaluate</a>() to evaluate script code.</p>
<pre class="cpp">

  <span class="type"><a href="qjsengine.html#QJSEngine">QJSEngine</a></span> myEngine;
  <span class="type"><a href="qjsvalue.html">QJSValue</a></span> three <span class="operator">=</span> myEngine<span class="operator">.</span>evaluate(<span class="string">&quot;1 + 2&quot;</span>);

</pre>
<p><a href="qjsengine.html#evaluate">evaluate</a>() returns a <a href="qjsvalue.html">QJSValue</a> that holds the result of the evaluation. The <a href="qjsvalue.html">QJSValue</a> class provides functions for converting the result to various C++ types (e.g&#x2e; <a href="qjsvalue.html#toString">QJSValue::toString</a>() and <a href="qjsvalue.html#toNumber">QJSValue::toNumber</a>()).</p>
<p>The following code snippet shows how a script function can be defined and then invoked from C++ using <a href="qjsvalue.html#call">QJSValue::call</a>():</p>
<pre class="cpp">

  <span class="type"><a href="qjsvalue.html">QJSValue</a></span> fun <span class="operator">=</span> myEngine<span class="operator">.</span>evaluate(<span class="string">&quot;(function(a, b) { return a + b; })&quot;</span>);
  <span class="type"><a href="qjsvalue.html#QJSValueList-typedef">QJSValueList</a></span> args;
  args <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="number">1</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="number">2</span>;
  <span class="type"><a href="qjsvalue.html">QJSValue</a></span> threeAgain <span class="operator">=</span> fun<span class="operator">.</span>call(args);

</pre>
<p>As can be seen from the above snippets, a script is provided to the engine in the form of a string. One common way of loading scripts is by reading the contents of a file and passing it to <a href="qjsengine.html#evaluate">evaluate</a>():</p>
<pre class="cpp">

  <span class="type">QString</span> fileName <span class="operator">=</span> <span class="string">&quot;helloworld.qs&quot;</span>;
  <span class="type">QFile</span> scriptFile(fileName);
  <span class="keyword">if</span> (<span class="operator">!</span>scriptFile<span class="operator">.</span>open(<span class="type">QIODevice</span><span class="operator">::</span>ReadOnly))
      <span class="comment">// handle error</span>
  <span class="type">QTextStream</span> stream(<span class="operator">&amp;</span>scriptFile);
  <span class="type">QString</span> contents <span class="operator">=</span> stream<span class="operator">.</span>readAll();
  scriptFile<span class="operator">.</span>close();
  myEngine<span class="operator">.</span>evaluate(contents<span class="operator">,</span> fileName);

</pre>
<p>Here we pass the name of the file as the second argument to <a href="qjsengine.html#evaluate">evaluate</a>(). This does not affect evaluation in any way; the second argument is a general-purpose string that is stored in the <code>Error</code> object for debugging purposes.</p>
<p>For larger pieces of functionality, you may want to encapsulate your code and data into modules. A module is a file that contains script code, variables, etc., and uses export statements to describe its interface towards the rest of the application. With the help of import statements, a module can refer to functionality from other modules. This allows building a scripted application from smaller connected building blocks in a safe way. In contrast, the approach of using <a href="qjsengine.html#evaluate">evaluate</a>() carries the risk that internal variables or functions from one <a href="qjsengine.html#evaluate">evaluate</a>() call accidentally pollute the global object and affect subsequent evaluations.</p>
<p>The following example provides a module that can add numbers:</p>
<pre class="cpp">

  <span class="keyword">export</span> function sum(left<span class="operator">,</span> right)
  {
      <span class="keyword">return</span> left <span class="operator">+</span> right
  }

</pre>
<p>This module can be loaded with QJSEngine::import() if it is saved under the name <code>math.mjs</code>:</p>
<pre class="cpp">

  <span class="type">QJSvalue</span> module <span class="operator">=</span> myEngine<span class="operator">.</span>importModule(<span class="string">&quot;./math.mjs&quot;</span>);
  <span class="type"><a href="qjsvalue.html">QJSValue</a></span> sumFunction <span class="operator">=</span> module<span class="operator">.</span>property(<span class="string">&quot;sum&quot;</span>);
  <span class="type"><a href="qjsvalue.html">QJSValue</a></span> result <span class="operator">=</span> sumFunction<span class="operator">.</span>call(args);

</pre>
<p>Modules can also use functionality from other modules using import statements:</p>
<pre class="cpp">

  import { sum } from <span class="string">&quot;./math.mjs&quot;</span>;
  <span class="keyword">export</span> function addTwice(left<span class="operator">,</span> right)
  {
      <span class="keyword">return</span> sum(left<span class="operator">,</span> right) <span class="operator">*</span> <span class="number">2</span>;
  }

</pre>
<a name="engine-configuration"></a>
<h3 id="engine-configuration">Engine Configuration</h3>
<p>The <a href="qjsengine.html#globalObject">globalObject</a>() function returns the <b>Global Object</b> associated with the script engine. Properties of the Global Object are accessible from any script code (i.e&#x2e; they are global variables). Typically, before evaluating &quot;user&quot; scripts, you will want to configure a script engine by adding one or more properties to the Global Object:</p>
<pre class="cpp">

  myEngine<span class="operator">.</span>globalObject()<span class="operator">.</span>setProperty(<span class="string">&quot;myNumber&quot;</span><span class="operator">,</span> <span class="number">123</span>);
  <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
  <span class="type"><a href="qjsvalue.html">QJSValue</a></span> myNumberPlusOne <span class="operator">=</span> myEngine<span class="operator">.</span>evaluate(<span class="string">&quot;myNumber + 1&quot;</span>);

</pre>
<p>Adding custom properties to the scripting environment is one of the standard means of providing a scripting API that is specific to your application. Usually these custom properties are objects created by the <a href="qjsengine.html#newQObject">newQObject</a>() or <a href="qjsengine.html#newObject">newObject</a>() functions.</p>
<a name="script-exceptions"></a>
<h3 id="script-exceptions">Script Exceptions</h3>
<p><a href="qjsengine.html#evaluate">evaluate</a>() can throw a script exception (e.g&#x2e; due to a syntax error). If it does, then <a href="qjsengine.html#evaluate">evaluate</a>() returns the value that was thrown (typically an <code>Error</code> object). Use <a href="qjsvalue.html#isError">QJSValue::isError</a>() to check for exceptions.</p>
<p>For detailed information about the error, use <a href="qjsvalue.html#toString">QJSValue::toString</a>() to obtain an error message, and use <a href="qjsvalue.html#property">QJSValue::property</a>() to query the properties of the <code>Error</code> object. The following properties are available:</p>
<ul>
<li><code>name</code></li>
<li><code>message</code></li>
<li><code>fileName</code></li>
<li><code>lineNumber</code></li>
<li><code>stack</code></li>
</ul>
<pre class="cpp">

  <span class="type"><a href="qjsvalue.html">QJSValue</a></span> result <span class="operator">=</span> myEngine<span class="operator">.</span>evaluate(<span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>);
  <span class="keyword">if</span> (result<span class="operator">.</span>isError())
      qDebug()
              <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;Uncaught exception at line&quot;</span>
              <span class="operator">&lt;</span><span class="operator">&lt;</span> result<span class="operator">.</span>property(<span class="string">&quot;lineNumber&quot;</span>)<span class="operator">.</span>toInt()
              <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;:&quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> result<span class="operator">.</span>toString();

</pre>
<a name="script-object-creation"></a>
<h3 id="script-object-creation">Script Object Creation</h3>
<p>Use <a href="qjsengine.html#newObject">newObject</a>() to create a JavaScript object; this is the C++ equivalent of the script statement <code>new Object()</code>. You can use the object-specific functionality in <a href="qjsvalue.html">QJSValue</a> to manipulate the script object (e.g&#x2e; <a href="qjsvalue.html#setProperty">QJSValue::setProperty</a>()). Similarly, use <a href="qjsengine.html#newArray">newArray</a>() to create a JavaScript array object.</p>
<a name="qobject-integration"></a>
<h3 id="qobject-integration">QObject Integration</h3>
<p>Use <a href="qjsengine.html#newQObject">newQObject</a>() to wrap a QObject (or subclass) pointer. <a href="qjsengine.html#newQObject">newQObject</a>() returns a proxy script object; properties, children, and signals and slots of the QObject are available as properties of the proxy object. No binding code is needed because it is done dynamically using the Qt meta object system.</p>
<pre class="cpp">

  <span class="type">QPushButton</span> <span class="operator">*</span>button <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QPushButton</span>;
  <span class="type"><a href="qjsvalue.html">QJSValue</a></span> scriptButton <span class="operator">=</span> myEngine<span class="operator">.</span>newQObject(button);
  myEngine<span class="operator">.</span>globalObject()<span class="operator">.</span>setProperty(<span class="string">&quot;button&quot;</span><span class="operator">,</span> scriptButton);

  myEngine<span class="operator">.</span>evaluate(<span class="string">&quot;button.checkable = true&quot;</span>);

  qDebug() <span class="operator">&lt;</span><span class="operator">&lt;</span> scriptButton<span class="operator">.</span>property(<span class="string">&quot;checkable&quot;</span>)<span class="operator">.</span>toBool();
  scriptButton<span class="operator">.</span>property(<span class="string">&quot;show&quot;</span>)<span class="operator">.</span>call(); <span class="comment">// call the show() slot</span>

</pre>
<p>Use <a href="qjsengine.html#newQMetaObject-1">newQMetaObject</a>() to wrap a QMetaObject; this gives you a &quot;script representation&quot; of a QObject-based class. <a href="qjsengine.html#newQMetaObject-1">newQMetaObject</a>() returns a proxy script object; enum values of the class are available as properties of the proxy object.</p>
<p>Constructors exposed to the meta-object system (using Q_INVOKABLE) can be called from the script to create a new QObject instance with JavaScriptOwnership. For example, given the following class definition:</p>
<pre class="cpp">

  <span class="keyword">class</span> MyObject : <span class="keyword">public</span> <span class="type">QObject</span>
  {
      Q_OBJECT

  <span class="keyword">public</span>:
      Q_INVOKABLE MyObject() {}
  };

</pre>
<p>The <code>staticMetaObject</code> for the class can be exposed to JavaScript like so:</p>
<pre class="cpp">

  <span class="type"><a href="qjsvalue.html">QJSValue</a></span> jsMetaObject <span class="operator">=</span> engine<span class="operator">.</span>newQMetaObject(<span class="operator">&amp;</span>MyObject<span class="operator">::</span>staticMetaObject);
  engine<span class="operator">.</span>globalObject()<span class="operator">.</span>setProperty(<span class="string">&quot;MyObject&quot;</span><span class="operator">,</span> jsMetaObject);

</pre>
<p>Instances of the class can then be created in JavaScript:</p>
<pre class="cpp">

  engine<span class="operator">.</span>evaluate(<span class="string">&quot;var myObject = new MyObject()&quot;</span>);

</pre>
<p><b>Note: </b>Currently only classes using the Q_OBJECT macro are supported; it is not possible to expose the <code>staticMetaObject</code> of a Q_GADGET class to JavaScript.</p><a name="dynamic-qobject-properties"></a>
<h4 id="dynamic-qobject-properties">Dynamic QObject Properties</h4>
<p>Dynamic QObject properties are not supported. For example, the following code will not work:</p>
<pre class="cpp">

  <span class="type"><a href="qjsengine.html#QJSEngine">QJSEngine</a></span> engine;

  <span class="type">QObject</span> <span class="operator">*</span>myQObject <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QObject</span>();
  myQObject<span class="operator">-</span><span class="operator">&gt;</span>setProperty(<span class="string">&quot;dynamicProperty&quot;</span><span class="operator">,</span> <span class="number">3</span>);

  <span class="type"><a href="qjsvalue.html">QJSValue</a></span> myScriptQObject <span class="operator">=</span> engine<span class="operator">.</span>newQObject(myQObject);
  engine<span class="operator">.</span>globalObject()<span class="operator">.</span>setProperty(<span class="string">&quot;myObject&quot;</span><span class="operator">,</span> myScriptQObject);

  qDebug() <span class="operator">&lt;</span><span class="operator">&lt;</span> engine<span class="operator">.</span>evaluate(<span class="string">&quot;myObject.dynamicProperty&quot;</span>)<span class="operator">.</span>toInt();

</pre>
<a name="extensions"></a>
<h3 id="extensions">Extensions</h3>
<p><a href="qjsengine.html">QJSEngine</a> provides a compliant ECMAScript implementation. By default, familiar utilities like logging are not available, but they can can be installed via the <a href="qjsengine.html#installExtensions">installExtensions</a>() function.</p>
</div>
<p><b>See also </b><a href="qjsvalue.html">QJSValue</a>, <a href="qtjavascript.html">Making Applications Scriptable</a>, and <a href="qtqml-javascript-functionlist.html">List of JavaScript Objects and Functions</a>.</p>
<!-- @@@QJSEngine -->
<div class="types">
<h2>Member Type Documentation</h2>
<!-- $$$Extension$$$TranslationExtension$$$ConsoleExtension$$$GarbageCollectionExtension$$$AllExtensions -->
<h3 class="flags" id="Extension-enum"><a name="Extension-enum"></a>enum QJSEngine::<span class="name">Extension</span><br/>flags QJSEngine::<span class="name">Extensions</span></h3>
<p>This enum is used to specify extensions to be installed via <a href="qjsengine.html#installExtensions">installExtensions</a>().</p>
<div class="table"><table class="valuelist"><tr valign="top" class="odd"><th class="tblConst">Constant</th><th class="tblval">Value</th><th class="tbldscr">Description</th></tr>
<tr><td class="topAlign"><code>QJSEngine::TranslationExtension</code></td><td class="topAlign tblval"><code>0x1</code></td><td class="topAlign">Indicates that translation functions (<code>qsTr()</code>, for example) should be installed.</td></tr>
<tr><td class="topAlign"><code>QJSEngine::ConsoleExtension</code></td><td class="topAlign tblval"><code>0x2</code></td><td class="topAlign">Indicates that console functions (<code>console.log()</code>, for example) should be installed.</td></tr>
<tr><td class="topAlign"><code>QJSEngine::GarbageCollectionExtension</code></td><td class="topAlign tblval"><code>0x4</code></td><td class="topAlign">Indicates that garbage collection functions (<code>gc()</code>, for example) should be installed.</td></tr>
<tr><td class="topAlign"><code>QJSEngine::AllExtensions</code></td><td class="topAlign tblval"><code>0xffffffff</code></td><td class="topAlign">Indicates that all extension should be installed.</td></tr>
</table></div>
<p><b>TranslationExtension</b></p>
<p>The relation between script translation functions and C++ translation functions is described in the following table:</p>
<div class="table"><table class="generic">
 <thead><tr class="qt-style"><th >Script Function</th><th >Corresponding C++ Function</th></tr></thead>
<tr valign="top" class="odd"><td >qsTr()</td><td >QObject::tr()</td></tr>
<tr valign="top" class="even"><td >QT_TR_NOOP()</td><td >QT_TR_NOOP()</td></tr>
<tr valign="top" class="odd"><td >qsTranslate()</td><td >QCoreApplication::translate()</td></tr>
<tr valign="top" class="even"><td >QT_TRANSLATE_NOOP()</td><td >QT_TRANSLATE_NOOP()</td></tr>
<tr valign="top" class="odd"><td >qsTrId()</td><td >qtTrId()</td></tr>
<tr valign="top" class="even"><td >QT_TRID_NOOP()</td><td >QT_TRID_NOOP()</td></tr>
</table></div>
<p>This flag also adds an <code>arg()</code> function to the string prototype.</p>
<p>For more information, see the Internationalization with Qt documentation.</p>
<p><b>ConsoleExtension</b></p>
<p>The console object implements a subset of the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Console">Console API</a>, which provides familiar logging functions, such as <code>console.log()</code>.</p>
<p>The list of functions added is as follows:</p>
<ul>
<li><code>console.assert()</code></li>
<li><code>console.debug()</code></li>
<li><code>console.exception()</code></li>
<li><code>console.info()</code></li>
<li><code>console.log()</code> (equivalent to <code>console.debug()</code>)</li>
<li><code>console.error()</code></li>
<li><code>console.time()</code></li>
<li><code>console.timeEnd()</code></li>
<li><code>console.trace()</code></li>
<li><code>console.count()</code></li>
<li><code>console.warn()</code></li>
<li><code>print()</code> (equivalent to <code>console.debug()</code>)</li>
</ul>
<p>For more information, see the Console API documentation.</p>
<p><b>GarbageCollectionExtension</b></p>
<p>The <code>gc()</code> function is equivalent to calling <a href="qjsengine.html#collectGarbage">collectGarbage</a>().</p>
<p>The Extensions type is a typedef for <a href="">QFlags</a>&lt;Extension&gt;. It stores an OR combination of Extension values.</p>
<!-- @@@Extension -->
</div>
<div class="func">
<h2>Member Function Documentation</h2>
<!-- $$$QJSEngine[overload1]$$$QJSEngine -->
<h3 class="fn" id="QJSEngine"><a name="QJSEngine"></a>QJSEngine::<span class="name">QJSEngine</span>()</h3>
<p>Constructs a <a href="qjsengine.html">QJSEngine</a> object.</p>
<p>The <a href="qjsengine.html#globalObject">globalObject</a>() is initialized to have properties as described in <a href="http://www.ecma-international.org/publications/standards/Ecma-262.htm">ECMA-262</a>, Section 15.1&#x2e;</p>
<!-- @@@QJSEngine -->
<!-- $$$QJSEngine$$$QJSEngineQObject* -->
<h3 class="fn" id="QJSEngine-1"><a name="QJSEngine-1"></a>QJSEngine::<span class="name">QJSEngine</span>(<span class="type">QObject</span> *<i>parent</i>)</h3>
<p>Constructs a <a href="qjsengine.html">QJSEngine</a> object with the given <i>parent</i>.</p>
<p>The <a href="qjsengine.html#globalObject">globalObject</a>() is initialized to have properties as described in <a href="http://www.ecma-international.org/publications/standards/Ecma-262.htm">ECMA-262</a>, Section 15.1&#x2e;</p>
<!-- @@@QJSEngine -->
<!-- $$$~QJSEngine[overload1]$$$~QJSEngine -->
<h3 class="fn" id="dtor.QJSEngine"><a name="dtor.QJSEngine"></a><code>[override virtual] </code>QJSEngine::<span class="name">~QJSEngine</span>()</h3>
<p>Destroys this <a href="qjsengine.html">QJSEngine</a>.</p>
<p>Garbage is not collected from the persistent JS heap during <a href="qjsengine.html">QJSEngine</a> destruction. If you need all memory freed, call <a href="qjsengine.html#collectGarbage">collectGarbage</a> manually right before destroying the <a href="qjsengine.html">QJSEngine</a>.</p>
<!-- @@@~QJSEngine -->
<!-- $$$collectGarbage[overload1]$$$collectGarbage -->
<h3 class="fn" id="collectGarbage"><a name="collectGarbage"></a><span class="type">void</span> QJSEngine::<span class="name">collectGarbage</span>()</h3>
<p>Runs the garbage collector.</p>
<p>The garbage collector will attempt to reclaim memory by locating and disposing of objects that are no longer reachable in the script environment.</p>
<p>Normally you don't need to call this function; the garbage collector will automatically be invoked when the <a href="qjsengine.html">QJSEngine</a> decides that it's wise to do so (i.e&#x2e; when a certain number of new objects have been created). However, you can call this function to explicitly request that garbage collection should be performed as soon as possible.</p>
<!-- @@@collectGarbage -->
<!-- $$$evaluate[overload1]$$$evaluateconstQString&constQString&int -->
<h3 class="fn" id="evaluate"><a name="evaluate"></a><span class="type"><a href="qjsvalue.html">QJSValue</a></span> QJSEngine::<span class="name">evaluate</span>(const <span class="type">QString</span> &amp;<i>program</i>, const <span class="type">QString</span> &amp;<i>fileName</i> = QString(), <span class="type">int</span> <i>lineNumber</i> = 1)</h3>
<p>Evaluates <i>program</i>, using <i>lineNumber</i> as the base line number, and returns the result of the evaluation.</p>
<p>The script code will be evaluated in the context of the global object.</p>
<p>The evaluation of <i>program</i> can cause an <a href="qjsengine.html#script-exceptions">exception</a> in the engine; in this case the return value will be the exception that was thrown (typically an <code>Error</code> object; see <a href="qjsvalue.html#isError">QJSValue::isError</a>()).</p>
<p><i>lineNumber</i> is used to specify a starting line number for <i>program</i>; line number information reported by the engine that pertains to this evaluation will be based on this argument. For example, if <i>program</i> consists of two lines of code, and the statement on the second line causes a script exception, the exception line number would be <i>lineNumber</i> plus one. When no starting line number is specified, line numbers will be 1-based.</p>
<p><i>fileName</i> is used for error reporting. For example, in error objects the file name is accessible through the &quot;fileName&quot; property if it is provided with this function.</p>
<p><b>Note: </b>If an exception was thrown and the exception value is not an Error instance (i.e&#x2e;, <a href="qjsvalue.html#isError">QJSValue::isError</a>() returns <code>false</code>), the exception value will still be returned, but there is currently no API for detecting that an exception did occur in this case.</p><!-- @@@evaluate -->
<!-- $$$fromScriptValue[overload1]$$$fromScriptValueconstQJSValue& -->
<h3 class="fn" id="fromScriptValue"><a name="fromScriptValue"></a><span class="type">T</span> QJSEngine::<span class="name">fromScriptValue</span>(const <span class="type"><a href="qjsvalue.html">QJSValue</a></span> &amp;<i>value</i>)</h3>
<p>Returns the given <i>value</i> converted to the template type <code>T</code>.</p>
<p><b>See also </b><a href="qjsengine.html#toScriptValue">toScriptValue</a>().</p>
<!-- @@@fromScriptValue -->
<!-- $$$globalObject[overload1]$$$globalObject -->
<h3 class="fn" id="globalObject"><a name="globalObject"></a><span class="type"><a href="qjsvalue.html">QJSValue</a></span> QJSEngine::<span class="name">globalObject</span>() const</h3>
<p>Returns this engine's Global Object.</p>
<p>By default, the Global Object contains the built-in objects that are part of <a href="http://www.ecma-international.org/publications/standards/Ecma-262.htm">ECMA-262</a>, such as Math, Date and String. Additionally, you can set properties of the Global Object to make your own extensions available to all script code. Non-local variables in script code will be created as properties of the Global Object, as well as local variables in global code.</p>
<!-- @@@globalObject -->
<!-- $$$importModule[overload1]$$$importModuleconstQString& -->
<h3 class="fn" id="importModule"><a name="importModule"></a><span class="type"><a href="qjsvalue.html">QJSValue</a></span> QJSEngine::<span class="name">importModule</span>(const <span class="type">QString</span> &amp;<i>fileName</i>)</h3>
<p>Imports the module located at <i>fileName</i> and returns a module namespace object that contains all exported variables, constants and functions as properties.</p>
<p>If this is the first time the module is imported in the engine, the file is loaded from the specified location in either the local file system or the Qt resource system and evaluated as an ECMAScript module. The file is expected to be encoded in UTF-8 text.</p>
<p>Subsequent imports of the same module will return the previously imported instance. Modules are singletons and remain around until the engine is destroyed.</p>
<p>The specified <i>fileName</i> will internally be normalized using QFileInfo::canonicalFilePath(). That means that multiple imports of the same file on disk using different relative paths will load the file only once.</p>
<p><b>Note: </b>If an exception is thrown during the loading of the module, the return value will be the exception (typically an <code>Error</code> object; see <a href="qjsvalue.html#isError">QJSValue::isError</a>()).</p><p>This function was introduced in  Qt 5.12.</p>
<!-- @@@importModule -->
<!-- $$$installExtensions[overload1]$$$installExtensionsQJSEngine::ExtensionsconstQJSValue& -->
<h3 class="fn" id="installExtensions"><a name="installExtensions"></a><span class="type">void</span> QJSEngine::<span class="name">installExtensions</span>(<span class="type"><a href="qjsengine.html#Extension-enum">QJSEngine::Extensions</a></span> <i>extensions</i>, const <span class="type"><a href="qjsvalue.html">QJSValue</a></span> &amp;<i>object</i> = QJSValue())</h3>
<p>Installs JavaScript <i>extensions</i> to add functionality that is not available in a standard ECMAScript implementation.</p>
<p>The extensions are installed on the given <i>object</i>, or on the <a href="qjsengine.html#globalObject">Global Object</a> if no object is specified.</p>
<p>Several extensions can be installed at once by <code>OR</code>-ing the enum values:</p>
<pre class="cpp">

  installExtensions(<span class="type"><a href="qjsengine.html#QJSEngine">QJSEngine</a></span><span class="operator">::</span>TranslationExtension <span class="operator">|</span> <span class="type"><a href="qjsengine.html#QJSEngine">QJSEngine</a></span><span class="operator">::</span>ConsoleExtension);

</pre>
<p>This function was introduced in  Qt 5.6.</p>
<p><b>See also </b><a href="qjsengine.html#Extension-enum">Extension</a>.</p>
<!-- @@@installExtensions -->
<!-- $$$newArray[overload1]$$$newArrayuint -->
<h3 class="fn" id="newArray"><a name="newArray"></a><span class="type"><a href="qjsvalue.html">QJSValue</a></span> QJSEngine::<span class="name">newArray</span>(<span class="type">uint</span> <i>length</i> = 0)</h3>
<p>Creates a JavaScript object of class Array with the given <i>length</i>.</p>
<p><b>See also </b><a href="qjsengine.html#newObject">newObject</a>().</p>
<!-- @@@newArray -->
<!-- $$$newErrorObject[overload1]$$$newErrorObjectQJSValue::ErrorTypeconstQString& -->
<h3 class="fn" id="newErrorObject"><a name="newErrorObject"></a><span class="type"><a href="qjsvalue.html">QJSValue</a></span> QJSEngine::<span class="name">newErrorObject</span>(<span class="type"><a href="qjsvalue.html#ErrorType-enum">QJSValue::ErrorType</a></span> <i>errorType</i>, const <span class="type">QString</span> &amp;<i>message</i> = QString())</h3>
<p>Creates a JavaScript object of class Error.</p>
<p>The prototype of the created object will be <i>errorType</i>.</p>
<p>This function was introduced in  Qt 5.12.</p>
<p><b>See also </b><a href="qjsengine.html#newObject">newObject</a>(), <a href="qjsengine.html#throwError">throwError</a>(), and <a href="qjsvalue.html#isError">QJSValue::isError</a>().</p>
<!-- @@@newErrorObject -->
<!-- $$$newObject[overload1]$$$newObject -->
<h3 class="fn" id="newObject"><a name="newObject"></a><span class="type"><a href="qjsvalue.html">QJSValue</a></span> QJSEngine::<span class="name">newObject</span>()</h3>
<p>Creates a JavaScript object of class Object.</p>
<p>The prototype of the created object will be the Object prototype object.</p>
<p><b>See also </b><a href="qjsengine.html#newArray">newArray</a>() and <a href="qjsvalue.html#setProperty">QJSValue::setProperty</a>().</p>
<!-- @@@newObject -->
<!-- $$$newQMetaObject[overload1]$$$newQMetaObjectconstQMetaObject* -->
<h3 class="fn" id="newQMetaObject"><a name="newQMetaObject"></a><span class="type"><a href="qjsvalue.html">QJSValue</a></span> QJSEngine::<span class="name">newQMetaObject</span>(const <span class="type">QMetaObject</span> *<i>metaObject</i>)</h3>
<p>Creates a JavaScript object that wraps the given QMetaObject The <i>metaObject</i> must outlive the script engine. It is recommended to only use this method with static metaobjects.</p>
<p>When called as a constructor, a new instance of the class will be created. Only constructors exposed by Q_INVOKABLE will be visible from the script engine.</p>
<p>This function was introduced in  Qt 5.8.</p>
<p><b>See also </b><a href="qjsengine.html#newQObject">newQObject</a>() and <a href="qjsengine.html#qobject-integration">QObject Integration</a>.</p>
<!-- @@@newQMetaObject -->
<!-- $$$newQMetaObject$$$newQMetaObject -->
<h3 class="fn" id="newQMetaObject-1"><a name="newQMetaObject-1"></a><span class="type"><a href="qjsvalue.html">QJSValue</a></span> QJSEngine::<span class="name">newQMetaObject</span>()</h3>
<p>Creates a JavaScript object that wraps the static QMetaObject associated with class <code>T</code>.</p>
<p>This function was introduced in  Qt 5.8.</p>
<p><b>See also </b><a href="qjsengine.html#newQObject">newQObject</a>() and <a href="qjsengine.html#qobject-integration">QObject Integration</a>.</p>
<!-- @@@newQMetaObject -->
<!-- $$$newQObject[overload1]$$$newQObjectQObject* -->
<h3 class="fn" id="newQObject"><a name="newQObject"></a><span class="type"><a href="qjsvalue.html">QJSValue</a></span> QJSEngine::<span class="name">newQObject</span>(<span class="type">QObject</span> *<i>object</i>)</h3>
<p>Creates a JavaScript object that wraps the given QObject <i>object</i>, using JavaScriptOwnership.</p>
<p>Signals and slots, properties and children of <i>object</i> are available as properties of the created <a href="qjsvalue.html">QJSValue</a>.</p>
<p>If <i>object</i> is a null pointer, this function returns a null value.</p>
<p>If a default prototype has been registered for the <i>object</i>'s class (or its superclass, recursively), the prototype of the new script object will be set to be that default prototype.</p>
<p>If the given <i>object</i> is deleted outside of the engine's control, any attempt to access the deleted QObject's members through the JavaScript wrapper object (either by script code or C++) will result in a <a href="qjsengine.html#script-exceptions">script exception</a>.</p>
<p><b>See also </b><a href="qjsvalue.html#toQObject">QJSValue::toQObject</a>().</p>
<!-- @@@newQObject -->
<!-- $$$throwError[overload1]$$$throwErrorconstQString& -->
<h3 class="fn" id="throwError"><a name="throwError"></a><span class="type">void</span> QJSEngine::<span class="name">throwError</span>(const <span class="type">QString</span> &amp;<i>message</i>)</h3>
<p>Throws a run-time error (exception) with the given <i>message</i>.</p>
<p>This method is the C++ counterpart of a <code>throw()</code> expression in JavaScript. It enables C++ code to report run-time errors to <a href="qjsengine.html">QJSEngine</a>. Therefore it should only be called from C++ code that was invoked by a JavaScript function through <a href="qjsengine.html">QJSEngine</a>.</p>
<p>When returning from C++, the engine will interrupt the normal flow of execution and call the the next pre-registered exception handler with an error object that contains the given <i>message</i>. The error object will point to the location of the top-most context on the JavaScript caller stack; specifically, it will have properties <code>lineNumber</code>, <code>fileName</code> and <code>stack</code>. These properties are described in <a href="qjsengine.html#script-exceptions">Script Exceptions</a>.</p>
<p>In the following example a C++ method in <i>FileAccess.cpp</i> throws an error in <i>qmlFile.qml</i> at the position where <code>readFileAsText()</code> is called:</p>
<pre class="cpp">

  <span class="comment">// qmlFile.qml</span>
  function someFunction() {
    <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
    var text <span class="operator">=</span> FileAccess<span class="operator">.</span>readFileAsText(<span class="string">&quot;/path/to/file.txt&quot;</span>);
  }

</pre>
<pre class="cpp">

  <span class="comment">// FileAccess.cpp</span>
  <span class="comment">// Assuming that FileAccess is a QObject-derived class that has been</span>
  <span class="comment">// registered as a singleton type and provides an invokable method</span>
  <span class="comment">// readFileAsText()</span>

  <span class="type"><a href="qjsvalue.html">QJSValue</a></span> FileAccess<span class="operator">::</span>readFileAsText(<span class="keyword">const</span> <span class="type">QString</span> <span class="operator">&amp;</span> filePath) {
    <span class="type">QFile</span> file(filePath);

    <span class="keyword">if</span> (<span class="operator">!</span>file<span class="operator">.</span>open(<span class="type">QIODevice</span><span class="operator">::</span>ReadOnly)) {
      jsEngine<span class="operator">-</span><span class="operator">&gt;</span>throwError(file<span class="operator">.</span>errorString());
      <span class="keyword">return</span> <span class="type">QString</span>();
    }

    <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
    <span class="keyword">return</span> content;
  }

</pre>
<p>It is also possible to catch the thrown error in JavaScript:</p>
<pre class="cpp">

  <span class="comment">// qmlFile.qml</span>
  function someFunction() {
    <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
    var text;
    <span class="keyword">try</span> {
      text <span class="operator">=</span> FileAccess<span class="operator">.</span>readFileAsText(<span class="string">&quot;/path/to/file.txt&quot;</span>);
    } <span class="keyword">catch</span> (error) {
      console<span class="operator">.</span>warn(<span class="string">&quot;In &quot;</span> <span class="operator">+</span> error<span class="operator">.</span>fileName <span class="operator">+</span> <span class="string">&quot;:&quot;</span> <span class="operator">+</span> <span class="string">&quot;error.lineNumber&quot;</span> <span class="operator">+</span>
                   <span class="string">&quot;: &quot;</span> <span class="operator">+</span> error<span class="operator">.</span>message);
    }
  }

</pre>
<p>If you need a more specific run-time error to describe an exception, you can use the <a href="qjsengine.html#throwError-1">throwError(QJSValue::ErrorType errorType, const QString &amp;message)</a> overload.</p>
<p>This function was introduced in  Qt 5.12.</p>
<p><b>See also </b><a href="qjsengine.html#script-exceptions">Script Exceptions</a>.</p>
<!-- @@@throwError -->
<!-- $$$throwError$$$throwErrorQJSValue::ErrorTypeconstQString& -->
<h3 class="fn" id="throwError-1"><a name="throwError-1"></a><span class="type">void</span> QJSEngine::<span class="name">throwError</span>(<span class="type"><a href="qjsvalue.html#ErrorType-enum">QJSValue::ErrorType</a></span> <i>errorType</i>, const <span class="type">QString</span> &amp;<i>message</i> = QString())</h3>
<p>This function overloads <a href="qjsengine.html#throwError">throwError</a>().</p>
<p>Throws a run-time error (exception) with the given <i>errorType</i> and <i>message</i>.</p>
<pre class="cpp">

  <span class="comment">// Assuming that DataEntry is a QObject-derived class that has been</span>
  <span class="comment">// registered as a singleton type and provides an invokable method</span>
  <span class="comment">// setAge().</span>

  <span class="type">void</span> DataEntry<span class="operator">::</span>setAge(<span class="type">int</span> age) {
    <span class="keyword">if</span> (age <span class="operator">&lt;</span> <span class="number">0</span> <span class="operator">|</span><span class="operator">|</span> age <span class="operator">&gt;</span> <span class="number">200</span>) {
      jsEngine<span class="operator">-</span><span class="operator">&gt;</span>throwError(<span class="type"><a href="qjsvalue.html">QJSValue</a></span><span class="operator">::</span>RangeError<span class="operator">,</span>
                           <span class="string">&quot;Age must be between 0 and 200&quot;</span>);
    }
    <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
  }

</pre>
<p>This function was introduced in  Qt 5.12.</p>
<p><b>See also </b><a href="qjsengine.html#script-exceptions">Script Exceptions</a> and <a href="qjsengine.html#newErrorObject">newErrorObject</a>().</p>
<!-- @@@throwError -->
<!-- $$$toScriptValue[overload1]$$$toScriptValueconstT& -->
<h3 class="fn" id="toScriptValue"><a name="toScriptValue"></a><span class="type"><a href="qjsvalue.html">QJSValue</a></span> QJSEngine::<span class="name">toScriptValue</span>(const <span class="type">T</span> &amp;<i>value</i>)</h3>
<p>Creates a <a href="qjsvalue.html">QJSValue</a> with the given <i>value</i>.</p>
<p><b>See also </b><a href="qjsengine.html#fromScriptValue">fromScriptValue</a>().</p>
<!-- @@@toScriptValue -->
</div>
<div class="relnonmem">
<h2>Related Non-Members</h2>
<!-- $$$qjsEngine[overload1]$$$qjsEngineconstQObject* -->
<h3 class="fn" id="qjsEnginex"><a name="qjsEnginex"></a><span class="type"><a href="qjsengine.html#QJSEngine">QJSEngine</a></span> *<span class="name">qjsEngine</span>(const <span class="type">QObject</span> *<i>object</i>)</h3>
<p>Returns the <a href="qjsengine.html">QJSEngine</a> associated with <i>object</i>, if any.</p>
<p>This function is useful if you have exposed a QObject to the JavaScript environment and later in your program would like to regain access. It does not require you to keep the wrapper around that was returned from <a href="qjsengine.html#newQObject">QJSEngine::newQObject</a>().</p>
<p>This function was introduced in  Qt 5.5.</p>
<!-- @@@qjsEngine -->
</div>
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</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>