{"id":341,"date":"2024-09-25T21:10:42","date_gmt":"2024-09-25T13:10:42","guid":{"rendered":"https:\/\/chaixiangyu.cn\/?p=341"},"modified":"2024-09-25T21:34:49","modified_gmt":"2024-09-25T13:34:49","slug":"qt","status":"publish","type":"post","link":"https:\/\/chaixiangyu.cn\/?p=341","title":{"rendered":"Qt"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u00a01. Implementing Multi-language Support in Qt<\/h2>\n\n\n\n<p>Here&#8217;s how to implement multi-language support in Qt:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>QTranslator *trans = new QTranslator;\nif (curLang == \"us_EN\") {\n    trans->load(QCoreApplication::applicationDirPath() + \"\/language\/us_EN.qm\");\n} else if (curLang == \"zh_CN\") {\n    trans->load(QCoreApplication::applicationDirPath() + \"\/language\/zh_CN.qm\");\n}\nqApp->installTranslator(trans);\n<\/code><\/code><\/pre>\n\n\n\n<p>This code snippet should be placed before <code>ui-&gt;setupUi(this);<\/code> to ensure that the translation files are loaded before the UI is initialized.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. Dynamic Multi-language Update for Sub-windows in Qt<\/h3>\n\n\n\n<p>To dynamically update the text content of sub-windows when switching languages, the <code>retranslateUi()<\/code> method must be called. This method, generated by Qt Designer, sets the translations for all text elements in the UI.<\/p>\n\n\n\n<p><strong>Detailed Steps to Call the <code>retranslateUi()<\/code> Method:<\/strong><\/p>\n\n\n\n<p><strong>Detailed Steps to Call the <code>retranslateUi()<\/code> Method:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>1. Ensure the Sub-window Has a <code>retranslateUi()<\/code> Method<\/strong> If the sub-window is created using Qt Designer, the <code>retranslateUi()<\/code> method will automatically be included in the <code>ui_*.h<\/code> file. For example, if your sub-window class is <code>ChildWindow<\/code>, it typically has a member object like <code>Ui::ChildWindow<\/code>, where <code>retranslateUi()<\/code> is defined.<\/li>\n\n\n\n<li>2. <strong>Implement <code>changeEvent(QEvent *event)<\/code> in the Sub-window Class<\/strong> Ensure the sub-window overrides the <code>changeEvent<\/code> method to detect the <code>QEvent::LanguageChange<\/code> event and call <code>retranslateUi()<\/code>.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ ChildWindow.h\n\nifndef CHILDWINDOW_H\n\ndefine CHILDWINDOW_H\n\ninclude\n\nnamespace Ui {\n\nclass ChildWindow;\n\n}\n\nclass ChildWindow : public QWidget\n\n{\n\nQ_OBJECT\n\npublic:\n\nexplicit ChildWindow(QWidget *parent = nullptr);\n\n~ChildWindow();\n\nprotected:\n\nvoid changeEvent(QEvent *event) override; \/\/ Override changeEvent\n\nprivate:\n\nUi::ChildWindow *ui;\n\n};\n\nendif \/\/ CHILDWINDOW_H<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>3. <strong>Call <code>retranslateUi()<\/code> in the Sub-window Implementation<\/strong> In the <code>changeEvent<\/code> method, detect the language change event and call <code>retranslateUi()<\/code>.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ ChildWindow.cpp\n#include \"ChildWindow.h\"\n#include \"ui_ChildWindow.h\"\n\nChildWindow::ChildWindow(QWidget *parent) :\n    QWidget(parent),\n    ui(new Ui::ChildWindow)\n{\n    ui->setupUi(this);\n}\n\nChildWindow::~ChildWindow()\n{\n    delete ui;\n}\n\nvoid ChildWindow::changeEvent(QEvent *event)\n{\n    if (event->type() == QEvent::LanguageChange) {\n        ui->retranslateUi(this);  \/\/ Update the UI text\n    }\n    QWidget::changeEvent(event);  \/\/ Call the base class changeEvent\n}\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>4. <strong>Install the Translator and Trigger Events in the Main Window<\/strong> Ensure that when switching languages, the translator is correctly installed, and <code>changeEvent<\/code> is triggered for both the main and sub-windows.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ MainWindow.cpp\n#include \"MainWindow.h\"\n#include \"ChildWindow.h\"\n#include &lt;QTranslator>\n#include &lt;QApplication>\n\nMainWindow::MainWindow(QWidget *parent) :\n    QMainWindow(parent),\n    ui(new Ui::MainWindow)\n{\n    ui->setupUi(this);\n    childWindow = new ChildWindow(this);  \/\/ Create sub-window\n}\n\nvoid MainWindow::switchLanguage(const QString &amp;languageFile)\n{\n    QTranslator translator;\n    if (translator.load(languageFile)) {\n        qApp->installTranslator(&amp;translator);  \/\/ Install the translator\n        QEvent event(QEvent::LanguageChange);\n        QApplication::sendEvent(this, &amp;event);        \/\/ Trigger language change in main window\n        QApplication::sendEvent(childWindow, &amp;event); \/\/ Trigger language change in sub-window\n    }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Summary:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Sub-windows need to override <code>changeEvent<\/code> and call <code>retranslateUi()<\/code> when <code>QEvent::LanguageChange<\/code> occurs.<\/li>\n\n\n\n<li>Ensure that the language change event is correctly sent to all relevant windows, including sub-windows, when switching languages.<\/li>\n\n\n\n<li>This approach ensures that both the main and sub-windows dynamically update their text content during language changes.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">3. Refreshing Newly Added <code>tr()<\/code> in Qt Creator and Qt Linguist<\/h3>\n\n\n\n<p>The <code>tr()<\/code> function in Qt is used for string localization. When new or modified content is added within <code>tr()<\/code>, you need to refresh Qt Linguist to display the latest translations. Here are the steps:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Update Translation Files (.ts):<\/strong>\n<ul class=\"wp-block-list\">\n<li>Right-click the <code>.pro<\/code> file in the project tree and select \u201cRun qmake\u201d or choose \u201cRun qmake\u201d from the \u201cBuild\u201d menu.<\/li>\n\n\n\n<li>This updates the project files and detects new <code>tr()<\/code> calls.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Generate or Update <code>.ts<\/code> Files:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Go to \u201cTools\u201d \u2192 \u201cExternal\u201d \u2192 \u201cLinguist\u201d \u2192 \u201cUpdate Translations\u201d in Qt Creator. This automatically runs the <code>lupdate<\/code> command, updating the <code>.ts<\/code> files with the latest <code>tr()<\/code> strings.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Refresh Qt Linguist:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Open the relevant <code>.ts<\/code> file to check for new <code>tr()<\/code> strings. If the new strings are not visible, try rerunning <code>lupdate<\/code> or manually editing the <code>.ts<\/code> file.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Edit and Save Translations:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Use Qt Linguist to edit the translations in the <code>.ts<\/code> file and save your changes.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Regenerate <code>.qm<\/code> Files:<\/strong>\n<ul class=\"wp-block-list\">\n<li>In Qt Creator, go to \u201cExternal\u201d \u2192 \u201cLinguist\u201d \u2192 \u201cCompile Translations\u201d to regenerate the <code>.qm<\/code> files for use by your application.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<p>After completing these steps, the new content within <code>tr()<\/code> will be refreshed and displayed correctly in Qt Linguist.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u00a01. Implementing Multi-language Support in Qt Here&#038;#821 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-341","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/chaixiangyu.cn\/index.php?rest_route=\/wp\/v2\/posts\/341","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/chaixiangyu.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chaixiangyu.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chaixiangyu.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/chaixiangyu.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=341"}],"version-history":[{"count":3,"href":"https:\/\/chaixiangyu.cn\/index.php?rest_route=\/wp\/v2\/posts\/341\/revisions"}],"predecessor-version":[{"id":351,"href":"https:\/\/chaixiangyu.cn\/index.php?rest_route=\/wp\/v2\/posts\/341\/revisions\/351"}],"wp:attachment":[{"href":"https:\/\/chaixiangyu.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=341"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chaixiangyu.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=341"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chaixiangyu.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=341"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}