QT开发之路 企业级开发系列文章,主要目标快速学习、完善、提升 相关技能 高效完成企业级项目开发 分享在企业中积累的实用技能和经验。
通过具体的编码过程、代码示例、步骤详解、核心内容和展示的方法解决遇到的实际问题。
阅读前声明 本系列文章属于付费内容 禁止转载、复制、抄袭 仅限付费读者阅读参考。
QT 多语言转换 ts、qm
1 运行环境
2 命令用法
3 软件界面
4 vs配置
5 .h代码
6 .cpp代码
7 其它
8 en.ts
9 xf.ts
10 zh.ts
11 其它文章
1 运行环境
VS2017+QT
2 命令用法
lupdate 更新翻译文件 生成 .ts 文件
用法:
lupdate ./ -ts language/QLinguist_en.ts language/QLinguist_zh.ts language/QLinguist_xf.ts
language/QLinguist_en.ts //language/目录 按照实际需求更改
lrelease 发布更新文件 生成 .qm 文件
用法:
lrelease language/QLinguist_en.ts language/QLinguist_zh.ts language/QLinguist_xf.ts
language/QLinguist_en.ts // language/ 目录 按照实际需求更改
3 软件界面
4 vs配置
项目-> 属性->生成事件->生成后事件->输入
lupdate ./ -ts language/QLinguist_en.ts language/QLinguist_zh.ts language/QLinguist_xf.ts
lrelease language/QLinguist_en.ts language/QLinguist_zh.ts language/QLinguist_xf.ts
5 .h代码
#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_QLinguist.h"
#include <QTranslator>
#include <QDebug>
#pragma execution_character_set("utf-8")
class QLinguist : public QMainWindow
{
Q_OBJECT
public:
QLinguist(QWidget *parent = nullptr);
~QLinguist();
private slots:
void on_ActionTriggered();
void on_ActionXf_Triggered();
void on_ActionEn_Triggered();
private:
Ui::QLinguistClass ui;
QString strFilePath = "";
QTranslator zh;
QTranslator xf;
QTranslator en;
};
6 .cpp代码
#include "QLinguist.h"
QLinguist::QLinguist(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
setWindowTitle("多语言切换Demo CSDN 双子座断点 V1.0 https://blog.csdn.net/qq_37529913?type=blog");
strFilePath = "C:/Users/dream/source/repos/QLinguist/QLinguist/language/";
zh.load(strFilePath + "QLinguist_zh.qm");
xf.load(strFilePath + "QLinguist_xf.qm");
en.load(strFilePath + "QLinguist_en.qm");
connect(ui.action, SIGNAL(triggered()), SLOT(on_ActionTriggered()));
connect(ui.action_2, SIGNAL(triggered()), SLOT(on_ActionXf_Triggered()));
connect(ui.action_3, SIGNAL(triggered()), SLOT(on_ActionEn_Triggered()));
}
QLinguist::~QLinguist()
{
}
void QLinguist::on_ActionTriggered()
{
qDebug() << ".....";
qApp->installTranslator(&zh);
ui.retranslateUi(this);
}
void QLinguist::on_ActionXf_Triggered()
{
qDebug() << ".....";
qApp->installTranslator(&xf);
ui.retranslateUi(this);
}
void QLinguist::on_ActionEn_Triggered()
{
qDebug() << ".....";
qApp->installTranslator(&en);
ui.retranslateUi(this);
}
7 其它
void QLinguist::on_ActionEn_Triggered()
{
qDebug() << ".....";
//qApp->installTranslator(&en);
//ui.retranslateUi(this);
//ui.lineEdit->setText(tr("图像路径为空!"));
}
8 en.ts
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="en_US">
<context>
<name>QLinguist</name>
<message>
<location filename="../QLinguist.cpp" line="51"/>
<source>图像路径为空!</source>
<translation type="unfinished">The image path is empty!</translation>
</message>
</context>
<context>
<name>QLinguistClass</name>
<message>
<location filename="../QLinguist.ui" line="14"/>
<location filename="../x64/Release/uic/ui_QLinguist.h" line="227"/>
<source>多语言切换Demo CSDN 双子座断点 V1.0 https://blog.csdn.net/qq_37529913?type=blog</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../QLinguist.ui" line="29"/>
<location filename="../x64/Debug/uic/ui_QLinguist.h" line="204"/>
<location filename="../x64/Release/uic/ui_QLinguist.h" line="231"/>
<source>图像路径:</source>
<translation type="unfinished">image path:</translation>
</message>
<message>
<location filename="../QLinguist.ui" line="62"/>
<location filename="../x64/Debug/uic/ui_QLinguist.h" line="205"/>
<location filename="../x64/Release/uic/ui_QLinguist.h" line="232"/>
<source>曝光:</source>
<translation type="unfinished">Exposure:</translation>
</message>
<message>
<location filename="../QLinguist.ui" line="95"/>
<location filename="../x64/Debug/uic/ui_QLinguist.h" line="206"/>
<location filename="../x64/Release/uic/ui_QLinguist.h" line="233"/>
<source>增益:</source>
<translation type="unfinished">gain:</translation>
</message>
<message>
<location filename="../QLinguist.ui" line="128"/>
<location filename="../x64/Debug/uic/ui_QLinguist.h" line="207"/>
<location filename="../x64/Release/uic/ui_QLinguist.h" line="234"/>
<source>区域:</source>
<translation type="unfinished">roi:</translation>
</message>
<message>
<location filename="../QLinguist.ui" line="161"/>
<location filename="../x64/Debug/uic/ui_QLinguist.h" line="208"/>
<location filename="../x64/Release/uic/ui_QLinguist.h" line="235"/>
<source>串口:</source>
<translation type="unfinished">com:</translation>
</message>
<message>
<location filename="../QLinguist.ui" line="236"/>
<location filename="../x64/Debug/uic/ui_QLinguist.h" line="209"/>
<location filename="../x64/Release/uic/ui_QLinguist.h" line="237"/>
<source>语言选择</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../QLinguist.ui" line="246"/>
<location filename="../x64/Debug/uic/ui_QLinguist.h" line="201"/>
<location filename="../x64/Release/uic/ui_QLinguist.h" line="228"/>
<source>简体中文</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../QLinguist.ui" line="251"/>
<location filename="../x64/Debug/uic/ui_QLinguist.h" line="202"/>
<location filename="../x64/Release/uic/ui_QLinguist.h" line="229"/>
<source>繁体中文</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../QLinguist.ui" line="256"/>
<location filename="../x64/Debug/uic/ui_QLinguist.h" line="203"/>
<location filename="../x64/Release/uic/ui_QLinguist.h" line="230"/>
<source>English</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../x64/Debug/uic/ui_QLinguist.h" line="200"/>
<source>QLinguist</source>
<translation type="unfinished"></translation>
</message>
</context>
</TS>
9 xf.ts
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1">
<context>
<name>QLinguist</name>
<message>
<location filename="../QLinguist.cpp" line="51"/>
<source>图像路径为空!</source>
<translation type="unfinished">圖像路徑為空!</translation>
</message>
</context>
<context>
<name>QLinguistClass</name>
<message>
<location filename="../QLinguist.ui" line="14"/>
<location filename="../x64/Release/uic/ui_QLinguist.h" line="227"/>
<source>多语言切换Demo CSDN 双子座断点 V1.0 https://blog.csdn.net/qq_37529913?type=blog</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../QLinguist.ui" line="29"/>
<location filename="../x64/Debug/uic/ui_QLinguist.h" line="204"/>
<location filename="../x64/Release/uic/ui_QLinguist.h" line="231"/>
<source>图像路径:</source>
<translation type="unfinished">圖像路徑:</translation>
</message>
<message>
<location filename="../QLinguist.ui" line="62"/>
<location filename="../x64/Debug/uic/ui_QLinguist.h" line="205"/>
<location filename="../x64/Release/uic/ui_QLinguist.h" line="232"/>
<source>曝光:</source>
<translation type="unfinished">曝光:</translation>
</message>
<message>
<location filename="../QLinguist.ui" line="95"/>
<location filename="../x64/Debug/uic/ui_QLinguist.h" line="206"/>
<location filename="../x64/Release/uic/ui_QLinguist.h" line="233"/>
<source>增益:</source>
<translation type="unfinished">增益:</translation>
</message>
<message>
<location filename="../QLinguist.ui" line="128"/>
<location filename="../x64/Debug/uic/ui_QLinguist.h" line="207"/>
<location filename="../x64/Release/uic/ui_QLinguist.h" line="234"/>
<source>区域:</source>
<translation type="unfinished">區域:</translation>
</message>
<message>
<location filename="../QLinguist.ui" line="161"/>
<location filename="../x64/Debug/uic/ui_QLinguist.h" line="208"/>
<location filename="../x64/Release/uic/ui_QLinguist.h" line="235"/>
<source>串口:</source>
<translation type="unfinished">串口:</translation>
</message>
<message>
<location filename="../QLinguist.ui" line="236"/>
<location filename="../x64/Debug/uic/ui_QLinguist.h" line="209"/>
<location filename="../x64/Release/uic/ui_QLinguist.h" line="237"/>
<source>语言选择</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../QLinguist.ui" line="246"/>
<location filename="../x64/Debug/uic/ui_QLinguist.h" line="201"/>
<location filename="../x64/Release/uic/ui_QLinguist.h" line="228"/>
<source>简体中文</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../QLinguist.ui" line="251"/>
<location filename="../x64/Debug/uic/ui_QLinguist.h" line="202"/>
<location filename="../x64/Release/uic/ui_QLinguist.h" line="229"/>
<source>繁体中文</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../QLinguist.ui" line="256"/>
<location filename="../x64/Debug/uic/ui_QLinguist.h" line="203"/>
<location filename="../x64/Release/uic/ui_QLinguist.h" line="230"/>
<source>English</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../x64/Debug/uic/ui_QLinguist.h" line="200"/>
<source>QLinguist</source>
<translation type="unfinished"></translation>
</message>
</context>
</TS>
10 zh.ts
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="zh_CN">
<context>
<name>QLinguist</name>
<message>
<location filename="../QLinguist.cpp" line="49"/>
<source>图像路径为空!</source>
<translation type="unfinished">图像路径为空!</translation>
</message>
</context>
<context>
<name>QLinguistClass</name>
<message>
<location filename="../QLinguist.ui" line="14"/>
<source>多语言切换Demo CSDN 双子座断点 V1.0 https://blog.csdn.net/qq_37529913?type=blog</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../QLinguist.ui" line="29"/>
<source>图像路径:</source>
<translation type="unfinished">图像路径:</translation>
</message>
<message>
<location filename="../QLinguist.ui" line="62"/>
<source>曝光:</source>
<translation type="unfinished">曝光:</translation>
</message>
<message>
<location filename="../QLinguist.ui" line="95"/>
<source>增益:</source>
<translation type="unfinished">增益:</translation>
</message>
<message>
<location filename="../QLinguist.ui" line="128"/>
<source>区域:</source>
<translation type="unfinished">区域:</translation>
</message>
<message>
<location filename="../QLinguist.ui" line="161"/>
<source>串口:</source>
<translation type="unfinished">串口:</translation>
</message>
<message>
<location filename="../QLinguist.ui" line="236"/>
<source>语言选择</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../QLinguist.ui" line="246"/>
<source>简体中文</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../QLinguist.ui" line="251"/>
<source>繁体中文</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../QLinguist.ui" line="256"/>
<source>English</source>
<translation type="unfinished"></translation>
</message>
</context>
</TS>