1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the documentation of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:FDL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Free Documentation License Usage
** Alternatively, this file may be used under the terms of the GNU Free
** Documentation License version 1.3 as published by the Free Software
** Foundation and appearing in the file included in the packaging of
** this file. Please review the following information to ensure
** the GNU Free Documentation License version 1.3 requirements
** will be met: https://www.gnu.org/licenses/fdl-1.3.html.
** $QT_END_LICENSE$
**
****************************************************************************/
/*!
\group qtjavascript
\title Scripting Classes and Overviews
\brief Classes for embedding JavaScript in Qt/C++ applications.
*/
/*!
\page qtjavascript.html
\title Making Applications Scriptable
\ingroup frameworks-technologies
\brief incorporating JavaScript in Qt applications.
Qt provides support for application scripting with JavaScript.
The following guides and references cover aspects of programming with
JavaScript and Qt.
\tableofcontents
\section1 Scripting Classes
The following classes add scripting capabilities to Qt applications.
\annotatedlist qtjavascript
\section1 Basic Usage
To evaluate script code, you create a QJSEngine and call its
evaluate() function, passing the script code (text) to evaluate
as argument.
\snippet qtjavascript/evaluation/main.cpp 0
The return value will be the result of the evaluation (represented
as a QJSValue object); this can be converted to standard C++
and Qt types.
Custom properties can be made available to scripts by registering
them with the script engine. This is most easily done by setting
properties of the script engine's \e{Global Object}:
\snippet qtjavascript/registeringvalues/main.cpp 0
This places the properties in the script environment, thus making them
available to script code.
\section1 Making a QObject Available to the Script Engine
Any QObject-based instance can be made available for use with scripts.
When a QObject is passed to the QJSEngine::newQObject() function,
a Qt Script wrapper object is created that can be used to make the
QObject's signals, slots, properties, and child objects available
to scripts.
Here's an example of making an instance of a QObject subclass
available to script code under the name \c{"myObject"}:
\snippet qtjavascript/registeringobjects/main.cpp 0
This will create a global variable called \c{myObject} in the
script environment. The variable serves as a proxy to the
underlying C++ object. Note that the name of the script variable
can be anything; i.e., it is not dependent upon QObject::objectName().
\section1 Implications for Application Security
The security model of application scripting with JavaScript follows
the same model as for C++ code: the user installs scripts to run
that they trust in the same way as they install Qt applications.
In order to preserve the trust of users, application developers should
not evaluate arbitrary JavaScript code. The JavaScript engine's sandbox is
only a semantic barrier. The script is evaluated in the same process and
with the same privileges as the rest of the application and shares the
same memory. As a consequence, C++ objects exposed to scripts are
accessible without additional security guards.
*/
|