| 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/helloscript | 
|---|
| 44 | \title Hello Script Example | 
|---|
| 45 |  | 
|---|
| 46 | The Hello Script example shows the basic use of Qt Script: How to embed | 
|---|
| 47 | a script engine into the application, how to evaluate a script, and how | 
|---|
| 48 | to process the result of the evaluation. The example also shows how to | 
|---|
| 49 | apply internationalization to scripts. | 
|---|
| 50 |  | 
|---|
| 51 | \snippet examples/script/helloscript/main.cpp 0 | 
|---|
| 52 |  | 
|---|
| 53 | The application will load the script file to evaluate from a resource, so | 
|---|
| 54 | we first make sure that the resource is initialized. | 
|---|
| 55 |  | 
|---|
| 56 | \snippet examples/script/helloscript/main.cpp 1 | 
|---|
| 57 |  | 
|---|
| 58 | We attempt to load a translation, and install translation functions in the | 
|---|
| 59 | script engine. How to produce a translation is explained later. | 
|---|
| 60 |  | 
|---|
| 61 | \snippet examples/script/helloscript/main.cpp 2 | 
|---|
| 62 |  | 
|---|
| 63 | A push button is created and exported to the script environment as a | 
|---|
| 64 | global variable, \c button. Scripts will be able to access properties, | 
|---|
| 65 | signals and slots of the button as properties of the \c button script | 
|---|
| 66 | object; the script object acts as a proxy to the C++ button object. | 
|---|
| 67 |  | 
|---|
| 68 | \snippet examples/script/helloscript/main.cpp 3 | 
|---|
| 69 |  | 
|---|
| 70 | The contents of the script file are read. | 
|---|
| 71 |  | 
|---|
| 72 | \snippet examples/script/helloscript/helloscript.qs 0 | 
|---|
| 73 |  | 
|---|
| 74 | The script sets the \c text (note that the qTr() function is used to allow | 
|---|
| 75 | for translation) and \c styleSheet properties of the button, and calls the | 
|---|
| 76 | button's \c show() slot. | 
|---|
| 77 |  | 
|---|
| 78 | \snippet examples/script/helloscript/main.cpp 4 | 
|---|
| 79 |  | 
|---|
| 80 | The script is evaluated. Note that the file name is passed as the | 
|---|
| 81 | (optional) second parameter; this makes it possible for the script engine | 
|---|
| 82 | to produce a meaningful backtrace if something goes wrong, and makes the | 
|---|
| 83 | qTr() function be able to resolve the translations that are associated | 
|---|
| 84 | with this script. | 
|---|
| 85 |  | 
|---|
| 86 | \snippet examples/script/helloscript/main.cpp 5 | 
|---|
| 87 |  | 
|---|
| 88 | If the result is an Error object (e.g. the script contained a syntax | 
|---|
| 89 | error, or tried to call a function that doesn't exist), we obtain | 
|---|
| 90 | the line number and string representation of the error and display | 
|---|
| 91 | it in a message box. | 
|---|
| 92 |  | 
|---|
| 93 | \snippet examples/script/helloscript/main.cpp 6 | 
|---|
| 94 |  | 
|---|
| 95 | If the evaluation went well, the application event loop is entered. | 
|---|
| 96 |  | 
|---|
| 97 | \section1 Translating the Application | 
|---|
| 98 |  | 
|---|
| 99 | The Qt Script internalization support builds on what Qt already provides | 
|---|
| 100 | for C++; see the \l{Hello tr() Example} for an introduction. | 
|---|
| 101 |  | 
|---|
| 102 | Since we haven't made the translation file \c helloscript_la.qm, the | 
|---|
| 103 | source text is shown when we run the application ("Hello world!"). | 
|---|
| 104 |  | 
|---|
| 105 | To generate the translation file, run \c lupdate as follows: | 
|---|
| 106 |  | 
|---|
| 107 | \code | 
|---|
| 108 | lupdate helloscript.qs -ts helloscript_la.ts | 
|---|
| 109 | \endcode | 
|---|
| 110 |  | 
|---|
| 111 | You should now have a file \c helloscript_la.ts in the current | 
|---|
| 112 | directory. Run \c linguist to edit the translation: | 
|---|
| 113 |  | 
|---|
| 114 | \code | 
|---|
| 115 | linguist helloscript_la.ts | 
|---|
| 116 | \endcode | 
|---|
| 117 |  | 
|---|
| 118 | You should now see the text "helloscript.qs" in the top left pane. | 
|---|
| 119 | Double-click it, then click on "Hello world!" and enter "Orbis, te | 
|---|
| 120 | saluto!" in the \gui Translation pane (the middle right of the | 
|---|
| 121 | window). Don't forget the exclamation mark! | 
|---|
| 122 |  | 
|---|
| 123 | Click the \gui Done checkbox and choose \gui File|Save from the | 
|---|
| 124 | menu bar. The \c .ts file will no longer contain | 
|---|
| 125 |  | 
|---|
| 126 | \snippet doc/src/snippets/code/doc_src_examples_hellotr.qdoc 3 | 
|---|
| 127 |  | 
|---|
| 128 | but instead will have | 
|---|
| 129 |  | 
|---|
| 130 | \snippet doc/src/snippets/code/doc_src_examples_hellotr.qdoc 4 | 
|---|
| 131 |  | 
|---|
| 132 | To see the application running in Latin, we have to generate a \c .qm | 
|---|
| 133 | file from the \c .ts file. Generating a \c .qm file can be achieved | 
|---|
| 134 | either from within \e {Qt Linguist} (for a single \c .ts file), or | 
|---|
| 135 | by using the command line program \c lrelease which will produce one \c | 
|---|
| 136 | .qm file for each of the \c .ts files listed in the project file. | 
|---|
| 137 | Generate \c hellotr_la.qm from \c hellotr_la.ts by choosing | 
|---|
| 138 | \gui File|Release from \e {Qt Linguist}'s menu bar and pressing | 
|---|
| 139 | \gui Save in the file save dialog that pops up. Now run the \c helloscript | 
|---|
| 140 | program again. This time the button will be labelled "Orbis, te | 
|---|
| 141 | saluto!". | 
|---|
| 142 | */ | 
|---|