Arrow Pad Example

Image non disponible

We will use two translations, French and Dutch, although there is no effective limit on the number of possible translations that can be used with an application. The relevant lines of arrowpad.pro are

 
Sélectionnez
HEADERS      = arrowpad.h \
               mainwindow.h
SOURCES      = arrowpad.cpp \
               main.cpp \
               mainwindow.cpp

TRANSLATIONS = arrowpad_fr.ts \
               arrowpad_nl.ts

Run lupdate; it should produce two identical message files arrowpad_fr.ts and arrowpad_nl.ts. These files will contain all the source texts marked for translation with tr() calls and their contexts.

See the Qt Linguist Manual for more information about translating Qt application.

Line by Line Walkthrough

In arrowpad.h we define the ArrowPad subclass which is a subclass of QWidget. In the screenshot above, the central widget with the four buttons is an ArrowPad.

 
Sélectionnez
class ArrowPad : public QWidget
{
    Q_OBJECT

When lupdate is run it not only extracts the source texts but it also groups them into contexts. A context is the name of the class in which the source text appears. Thus, in this example, "ArrowPad" is a context: it is the context of the texts in the ArrowPad class. The Q_OBJECT macro defines tr(x) in ArrowPad like this:

 
Sélectionnez
qApp->translate("ArrowPad", x)

Knowing which class each source text appears in enables Qt Linguist to group texts that are logically related together, e.g. all the text in a dialog will have the context of the dialog's class name and will be shown together. This provides useful information for the translator since the context in which text appears may influence how it should be translated. For some translations keyboard accelerators may need to be changed and having all the source texts in a particular context (class) grouped together makes it easier for the translator to perform any accelerator changes without introducing conflicts.

In arrowpad.cpp we implement the ArrowPad class.

 
Sélectionnez
    upButton = new QPushButton(tr("&Up"));
    downButton = new QPushButton(tr("&Down"));
    leftButton = new QPushButton(tr("&Left"));
    rightButton = new QPushButton(tr("&Right"));

We call ArrowPad::tr() for each button's label since the labels are user-visible text.

Image non disponible
 
Sélectionnez
class MainWindow : public QMainWindow
{
    Q_OBJECT

In the screenshot above, the whole window is a MainWindow. This is defined in the mainwindow.h header file. Here too, we use Q_OBJECT, so that MainWindow will become a context in Qt Linguist.

 
Sélectionnez
    arrowPad = new ArrowPad;

In the implementation of MainWindow, mainwindow.cpp, we create an instance of our ArrowPad class.

 
Sélectionnez
    exitAct = new QAction(tr("E&xit"), this);
    exitAct->setShortcuts(QKeySequence::Quit);
    connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));

We also call MainWindow::tr() twice, once for the action and once for the shortcut.

Note the use of tr() to support different keys in other languages. "Ctrl+Q" is a good choice for Quit in English, but a Dutch translator migh