1
/****************************************************************************
3
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/
6
** This file is part of the documentation of the Qt Toolkit.
8
** $QT_BEGIN_LICENSE:FDL$
9
** GNU Free Documentation License
10
** Alternatively, this file may be used under the terms of the GNU Free
11
** Documentation License version 1.3 as published by the Free Software
12
** Foundation and appearing in the file included in the packaging of
16
** Alternatively, this file may be used in accordance with the terms
17
** and conditions contained in a signed written agreement between you
26
****************************************************************************/
30
\title Scripting Classes and Overviews
32
\brief Classes for embedding JavaScript in Qt/C++ applications.
36
\page qtjavascript.html
37
\title Making Applications Scriptable
38
\ingroup frameworks-technologies
39
\brief incorporating JavaScript in Qt applications.
41
Qt provides support for application scripting with JavaScript.
42
The following guides and references cover aspects of programming with
47
\section1 Scripting Classes
49
The following classes add scripting capabilities to Qt applications.
51
\annotatedlist qtjavascript
55
To evaluate script code, you create a QJSEngine and call its
56
evaluate() function, passing the script code (text) to evaluate
59
\snippet doc/src/snippets/qtjavascript/evaluation/main.cpp 0
61
The return value will be the result of the evaluation (represented
62
as a QJSValue object); this can be converted to standard C++
65
Custom properties can be made available to scripts by registering
66
them with the script engine. This is most easily done by setting
67
properties of the script engine's \e{Global Object}:
69
\snippet doc/src/snippets/qtjavascript/registeringvalues/main.cpp 0
71
This places the properties in the script environment, thus making them
72
available to script code.
74
\section1 Making a QObject Available to the Script Engine
76
Any QObject-based instance can be made available for use with scripts.
78
When a QObject is passed to the QJSEngine::newQObject() function,
79
a Qt Script wrapper object is created that can be used to make the
80
QObject's signals, slots, properties, and child objects available
83
Here's an example of making an instance of a QObject subclass
84
available to script code under the name \c{"myObject"}:
86
\snippet doc/src/snippets/qtjavascript/registeringobjects/main.cpp 0
88
This will create a global variable called \c{myObject} in the
89
script environment. The variable serves as a proxy to the
90
underlying C++ object. Note that the name of the script variable
91
can be anything; i.e., it is not dependent upon QObject::objectName().