Marking strings for localization

All the way back in Chapter 1, Getting Started with Qt Creator, I told you to always mark your strings for localization using the tr and qsTr functions: tr for C++ and qsTr for QML strings. Doing so has two key advantages:

  • First, it enables Qt to find every string that needs localization
  • Second, if you install a Qt translator object in your application and provide a translation file, the strings you wrap with these functions are automatically replaced by their localized equivalent

Let's examine the use of tr in more detail. All Qt objects that include the Q_OBJECT macro in their declaration include the tr function. You've seen it with one argument, as follows:

button = new QPushButton(tr("&Quit"), this);

The leading ...

Get Application Development with Qt Creator - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.