Qt

 1. Implementing Multi-language Support in Qt

Here’s how to implement multi-language support in Qt:

QTranslator *trans = new QTranslator;
if (curLang == "us_EN") {
    trans->load(QCoreApplication::applicationDirPath() + "/language/us_EN.qm");
} else if (curLang == "zh_CN") {
    trans->load(QCoreApplication::applicationDirPath() + "/language/zh_CN.qm");
}
qApp->installTranslator(trans);

This code snippet should be placed before ui->setupUi(this); to ensure that the translation files are loaded before the UI is initialized.

2. Dynamic Multi-language Update for Sub-windows in Qt

To dynamically update the text content of sub-windows when switching languages, the retranslateUi() method must be called. This method, generated by Qt Designer, sets the translations for all text elements in the UI.

Detailed Steps to Call the retranslateUi() Method:

Detailed Steps to Call the retranslateUi() Method:

  1. 1. Ensure the Sub-window Has a retranslateUi() Method If the sub-window is created using Qt Designer, the retranslateUi() method will automatically be included in the ui_*.h file. For example, if your sub-window class is ChildWindow, it typically has a member object like Ui::ChildWindow, where retranslateUi() is defined.
  2. 2. Implement changeEvent(QEvent *event) in the Sub-window Class Ensure the sub-window overrides the changeEvent method to detect the QEvent::LanguageChange event and call retranslateUi().
// ChildWindow.h

ifndef CHILDWINDOW_H

define CHILDWINDOW_H

include

namespace Ui {

class ChildWindow;

}

class ChildWindow : public QWidget

{

Q_OBJECT

public:

explicit ChildWindow(QWidget *parent = nullptr);

~ChildWindow();

protected:

void changeEvent(QEvent *event) override; // Override changeEvent

private:

Ui::ChildWindow *ui;

};

endif // CHILDWINDOW_H
  • 3. Call retranslateUi() in the Sub-window Implementation In the changeEvent method, detect the language change event and call retranslateUi().
// ChildWindow.cpp
#include "ChildWindow.h"
#include "ui_ChildWindow.h"

ChildWindow::ChildWindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ChildWindow)
{
    ui->setupUi(this);
}

ChildWindow::~ChildWindow()
{
    delete ui;
}

void ChildWindow::changeEvent(QEvent *event)
{
    if (event->type() == QEvent::LanguageChange) {
        ui->retranslateUi(this);  // Update the UI text
    }
    QWidget::changeEvent(event);  // Call the base class changeEvent
}
  • 4. Install the Translator and Trigger Events in the Main Window Ensure that when switching languages, the translator is correctly installed, and changeEvent is triggered for both the main and sub-windows.
// MainWindow.cpp
#include "MainWindow.h"
#include "ChildWindow.h"
#include <QTranslator>
#include <QApplication>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    childWindow = new ChildWindow(this);  // Create sub-window
}

void MainWindow::switchLanguage(const QString &languageFile)
{
    QTranslator translator;
    if (translator.load(languageFile)) {
        qApp->installTranslator(&translator);  // Install the translator
        QEvent event(QEvent::LanguageChange);
        QApplication::sendEvent(this, &event);        // Trigger language change in main window
        QApplication::sendEvent(childWindow, &event); // Trigger language change in sub-window
    }
}

Summary:

  • Sub-windows need to override changeEvent and call retranslateUi() when QEvent::LanguageChange occurs.
  • Ensure that the language change event is correctly sent to all relevant windows, including sub-windows, when switching languages.
  • This approach ensures that both the main and sub-windows dynamically update their text content during language changes.

3. Refreshing Newly Added tr() in Qt Creator and Qt Linguist

The tr() function in Qt is used for string localization. When new or modified content is added within tr(), you need to refresh Qt Linguist to display the latest translations. Here are the steps:

  1. Update Translation Files (.ts):
    • Right-click the .pro file in the project tree and select “Run qmake” or choose “Run qmake” from the “Build” menu.
    • This updates the project files and detects new tr() calls.
  2. Generate or Update .ts Files:
    • Go to “Tools” → “External” → “Linguist” → “Update Translations” in Qt Creator. This automatically runs the lupdate command, updating the .ts files with the latest tr() strings.
  3. Refresh Qt Linguist:
    • Open the relevant .ts file to check for new tr() strings. If the new strings are not visible, try rerunning lupdate or manually editing the .ts file.
  4. Edit and Save Translations:
    • Use Qt Linguist to edit the translations in the .ts file and save your changes.
  5. Regenerate .qm Files:
    • In Qt Creator, go to “External” → “Linguist” → “Compile Translations” to regenerate the .qm files for use by your application.

After completing these steps, the new content within tr() will be refreshed and displayed correctly in Qt Linguist.

Leave a Reply

您的电子邮箱地址不会被公开。 必填项已用 * 标注

Related Post