source: trunk/doc/src/examples/arrowpad.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: 8.3 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 linguist/arrowpad
30 \title Arrow Pad Example
31
32 This example is a slightly more involved and introduces a key \e
33 {Qt Linguist} concept: "contexts". It also shows how to use two
34 or more languages.
35
36 \image linguist-arrowpad_en.png
37
38 We will use two translations, French and Dutch, although there is no
39 effective limit on the number of possible translations that can be used
40 with an application. The relevant lines of \c arrowpad.pro are
41
42 \snippet examples/linguist/arrowpad/arrowpad.pro 0
43 \codeline
44 \snippet examples/linguist/arrowpad/arrowpad.pro 1
45
46 Run \c lupdate; it should produce two identical message files
47 \c arrowpad_fr.ts and \c arrowpad_nl.ts. These files will contain all the source
48 texts marked for translation with \c tr() calls and their contexts.
49
50 See the \l{Qt Linguist manual} for more information about
51 translating Qt application.
52
53 \section1 Line by Line Walkthrough
54
55 In \c arrowpad.h we define the \c ArrowPad subclass which is a
56 subclass of QWidget. In the screenshot above, the central
57 widget with the four buttons is an \c ArrowPad.
58
59 \snippet examples/linguist/arrowpad/arrowpad.h 0
60 \snippet examples/linguist/arrowpad/arrowpad.h 1
61 \snippet examples/linguist/arrowpad/arrowpad.h 2
62
63 When \c lupdate is run it not only extracts the source texts but it
64 also groups them into contexts. A context is the name of the class in
65 which the source text appears. Thus, in this example, "ArrowPad" is a
66 context: it is the context of the texts in the \c ArrowPad class.
67 The \c Q_OBJECT macro defines \c tr(x) in \c ArrowPad like this:
68
69 \snippet doc/src/snippets/code/doc_src_examples_arrowpad.qdoc 0
70
71 Knowing which class each source text appears in enables \e {Qt
72 Linguist} to group texts that are logically related together, e.g.
73 all the text in a dialog will have the context of the dialog's class
74 name and will be shown together. This provides useful information for
75 the translator since the context in which text appears may influence how
76 it should be translated. For some translations keyboard
77 accelerators may need to be changed and having all the source texts in a
78 particular context (class) grouped together makes it easier for the
79 translator to perform any accelerator changes without introducing
80 conflicts.
81
82 In \c arrowpad.cpp we implement the \c ArrowPad class.
83
84 \snippet examples/linguist/arrowpad/arrowpad.cpp 0
85 \snippet examples/linguist/arrowpad/arrowpad.cpp 1
86 \snippet examples/linguist/arrowpad/arrowpad.cpp 2
87 \snippet examples/linguist/arrowpad/arrowpad.cpp 3
88
89 We call \c ArrowPad::tr() for each button's label since the labels are
90 user-visible text.
91
92 \image linguist-arrowpad_en.png
93
94 \snippet examples/linguist/arrowpad/mainwindow.h 0
95 \snippet examples/linguist/arrowpad/mainwindow.h 1
96
97 In the screenshot above, the whole window is a \c MainWindow.
98 This is defined in the \c mainwindow.h header file. Here too, we
99 use \c Q_OBJECT, so that \c MainWindow will become a context in
100 \e {Qt Linguist}.
101
102 \snippet examples/linguist/arrowpad/mainwindow.cpp 0
103
104 In the implementation of \c MainWindow, \c mainwindow.cpp, we create
105 an instance of our \c ArrowPad class.
106
107 \snippet examples/linguist/arrowpad/mainwindow.cpp 1
108
109 We also call \c MainWindow::tr() twice, once for the action and
110 once for the shortcut.
111
112 Note the use of \c tr() to support different keys in other
113 languages. "Ctrl+Q" is a good choice for Quit in English, but a
114 Dutch translator might want to use "Ctrl+A" (for Afsluiten) and a
115 German translator "Strg+E" (for Beenden). When using \c tr() for
116 \key Ctrl key accelerators, the two argument form should be used
117 with the second argument describing the function that the
118 accelerator performs.
119
120 Our \c main() function is defined in \c main.cpp as usual.
121
122 \snippet examples/linguist/arrowpad/main.cpp 2
123 \snippet examples/linguist/arrowpad/main.cpp 3
124
125 We choose which translation to use according to the current locale.
126 QLocale::system() can be influenced by setting the \c LANG
127 environment variable, for example. Notice that the use of a naming
128 convention that incorporates the locale for \c .qm message files,
129 (and TS files), makes it easy to implement choosing the
130 translation file according to locale.
131
132 If there is no QM message file for the locale chosen the original
133 source text will be used and no error raised.
134
135 \section1 Translating to French and Dutch
136
137 We'll begin by translating the example application into French. Start
138 \e {Qt Linguist} with \c arrowpad_fr.ts. You should get the seven source
139 texts ("\&Up", "\&Left", etc.) grouped in two contexts ("ArrowPad"
140 and "MainWindow").
141
142 Now, enter the following translations:
143
144 \list
145 \o \c ArrowPad
146 \list
147 \o \&Up - \&Haut
148 \o \&Left - \&Gauche
149 \o \&Right - \&Droite
150 \o \&Down - \&Bas
151 \endlist
152 \o \c MainWindow
153 \list