Qt 编写应用支持多语言版本--一个GUI应用示例 (3)

然后是 HelloQt.h

#pragma once #include <QtWidgets/QMainWindow> #include <QTranslator> #include "myUI_HelloQt.h" class HelloQt : public QMainWindow { Q_OBJECT public: HelloQt(QWidget *parent = Q_NULLPTR); public slots: void on_trigger_language_zh(bool checked); void on_trigger_language_en(bool checked); protected slots: void on_btn_quit_clicked(bool clicked); protected: void changeEvent(QEvent *event) override; private: Ui::HelloQtClass ui; QTranslator *language_zh; };

然后是 HelloQt.c

#include <QDebug> #include "HelloQt.h" HelloQt::HelloQt(QWidget *parent) : QMainWindow(parent) { ui.setupUi(this); // load translation language_zh = new QTranslator(this); language_zh->load(":/translations/helloqt_zh"); // signals and slots QObject::connect(ui.act_language_zh, SIGNAL(triggered(bool)), this, SLOT(on_trigger_language_zh(bool))); QObject::connect(ui.act_language_en, SIGNAL(triggered(bool)), this, SLOT(on_trigger_language_en(bool))); QObject::connect(ui.btn_quit, SIGNAL(clicked(bool)), this, SLOT(on_btn_quit_clicked(bool))); } void HelloQt::on_trigger_language_zh(bool checked) { qDebug() << QObject::tr("install Chinese") << endl; qApp->installTranslator(language_zh); } void HelloQt::on_trigger_language_en(bool checked) { qDebug() << HelloQt::tr("install English") << endl; qApp->removeTranslator(language_zh); } void HelloQt::on_btn_quit_clicked(bool clicked) { qApp->quit(); } //************************************ // Method: changeEvent // FullName: HelloQt::changeEvent // Access: protected // Returns: void // Qualifier: // Parameter: QEvent * event // Description: change language. //************************************ void HelloQt::changeEvent(QEvent *event) { if (event->type() == QEvent::LanguageChange) { ui.retranslateUi(this); } else { QMainWindow::changeEvent(event); } }

然后是翻译文件 helloqt_zh.ts

<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE TS> <TS version="2.1" language="zh_CN"> <context> <name>HelloQt</name> <message> <location filename="HelloQt.cpp" line="27"/> <source>install English</source> <oldsource>English</oldsource> <translation>安装英文</translation> </message> </context> <context> <name>HelloQtClass</name> <message> <location filename="myUI_HelloQt.h" line="80"/> <source>HelloQt</source> <translation>你好Qt</translation> </message> </context> <context> <name>QObject</name> <message> <location filename="myUI_HelloQt.h" line="49"/> <location filename="myUI_HelloQt.h" line="83"/> <source>Chinese</source> <translation>中文</translation> </message> <message> <location filename="myUI_HelloQt.h" line="50"/> <location filename="myUI_HelloQt.h" line="84"/> <source>English</source> <translation>英文</translation> </message> <message> <location filename="myUI_HelloQt.h" line="56"/> <location filename="myUI_HelloQt.h" line="85"/> <source>&lt;h2&gt;&lt;i&gt;Hello&lt;/i&gt;&lt;font color=red&gt;Qt!&lt;/font&gt;&lt;/h2&gt;</source> <translation>&lt;h2&gt;&lt;i&gt;你好&lt;/i&gt;&lt;font color=red&gt;Qt!&lt;/font&gt;&lt;/h2&gt;</translation> </message> <message> <location filename="myUI_HelloQt.h" line="59"/> <location filename="myUI_HelloQt.h" line="86"/> <source>&amp;Quit</source> <translation>&amp;退出</translation> </message> <message> <location filename="HelloQt.cpp" line="21"/> <source>install Chinese</source> <translation>安装中文</translation> </message> </context> </TS>

然后是资源文件 HelloQt.qrc

<RCC> <qresource prefix="/translations"> <file alias="helloqt_zh">helloqt_zh.qm</file> </qresource> </RCC> 声明

欢迎转载,请注明出处和作者,同时保留声明。
作者:LinTeX9527
出处:https://www.cnblogs.com/LinTeX9527/p/11015060.html
本博客的文章如无特殊说明,均为原创,转载请注明出处。如未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpdyff.html