1 /****************************************************************************
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
6 ** This file is part of the documentation of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:FDL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
17 ** GNU Free Documentation License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Free
19 ** Documentation License version 1.3 as published by the Free Software
20 ** Foundation and appearing in the file included in the packaging of
21 ** this file. Please review the following information to ensure
22 ** the GNU Free Documentation License version 1.3 requirements
23 ** will be met: http://www.gnu.org/copyleft/fdl.html.
26 ****************************************************************************/
29 \example script/defaultprototypes
30 \title Default Prototypes Example
32 \brief The Default Prototypes QtScript example shows how to use default prototypes
33 to make a non-QObject-based type scriptable.
35 \image defaultprototypes-example.png
37 With QScriptEngine::setDefaultPrototype() you can specify
38 a QtScript object that defines a scripting interface for
39 a C++ type; Qt Script operations on values of such types
40 will then be delegated to your prototype object. In this
41 example, a simple scripting interface for QListWidgetItem is
42 defined, so that the text of items can easily be accessed from
45 To define a scripting API for QListWidgetItem in terms of
46 Qt properties and slots, we subclass QObject and QScriptable.
48 \snippet examples/script/defaultprototypes/prototypes.h 0
50 A single property, \c{text}, is defined, along with a slot,
53 \snippet examples/script/defaultprototypes/prototypes.cpp 0
55 The implementation of the property accessors use
56 the qscriptvalue_cast() function to cast the script object
57 to a QListWidgetItem pointer. The normal C++ QListWidgetItem
58 API is then used to implement the desired functionality.
60 Although not shown here, it is possible to throw a script
61 exception from a prototype function; for example, you could throw
62 a TypeError exception if the qscriptvalue_cast() fails.
64 QListWidgetItems are usually added to a QListWidget. While
65 QListWidget is a QObject-based class, not all the functionality
66 needed for this example are present. We can solve this by creating
67 a default prototype for the QListWidget class as well. The
68 prototype will augment the functionality already provided by the
69 Qt Script QObject integration; i.e. if a property or slot is not
70 found in the QListWidget object itself, the prototype will be used
73 \snippet examples/script/defaultprototypes/prototypes.h 1
75 The additional slots will make it possible to add items to
76 a QListWidget from script code, and to set the background
77 color of the widget from a string.
79 \snippet examples/script/defaultprototypes/prototypes.cpp 1
81 Again, we use qscriptvalue_cast() to cast the script object
82 to the relevant C++ type, in this case a QListWidget pointer.
83 The addItem() and addItems() functions simply forward their
84 arguments to the corresponding functions in the QListWidget
85 class. setBackgroundColor() gets the widget's palette, creates
86 a QColor from the given string argument and changes the palette
89 \snippet examples/script/defaultprototypes/main.cpp 0
91 The relevant C++ types must be made known to Qt's meta type
94 \snippet examples/script/defaultprototypes/main.cpp 1
96 For each type that we want to associate a prototype object with,
97 we create an instance of the prototype class, pass it to
98 QScriptEngine::newQObject(), and then create the link between
99 the C++ type and the resulting script object by calling
100 QScriptEngine::setDefaultPrototype().
102 \snippet examples/script/defaultprototypes/main.cpp 2
104 In this example, a single QListWidget object is added as
105 a global script variable, called \c{listWidget}. Script code
106 can add items to this widget by calling addItem() or addItems().
108 \snippet examples/script/defaultprototypes/code.js 0
110 Script code can connect to signals of the QListWidget object;
111 signal handlers can use the interface defined in
112 the QListWidgetItem prototype to manipulate item arguments.
114 \snippet examples/script/defaultprototypes/code.js 1
116 Not shown in this example is how to make QListWidgetItem
117 constructible from Qt Script code, i.e. to be able to
118 write "new QListWidgetItem()" in a script. In order to do
119 this, you have to define your own script constructor for
120 the type. The constructor would just be a factory function
121 that constructs a new C++ QListWidgetItem and returns it
122 back to the script. See QScriptEngine::newFunction() for more