Update copyright headers
[qt:qt.git] / doc / src / examples / defaultprototypes.qdoc
1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the documentation of the Qt Toolkit.
7 **
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.
16 **
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.
24 ** $QT_END_LICENSE$
25 **
26 ****************************************************************************/
27
28 /*!
29     \example script/defaultprototypes
30     \title Default Prototypes Example
31
32     \brief The Default Prototypes QtScript example shows how to use default prototypes
33     to make a non-QObject-based type scriptable.
34
35     \image defaultprototypes-example.png
36
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
43     script code.
44
45     To define a scripting API for QListWidgetItem in terms of
46     Qt properties and slots, we subclass QObject and QScriptable.
47
48     \snippet examples/script/defaultprototypes/prototypes.h 0
49
50     A single property, \c{text}, is defined, along with a slot,
51     \c{toString}.
52
53     \snippet examples/script/defaultprototypes/prototypes.cpp 0
54
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.
59
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.
63
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
71     as a fallback.
72
73     \snippet examples/script/defaultprototypes/prototypes.h 1
74
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.
78
79     \snippet examples/script/defaultprototypes/prototypes.cpp 1
80
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
87     accordingly.
88
89     \snippet examples/script/defaultprototypes/main.cpp 0
90
91     The relevant C++ types must be made known to Qt's meta type
92     system.
93
94     \snippet examples/script/defaultprototypes/main.cpp 1
95
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().
101
102     \snippet examples/script/defaultprototypes/main.cpp 2
103
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().
107
108     \snippet examples/script/defaultprototypes/code.js 0
109
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.
113
114     \snippet examples/script/defaultprototypes/code.js 1
115
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
123     information.
124 */