1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information (qt-info@nokia.com)
|
---|
5 | **
|
---|
6 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at qt-sales@nokia.com.
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | /*!
|
---|
43 | \example script/calculator
|
---|
44 | \title QtScript Calculator Example
|
---|
45 | \ingroup scripting
|
---|
46 |
|
---|
47 | In this simple QtScript example, we show how to implement the
|
---|
48 | functionality of a calculator widget.
|
---|
49 |
|
---|
50 | \image qtscript-calculator-example.png
|
---|
51 |
|
---|
52 | The program logic in this example is a fairly straight port of the logic in the C++ \l{Calculator Example}.
|
---|
53 | The graphical user interface is defined in a UI file.
|
---|
54 |
|
---|
55 | The C++ part of the example consists of four steps:
|
---|
56 | \list
|
---|
57 | \o Evaluate the script code that defines the \c{Calculator} class.
|
---|
58 |
|
---|
59 | \snippet examples/script/calculator/main.cpp 0a
|
---|
60 | \snippet examples/script/calculator/main.cpp 0b
|
---|
61 |
|
---|
62 | \o Create a widget from the UI file using QUiLoader.
|
---|
63 |
|
---|
64 | \snippet examples/script/calculator/main.cpp 1
|
---|
65 |
|
---|
66 | \o Call the Calculator constructor function to create a new \c{Calculator} script object, passing the widget as argument.
|
---|
67 |
|
---|
68 | \snippet examples/script/calculator/main.cpp 2
|
---|
69 |
|
---|
70 | \o Show the widget and start the application event loop.
|
---|
71 |
|
---|
72 | \snippet examples/script/calculator/main.cpp 3
|
---|
73 |
|
---|
74 | \endlist
|
---|
75 |
|
---|
76 | On the script side, the \c{Calculator} constructor function
|
---|
77 | initializes the instance variables of the new \c{Calculator}
|
---|
78 | object, and connects the clicked() signal of the form's buttons
|
---|
79 | to corresponding functions defined in the \c{Calculator} prototype
|
---|
80 | object; the effect is that when a button is clicked, the proper
|
---|
81 | script function will be invoked to carry out the operation.
|
---|
82 |
|
---|
83 | \snippet examples/script/calculator/calculator.js 0
|
---|
84 |
|
---|
85 | A \c{Calculator} object is just a plain script object; it is not
|
---|
86 | a widget. Instead, it stores a reference to the calculator form
|
---|
87 | (the widget) in an instance variable, \c{ui}. The calculator
|
---|
88 | script functions can access components of the form by referring
|
---|
89 | to the proper children of the \c{ui} member.
|
---|
90 |
|
---|
91 | \snippet examples/script/calculator/calculator.js 1
|
---|
92 |
|
---|
93 | The digitClicked() function uses the special local variable
|
---|
94 | __qt_sender__ to access the object that triggered the signal;
|
---|
95 | this gives us a simple way to retrieve the value of the digit
|
---|
96 | that was clicked.
|
---|
97 |
|
---|
98 | \snippet examples/script/calculator/calculator.js 2
|
---|
99 |
|
---|
100 | The changeSign() function shows how we retrieve the text property
|
---|
101 | of the calculator's display, change it appropriately, and write
|
---|
102 | back the new value.
|
---|
103 |
|
---|
104 |
|
---|
105 | */
|
---|