Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
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 \section1 Scripting Classes
15
16 The following classes add scripting capabilities to Qt applications.
17
18 \annotatedlist qtjavascript
19
20 \section1 Basic Usage
21
22 To evaluate script code, you create a QJSEngine and call its
23 evaluate() function, passing the script code (text) to evaluate
24 as argument.
25
26 \snippet qtjavascript/evaluation/main.cpp 0
27
28 The return value will be the result of the evaluation (represented
29 as a QJSValue object); this can be converted to standard C++
30 and Qt types.
31
32 Custom properties can be made available to scripts by registering
33 them with the script engine. This is most easily done by setting
34 properties of the script engine's \e{Global Object}:
35
36 \snippet qtjavascript/registeringvalues/main.cpp 0
37
38 This places the properties in the script environment, thus making them
39 available to script code.
40
41 \section1 Making a QObject Available to the Script Engine
42
43 Any QObject-based instance can be made available for use with scripts.
44
45 When a QObject is passed to the QJSEngine::newQObject() function,
46 a Qt Script wrapper object is created that can be used to make the
47 QObject's signals, slots, properties, and child objects available
48 to scripts.
49
50 Here's an example of making an instance of a QObject subclass
51 available to script code under the name \c{"myObject"}:
52
53 \snippet qtjavascript/registeringobjects/main.cpp 0
54
55 This will create a global variable called \c{myObject} in the
56 script environment. The variable serves as a proxy to the
57 underlying C++ object. Note that the name of the script variable
58 can be anything; i.e., it is not dependent upon QObject::objectName().
59
60 \section1 Implications for Application Security
61
62 The security model of application scripting with JavaScript follows
63 the same model as for C++ code: the user installs scripts to run
64 that they trust in the same way as they install Qt applications.
65
66 In order to preserve the trust of users, application developers should
67 not evaluate arbitrary JavaScript code. The JavaScript engine's sandbox is
68 only a semantic barrier. The script is evaluated in the same process and
69 with the same privileges as the rest of the application and shares the
70 same memory. As a consequence, C++ objects exposed to scripts are
71 accessible without additional security guards.
72 */