简体   繁体   English

为什么在Qt中创建主窗口之前必须先加载语言包?

[英]Why language packs have to be loaded before main window is created in Qt?

I just learned how to set a language pack for my app in Qt, and I am currently have two questions. 我刚刚学习了如何在Qt中为我的应用设置语言包,目前我有两个问题。 I wrote a minimal app to show where confused me. 我写了一个最小的应用程序来显示让我感到困惑的地方。
I created a main window, which only contains a QLabel to show text and a QPushButton to change label text from Chinese to English. 我创建了一个主窗口,其中仅包含一个用于显示文本的QLabel和一个用于将标签文本从中文更改为英语的QPushButton
And here is my code: 这是我的代码:

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    chineseTr = new QTranslator;
    chineseTr->load("../untitled/Language-Pack_zh-CN.qm");
    englishTr = new QTranslator;
    englishTr->load("../untitled/Language-Pack_en-US.qm");
    QCoreApplication::installTranslator(chineseTr);

    mWidget = new QWidget;
    setCentralWidget(mWidget);
    hLayout = new QHBoxLayout;
    mWidget->setLayout(hLayout);
    mLbl = new QLabel;
    mLbl->setText(tr("Text"));
    translateBtn = new QPushButton;
    connect(translateBtn, &QPushButton::clicked, this, &MainWindow::translateText);
    hLayout->addWidget(mLbl);
    hLayout->addWidget(translateBtn);
}

void MainWindow::translateText()
{
    QCoreApplication::removeTranslator(chineseTr);
    QCoreApplication::installTranslator(englishTr);
}

Question One: 问题一:
As you can see in the constructor, I loaded the language pack first, or the QLabel will just not show Chinese text. 正如您在构造函数中看到的那样,我首先加载了语言包,否则QLabel不会显示中文文本。 I can either load the Chinese language pack before creating everything else in the constructor, or load in main.cpp before creating MainWindow object. 我可以在构造函数中创建其他所有内容之前先加载中文包,或者在创建MainWindow对象之前先加载main.cpp Am I getting this wrong or those language packs just have to be loaded before creating class objects? 我是不是搞错了,还是在创建类对象之前只需要加载那些语言包?

Question Two: 问题二:
As you can see in the code, I clicked the button and to remove the Chinese language pack and install the new translator to load the English language pack. 正如您在代码中看到的那样,我单击了按钮并删除了中文语言包,并安装了新的翻译器以加载英语语言包。 But nothing works. 但是什么都行不通。 So how can I change language dynamically in the app? 那么,如何在应用程序中动态更改语言? I know this question may be simple, but I do not find any useful example online to show me how to do this? 我知道这个问题可能很简单,但是我没有在网上找到任何有用的示例来演示如何执行此操作? So can someone show me a way to do this or give me a hint? 那么有人可以告诉我一种方法来做到这一点还是可以给我提示?

Hope I explain myself clearly, and thanks in advance. 希望我能清楚地解释自己,并在此先感谢。

It's important to understand where translation happens: inside tr(...) call. 内部:了解那里发生的翻译是非常重要的tr(...)调用。 tr(...) returns the translated text. tr(...)返回翻译后的文本。 Which means, that replacing translator will influence subsequent calls to tr(...) , but have no chance to change texts already displayed in widgets. 这意味着,替换翻译器将影响对tr(...)后续调用,但没有机会更改窗口小部件中已经显示的文本。

Easiest way to do "switch language on the fly" is to introduce method containing all tr(...) calls and call it after replacing translator and in constructor. 实现“即时切换语言”的最简单方法是引入包含所有tr(...)调用的方法,并在替换翻译器并在构造函数中调用它。 I believe that code generated from .ui (the ones created by QtDesigner) files already has such method. 我相信从.ui (由QtDesigner创建的文件)文件生成的代码已经具有这种方法。

void MainWindow::translate()
{
     mLbl->setText(tr("Text"));
}

void MainWindow::translateText()
{
    QCoreApplication::removeTranslator(chineseTr);
    QCoreApplication::installTranslator(englishTr);
    translate();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM