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