Archives For QtCreator (IDE)

Think international from the beginning

Even if you think, that you don’t need to support more then one language – it’s better to have this in mind from the beginning, because it’s hard to add later.

I’m developing business apps and business is international, so I’m always developing for multi-language support.

This article is a short summary what’s needed and covers configuration, translation and build process.

Configure Qt Creator External Tools

There’s an extra application Qt Linguist to translate and two important commands lupdate and lrelease. Using External Tools it’s easy to have access without leaving Qt Creator or the need of the command line. Here’s HowTo:

1. Configure easy access to Qt Linguist

Together with you Qt installation you got the Qt Linguist. Please take a look at the manual: http://doc.qt.io/qt-5/qtlinguist-index.html

qt_ling_manual

I’m living in Germany and always develop my APPs in english and immediately doing the translation to german. This way I always detect soon if I forgot to make a String translatable. (we’ll talk about this later) Also german words normaly need some more space so I can control how the app looks in german vs english. I already used the Qt Linguist for my BlackBerry 10 Cascades development and really like the tool.

You’ll find the Qt Linguist app here:

configure_creator_03

Thanks to tips I got from Qt community (creator list) I found out that you can open Qt Linguist directly from Qt Creator.

Preferences -> Environment –> External Tools is the key:

configure_creator_02

Fill out the description (Linguist) and path to the Executable (Tap Choose and Select Linguist)

It’s a good idea to set the Working Directory. Instead of a hardcoded path you can also use a variable: %{CurrentProject:Path} Now the Linguist always opens inside your current project.

Adding the External Tool will place it under ‘Uncategorized’ – move it via Drag-n-Drop under existing Linguist category.

Now it’s easy to start the Linguist from inside your Qt Creator:

qt-linguist-logo

configure_creator_05

From Linguist open the files to translate (.ts).

A first screenshot from Linguist so you’ll get the idea:

configure_creator_06

As I opened Qt Linguist first time, I got it in german. Unfortunately there’s no language settings as described for Qt Creator above.

This little trick does it:

configure_creator_07

Search the translation files for linguist and rename the linguist_de.qm file – now Linguist opens in english.

(I created a Feature Request: https://bugreports.qt.io/browse/QTBUG-52456)

2. Configure lupdate in External Tools

Under Preferences –> Environment –> External Tools there already exist access to commands for lupdate and lrelease. (lupdate is looking for the strings you marked as translatable and to create or update the translation files <app>.ts. Using Linguist or external tools you can then translate the strings. lrelease compiles these translations from *.ts into *.qm. Only *.qml files will be deployed to your device)

I found out that the default configuration doesn’t work on Android and iOS – please change the executable:

06_qtc_ext_tools_04

3. Configure lrelease in External Tools

Do the same for lrelease:

06_qtc_ext_tools_05

That’s all to configure.

Make your Strings translatable

You can read in detail about this in Qt documentation:

Here’s a short starter:

To enable your app to use a Translator you have to add some lines of code into your main.cpp:

#include <QTranslator>
    // ...
    QGuiApplication app(argc, argv);

    QTranslator translator;
    if (translator.load(QLocale(), QLatin1String("my_app"), QLatin1String("_"), QLatin1String(":/translations"))) {
        app.installTranslator(&translator);
    } else {
        qDebug() << "cannot load translator " << QLocale::system().name() << " check content of translations.qrc";
    }

    QQmlApplicationEngine engine;
	// ...

Translation in your C++ sources is done using tr(), in QML files using qsTr()

QML: (simple text to be translated)

Label {
    text: qsTr("City")
}

QML: (text with arguments)

Label {
	property int theValue: 42
	text: qsTr("The answer of all is %1").arg(theValue)
}

QML: (text with arguments and quantity)

Label {
	property int count: 1
	text: qsTr("%1 piece(s)","",count).arg(count)
}

That’s all you need to know for your sourcecode.

HowTo extract translatable Strings

Now as your C++ and QML code contains all the stuff to make your Strings translatable – HowTo get these Strings out of your code to be able to translate using Qt Linguist or other solutions ?

lupdate does this. If you followed my tips above, you can use lupdate from External Tools inside Qt Creator. At first lupdate must know the languages used and also where to find the C++ and QML sources. Here are the relevant parts from my_app.pro:

TARGET = my_app
# cpp sources
SOURCES += main.cpp
# qml sources
lupdate_only {
    SOURCES +=  main.qml \
    common/*.qml \
    demo/*.qml
}
# Supported languages
LANGUAGES = de en fr
# used to create .ts files
 defineReplace(prependAll) {
     for(a,$$1):result += $$2$${a}$$3
     return($$result)
 }
 # Available translations
 tsroot = $$join(TARGET,,,.ts)
 tstarget = $$join(TARGET,,,_)
 TRANSLATIONS = $$PWD/translations/$$tsroot
 TRANSLATIONS += $$prependAll(LANGUAGES, $$PWD/translations/$$tstarget, .ts)

The compiler needs all C++ sources, lupdate also needs the qml sources. Using lupdate_only {} does the trick not to confuse the compiler. To avoid the need to create .ts files for each language the $$prependAll – stuff does this:

  • create my_app.ts
  • create my_app_de.ts
  • create my_app_en.ts
  • create my_app_fr.ts

All files are created inside project working dir at /translations – so this directory should be there.

Translate your Strings

Now open Qt Linguist from External Tools and select the .ts files.

Attention: Never open the rootfile my_app.ts – this file will always be regenerated from lupdate !.

Build and Run your APP on Android or iOS Devices

The compiled APP won’t use the *.ts files – the APPs always need the compressed compiled version *.qm.

lrelease compiles your .ts files into .qm files. Here’s the part for this in your my_app.pro:

# run LRELEASE to generate the qm files
qtPrepareTool(LRELEASE, lrelease)
 for(tsfile, TRANSLATIONS) {
     command = $$LRELEASE $$tsfile
     system($$command)|error("Failed to run: $$command")
 }

This will create the *.qm files where your *.ts files are: at root of your project dir in /translations folder.

If you take a look at the code in your main.cpp (see above) where QTranslater was loaded using :/translations as path to my_app_xx.qm files, you know from :/ that *.qm files must be part of Qt Resources.

For this from my_app.pro we’re providing a .qrc file containing all the qm files:

RESOURCES += qml.qrc \
    translations.qrc

Unfortunately the .qm files won’t automatically become part of translations.qrc. You have to add them manually. This only must be done if adding new languages. In this case you have to at the language to LANGUAGES in .pro, then run lupdate and build your project to create the new .ts and .qm files. Then you can right-click on translations.qrc and use Add Existing Files to add the .qm for the new language.

That’s it πŸ™‚

Summary

I spent much time to figure this out and to find a way without use of command line. Hopefully I described it in a way to make it easy for you to start translations in your Android and iOS APPs.

Here are the most important workflows HowTo translate your text and HowTo add more supported languages.

(The app from the screenshots below is named ‘one_page_x’)

Workflow adding / editing translatable Strings

workflow_text_edited

Workflow supporting a new language

workflow_new_language

Cascades does it different

Doing x-platform apps also for BlackBerry 10 (C++/Qt 4.8/Cascades) it’s really easy:

Only add the languages to your project file (bar-descriptor.xml) …

cascades_add

… then build the project and all the lupdate/lrelease ts/qm stuff is done by magic under the hood:

cascades_ts_qm


← Back (Qt Creator – First Deployment)

β†’ Next Article (HowTo survive as a Qt Newbie)

⇐ Home (Overview / Topics)

Edited 2016-06-05: Now with Qt 5.7 RC the Gallery is included and enhanced.

Edited 2016-04-28: Unfortunately Qt 5.7 Beta doesn’t include the Gallery because of some renaming-trouble. To test a first deployment running Qt 5.7 Beta please follow this article: qt-5.7-beta


Now it’s time to check if your installation and settings are well done. Easiest way to do this is to deploy an existing example to Android and iOS.

Qt Quick Controls 2 Gallery Example

Open Qt Creator, then goto Welcome –> Examples and search for labs.controls: (Qt 5.6) or search for Gallery 2 from Qt 5.7

01_first_deploy

In 5.6 where qt.labs.controls are only a TechPreview there’s only one Example: Gallery

Build and Run Configuration

Click on Gallery selects ‘Project’ on the left side and opens two windows: Project Build and Help:

02_first_deploy

Click on Add Kit to add your Kits:

03_first_deploy

I always only test on real devices, so I’m using these two Kits: Android for arm and iPhone.

04_first_deploy

Hover with your mouse over the top-right corner of a Kit and you can Remove it.

Each Kit has Build and Run Tabs where you can configure the build. Qt Creator should have done most of the work automatically – so for running the Example it should work out of the box.

Here’s the Android Build:

05_first_deploy

check the APK Build, where you can select the SDK. Also there’s the checkbox to use Gradle and how Qt libraries are bundled:

06_first_deploy

Compile, Deploy and Run on Android

Now connect your Android Phone via USB.

At left-bottom side please select the correct Kit to be used: Android – Debug:

07_first_deploy

Now we can (Build and) Run the APP:

08_first_deploy

Qt Creator asks you which Device to use. We already have connected the Android Device: STV100-4 is the BlackBerry PRIV:

09_first_deploy

Select the Device and Qt Creator will compile and build. Open the Compile Output to see what happens:

10_first_deploy

As next the APP was deployed to the Device and started. You can verify the Console output from ‘Application Output’.

priv

Compile, Deploy and Run on iOS

Now we want to do the same for iOS.

Connect your iPhone via USB and again select the right Kit:

11_first_deploy

This time there’s no extra Dialog to select the Device – it’s on top of Kit Selection. iPhone and Debug are selected – so we can Run again.

Project compiles, deploys and runs the APP on iPhone.

12_first_deploy

13_first_deploy

iphone

Go through this Gallery Example to see most of UI and Navigation Controls in Action.

From Settings you can switch between Material, Universal andΒ  Default.


← Back (Qt Creator – Settings)

β†’ Next Article (HowTo Translations (i18n))

⇐ Home (Overview / Topics)


Edited 2016-06-05: In the meantime with Qt 5.7 RC and Qt Creator 4.0 some screenshots look slightly different.

Edited 2016-04-28:


You should have read my Qt Intro before going on.

Please open Qt Creator –> Preferences

qt-creator-logo

It would take some time to explain all the settings. We’ll do this step-by-step while developing mobile APPs next weeks.

Here are some important settings you should do before start.

Set your language

configure_creator_01

Qt Creator supports many languages and by default will open using your locale.

For me developing software I got a better feeling using english for the IDE.

From Preferences -> Environment -> Interface you can set the language.

Build and Run

Please check Build and Run Settings. From my POV it’s a good idea to see what happens while a project was compiled and deployed to / run on a mobile device:

configure_creator_08

There’s also the path to your build directory, which is by default outside your projects at root of your working directory.

External Tools – Linguist

There are some important settings to open Qt Linguist directly from inside Qt Creator and HowTo update translatable Strings-

This is covered in HowTo Translations (i18n) – going on to the next sites you’ll read about.

Configure for Android

configure_creator_04

If you followed my blog you already have installed Android SDK and NDK.

Take a look at Preferences -> Android -> Android Configurations. JDK, SDK and NDK locations should point to the correct folder.

There’s also a checkbox to enable Gradle instead of Ant. Gradle is the recommended configuration.

Now we can verify if our installation and settings are correct and run a first application on a real device.


← Back (Qt Creator – Intro)

β†’ Next Article (First Deployment on Android / iOS)

⇐ Home (Overview / Topics)


You have followed my instructions and installed Qt.

Update 2016-06-05: Together with Qt 5.7 you’ll get Qt Creator 4.0. The new Creator uses a flat theme and was redesigned, so some screens look slightly different to the screenshots here.

Where’s my IDE ?

Inside the directory where you installed Qt you’ll find the IDE: Qt Creator.

OSX:

01_creator_intro

qt-creator-logo

Doubleclick to start Qt Creator.

Read the Manual

Coming from Android Studio, Eclipse Momentics or Xcode it will take some time to learn how things are done.

In this case I really recommend not to start development without reading the manual: http://doc.qt.io/qtcreator/index.html

02_creator_intro

Qt Creator

Qt Creator runs crossplatform (OSX, Linux, Windows) and supports compiling and building for many different platforms from Desktop to Mobile or Embedded. In this blog series I’ll only cover mobile development for Android and iOS – later followed by Windows10.

Here are some screenshots to give you a first overview. You’ll learn how to use Qt Creator following my blog series.

Opening Qt Creator you’ll get this:

03_creator_intro

Help gives you access to the really helpful and detailed documentation:

04_creator_intro

Before starting with development we’ll take a look at some important Settings.


← Back (Qt 5.7 RC)

β†’ Next Article (Qt Creator – Settings)

⇐ Home (Overview / Topics)