source: trunk/doc/src/examples/helloscript.qdoc@ 1168

Last change on this file since 1168 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 5.0 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the documentation of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:FDL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in a
14** written agreement between you and Nokia.
15**
16** GNU Free Documentation License
17** Alternatively, this file may be used under the terms of the GNU Free
18** Documentation License version 1.3 as published by the Free Software
19** Foundation and appearing in the file included in the packaging of this
20** file.
21**
22** If you have questions regarding the use of this file, please contact
23** Nokia at [email protected].
24** $QT_END_LICENSE$
25**
26****************************************************************************/
27
28/*!
29 \example script/helloscript
30 \title Hello Script Example
31
32 The Hello Script example shows the basic use of Qt Script: How to embed
33 a script engine into the application, how to evaluate a script, and how
34 to process the result of the evaluation. The example also shows how to
35 apply internationalization to scripts.
36
37 \snippet examples/script/helloscript/main.cpp 0
38
39 The application will load the script file to evaluate from a resource, so
40 we first make sure that the resource is initialized.
41
42 \snippet examples/script/helloscript/main.cpp 1
43
44 We attempt to load a translation, and install translation functions in the
45 script engine. How to produce a translation is explained later.
46
47 \snippet examples/script/helloscript/main.cpp 2
48
49 A push button is created and exported to the script environment as a
50 global variable, \c button. Scripts will be able to access properties,
51 signals and slots of the button as properties of the \c button script
52 object; the script object acts as a proxy to the C++ button object.
53
54 \snippet examples/script/helloscript/main.cpp 3
55
56 The contents of the script file are read.
57
58 \snippet examples/script/helloscript/helloscript.js 0
59
60 The script sets the \c text (note that the qTr() function is used to allow
61 for translation) and \c styleSheet properties of the button, and calls the
62 button's \c show() slot.
63
64 \snippet examples/script/helloscript/main.cpp 4
65
66 The script is evaluated. Note that the file name is passed as the
67 (optional) second parameter; this makes it possible for the script engine
68 to produce a meaningful backtrace if something goes wrong, and makes the
69 qTr() function be able to resolve the translations that are associated
70 with this script.
71
72 \snippet examples/script/helloscript/main.cpp 5
73
74 If the result is an Error object (e.g. the script contained a syntax
75 error, or tried to call a function that doesn't exist), we obtain
76 the line number and string representation of the error and display
77 it in a message box.
78
79 \snippet examples/script/helloscript/main.cpp 6
80
81 If the evaluation went well, the application event loop is entered.
82
83 \section1 Translating the Application
84
85 The Qt Script internalization support builds on what Qt already provides
86 for C++; see the \l{Hello tr() Example} for an introduction.
87
88 Since we haven't made the translation file \c helloscript_la.qm, the
89 source text is shown when we run the application ("Hello world!").
90
91 To generate the translation file, run \c lupdate as follows:
92
93 \code
94 lupdate helloscript.js -ts helloscript_la.ts
95 \endcode
96
97 You should now have a file \c helloscript_la.ts in the current
98 directory. Run \c linguist to edit the translation:
99
100 \code
101 linguist helloscript_la.ts
102 \endcode
103
104 You should now see the text "helloscript.js" in the top left pane.
105 Double-click it, then click on "Hello world!" and enter "Orbis, te
106 saluto!" in the \gui Translation pane (the middle right of the
107 window). Don't forget the exclamation mark!
108
109 Click the \gui Done checkbox and choose \gui File|Save from the
110 menu bar. The TS file will no longer contain
111
112 \snippet doc/src/snippets/code/doc_src_examples_hellotr.qdoc 3
113
114 but instead will have
115
116 \snippet doc/src/snippets/code/doc_src_examples_hellotr.qdoc 4
117
118 To see the application running in Latin, we have to generate a QM
119 file from the TS file. Generating a QM file can be achieved
120 either from within \e {Qt Linguist} (for a single TS file), or
121 by using the command line program \c lrelease which will produce one
122 QM file for each of the TS files listed in the project file.
123 Generate \c hellotr_la.qm from \c hellotr_la.ts by choosing
124 \gui File|Release from \e {Qt Linguist}'s menu bar and pressing
125 \gui Save in the file save dialog that pops up. Now run the \c helloscript
126 program again. This time the button will be labelled "Orbis, te
127 saluto!".
128*/
Note: See TracBrowser for help on using the repository browser.