| 1 |
/**************************************************************************** |
| 2 |
** |
| 3 |
** Copyright (C) 2017 The Qt Company Ltd. |
| 4 |
** Contact: https://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 https://www.qt.io/terms-conditions. For further |
| 15 |
** information use the contact form at https://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: https://www.gnu.org/licenses/fdl-1.3.html. |
| 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 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 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 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 script/helloscript/main.cpp 3 |
| 55 |
|
| 56 |
The contents of the script file are read. |
| 57 |
|
| 58 |
\snippet 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 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 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 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 code/doc_src_examples_hellotr.qdoc 3 |
| 113 |
|
| 114 |
but instead will have |
| 115 |
|
| 116 |
\snippet 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 |
*/ |