Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qtjavascript.qdoc
Go to the documentation of this file.
1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5 \page qtjavascript.html
6 \title Making Applications Scriptable
7 \ingroup frameworks-technologies
8 \brief incorporating JavaScript in Qt applications.
9
10 Qt provides support for application scripting with JavaScript.
11 The following guides and references cover aspects of programming with
12 JavaScript and Qt.
13
14 \tableofcontents
15
16 \section1 Scripting Classes
17
18 The following classes add scripting capabilities to Qt applications.
19
20 \annotatedlist qtjavascript
21
22 \section1 Basic Usage
23
24 To evaluate script code, you create a QJSEngine and call its
25 evaluate() function, passing the script code (text) to evaluate
26 as argument.
27
28 \snippet qtjavascript/evaluation/main.cpp 0
29
30 The return value will be the result of the evaluation (represented
31 as a QJSValue object); this can be converted to standard C++
32 and Qt types.
33
34 Custom properties can be made available to scripts by registering
35 them with the script engine. This is most easily done by setting
36 properties of the script engine's \e{Global Object}:
37
38 \snippet qtjavascript/registeringvalues/main.cpp 0
39
40 This places the properties in the script environment, thus making them
41 available to script code.
42
43 \section1 Making a QObject Available to the Script Engine
44
45 Any QObject-based instance can be made available for use with scripts.
46
47 When a QObject is passed to the QJSEngine::newQObject() function,
48 a Qt Script wrapper object is created that can be used to make the
49 QObject's signals, slots, properties, and child objects available
50 to scripts.
51
52 Here's an example of making an instance of a QObject subclass
53 available to script code under the name \c{"myObject"}:
54
55 \snippet qtjavascript/registeringobjects/main.cpp 0
56
57 This will create a global variable called \c{myObject} in the
58 script environment. The variable serves as a proxy to the
59 underlying C++ object. Note that the name of the script variable
60 can be anything; i.e., it is not dependent upon QObject::objectName().
61
62 \section1 Implications for Application Security
63
64 The security model of application scripting with JavaScript follows
65 the same model as for C++ code: the user installs scripts to run
66 that they trust in the same way as they install Qt applications.
67
68 In order to preserve the trust of users, application developers should
69 not evaluate arbitrary JavaScript code. The JavaScript engine's sandbox is
70 only a semantic barrier. The script is evaluated in the same process and
71 with the same privileges as the rest of the application and shares the
72 same memory. As a consequence, C++ objects exposed to scripts are
73 accessible without additional security guards.
74 */