Language Translator using Qt Creator

In this post, you will learn how to change text language using Qt creator.

Steps:

  1. Open Qt Creator Click on File from the menu and click on New File or Project…(Ctrl +  N).
  2. From the left column select Application, from the middle column select Qt Widget Application.
  3. Click on Choose.
  4. Give a name to the project and set the path where you want to save this project files.
  5. Click Next,
  6. Default kits will be selected.
  7. Click next.
  8. Give the class name of your choice. I gave LanguageTranslate.
  9. Select Baseclass as QMainWindow from the dropdown list.
  10. (Generate form box can be unchecked as we are not going to use it, we will be creating UI with code only).
  11. Click Next
  12. Click on Finish.

main.cpp doesn`t need to be modified.

Add translations to your Project

In your qmake project file, the translation variable has to be added and must contain all language files of your choice that you want to work with.

Open “LanguageTranslate.pro” file and add the line mentioned below.

TRANSLATIONS += translation_fa.ts

Open languagetranslate.h

and modify it as

#ifndef LANGUAGETRANSLATE_H
#define LANGUAGETRANSLATE_H

#include <QMainWindow>
#include <QLabel>
#include <QPushButton>
#include <QTranslator>

#define MAX_LANG_COUNTER 3

namespace Ui {
class LanguageTranslate;
}

class LanguageTranslate : public QMainWindow
{
    Q_OBJECT

public:
    explicit LanguageTranslate(QWidget *parent = nullptr);
    ~LanguageTranslate();
    QLabel *lang_label;
    QPushButton *bt_change_lang;
    QTranslator translator;
private:
    int languageChangeFlag;
    Ui::LanguageTranslate *ui;
private slots:
    void handle_Language_Change();
protected:
    void changeEvent(QEvent*);
};

#endif // LANGUAGETRANSLATE_H

Open languagetranslate.cpp

and copy-paste below mentioned code.

Download Project files

Video

Thank you for reading.

4 thoughts on “Language Translator using Qt Creator”

Leave a Comment

Your email address will not be published. Required fields are marked *