QT 多语言转换 ts、qm

news2024/10/29 5:18:57

        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>

11 其它文章

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2226985.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【论文笔记】Perceiver: General Perception with Iterative Attention

&#x1f34e;个人主页&#xff1a;小嗷犬的个人主页 &#x1f34a;个人网站&#xff1a;小嗷犬的技术小站 &#x1f96d;个人信条&#xff1a;为天地立心&#xff0c;为生民立命&#xff0c;为往圣继绝学&#xff0c;为万世开太平。 基本信息 标题: Perceiver: General Perce…

spring-第十一章 注解开发

spring 文章目录 spring前言1.注解回顾1.1原理1.2springIOC注解扫描原理1.2.1解释1.2.2案例 2.声明bean的注解补充&#xff1a;Bean注解&#xff0c;管理三方包对象 3.spring注解的使用3.1加入aop依赖3.2配置文件中添加context命名空间3.3配置文件中指定要扫描的包3.4在Bean上使…

【CSS in Depth 2 精译_055】8.3 伪类 :is() 和 :where() 的正确打开方式

当前内容所在位置&#xff08;可进入专栏查看其他译好的章节内容&#xff09; 【第三部分 现代 CSS 代码组织】 ✔️【第八章 层叠图层及其嵌套】 ✔️ 8.1 用 layer 图层来操控层叠规则&#xff08;上篇&#xff09; 8.1.1 图层的定义&#xff08;上篇&#xff09;8.1.2 图层的…

20241028给荣品RD-RK3588-AHD开发板刷Rockchip原厂的Buildroot之后确认AP6275P的蓝牙BLE功能

20241028给荣品RD-RK3588-AHD开发板刷Rockchip原厂的Buildroot之后确认AP6275P的蓝牙BLE功能 2024/10/28 16:56 手机&#xff1a;realme的GT NEO5【只要手机支持蓝牙BLE即可】 APK&#xff1a;在【你用的手机】应用市场下载 BLE调试助手并安装之后别用。 缘起&#xff1a;为了简…

大模型,多模态大模型面试问题记录【时序,Qformer,卷积,感受野,ControlNet,IP-adapter】

大模型&#xff0c;多模态大模型面试问题记录24/10/27 问题一&#xff1a;视频生成例如Sora或者视频理解internvl2模型怎么提取时序上的特征。问题二&#xff1a;Qformer介绍训练阶段一训练阶段二 问题三&#xff1a;卷积维度计算公式&#xff0c;感受野1. 卷积层输出高度和宽度…

Spring Cloud --- Sentinel 授权规则

授权规则概述 在某些场景下&#xff0c;需要根据调用接口的来源判断是否允许执行本次请求。此时就可以使用 Sentinel 提供的授权规则来实现&#xff0c;Sentinel 的授权规则能够根据请求的来源判断是否允许本次请求通过。 在 Sentinel 的授权规则中&#xff0c;提供了 白名单…

自修室预约系统|基于java和小程序的自修室预约系统设计与实现(源码+数据库+文档)

自修室预约系统 目录 基于java和小程序的自修室预约系统设计与实现 一、前言 二、系统设计 三、系统功能设计 四、数据库设计 五、核心代码 六、论文参考 七、最新计算机毕设选题推荐 八、源码获取&#xff1a; 博主介绍&#xff1a;✌️大厂码农|毕设布道师&#x…

asp.net core 入口 验证token,但有的接口要跳过验证

asp.net core 入口 验证token,但有的接口要跳过验证 在ASP.NET Core中&#xff0c;你可以使用中间件来验证token&#xff0c;并为特定的接口创建一个属性来标记是否跳过验证。以下是一个简化的例子&#xff1a; 创建一个自定义属性来标记是否跳过验证&#xff1a; public clas…

【华为HCIP实战课程二十五】中间到中间系统协议IS-IS配置实战续系统ID区域ID,网络工程师

上章简单讲解了ISIS基本配置,本章继续详细讲解ISIS配置及实施 IS-IS配置拓扑 1、R1进行配置IS-IS [R1]display current-configuration configuration isis isis 1 network-entity 49.0124.1111.1111.1111.00 //配置NET地址,由三部分组成,区域ID、系统ID和固定的SEL 00 i…

Kafka集群数据迁移方案

概述 MirrorMaker2&#xff08;后文简称 MM2&#xff09;在 2019 年 12 月随 Kafka 2.4.0 一起推出。顾名思义&#xff0c;是为了解决 Kafka 集群之间数据复制和数据同步的问题而诞生的 Kafka 官方的数据复制工具。在实际生产中&#xff0c;经常被用来实现 Kafka 数据的备份&a…

鼠标增强工具 MousePlus v5.3.9.0 中文绿色版

MousePlus 是一款功能强大的鼠标增强工具&#xff0c;它可以帮助用户提高鼠标操作效率和精准度。该软件可以自定义鼠标的各种功能和行为&#xff0c;让用户根据自己的习惯和需求来调整鼠标的表现。 详细功能 自定义鼠标按钮功能&#xff1a;可以为鼠标的各个按钮设置不同的功能…

【大模型系列】Mini-InternVL(2024.10)

Paper&#xff1a;https://arxiv.org/pdf/2410.16261Github&#xff1a;https://github.com/OpenGVLab/InternVL/tree/main/internvl_chat/shell/mini_internvlAuthor&#xff1a;Zhangwei Gao et al. 上海人工智能实验室 文章目录 0 总结(省流版)1 模型结构1.1 InternViT-300M…

探讨Facebook的AI研究:未来社交平台的技术前瞻

在数字时代&#xff0c;社交媒体已成为人们日常生活的重要组成部分。作为全球最大的社交网络之一&#xff0c;Facebook不断致力于人工智能&#xff08;AI&#xff09;的研究与应用&#xff0c;以提升用户体验、增强平台功能并推动技术创新。本文将探讨Facebook在AI领域的研究方…

一键导入Excel到阿里云PolarDB-MySQL版

今天&#xff0c;我将分享如何一键导入Excel到阿里云PolarDB-MySQL版数据库。 准备数据 这里&#xff0c;我们准备了一张excel表格如下&#xff1a; 连接到阿里云PolarDB 打开的卢导表&#xff0c;点击新建连接-选择阿里云PolarDB-MySQL版。如果你还没有这个工具&#xff0c;…

[NSSCTF 2nd]php签到 详细题解

知识点: linux文件后缀名绕过 表单文件上传 pathinfo 函数 file_put_contents()函数 命令执行 代码审计: <?phpfunction waf($filename){$black_list array("ph", "htaccess", "ini");$ext pathinfo($filename, PATHINFO_EXTENSION…

[0260].第25节:锁的不同角度分类

MySQL学习大纲 我的数据库学习大纲 从不同维度对锁的分类&#xff1a; 1.对数据操作的类型划分:读锁和写锁 1.1.读锁 与 写锁概述&#xff1a; 1.对于数据库中并发事务的读-读情况并不会引起什么问题。对于写-写、读-写或写-读这些情况可能会引起一些问题&#xff0c;需要使用…

云原生后端开发教程

云原生后端开发教程 引言 随着云计算的普及&#xff0c;云原生架构逐渐成为现代软件开发的主流。云原生不仅仅是将应用部署到云上&#xff0c;而是一种构建和运行应用的方式&#xff0c;充分利用云计算的弹性和灵活性。本文将深入探讨云原生后端开发的核心概念、工具和实践&a…

Docker 常用命令全解析:提升对雷池社区版的使用经验

Docker 常用命令解析 Docker 是一个开源的容器化平台&#xff0c;允许开发者将应用及其依赖打包到一个可移植的容器中。以下是一些常用的 Docker 命令及其解析&#xff0c;帮助您更好地使用 Docker。 1. Docker 基础命令 查看 Docker 版本 docker --version查看 Docker 运行…

常见的java开发面试题

目录 1.SpringBoot 打成的jar包和普通的jar包有什么区别&#xff1f; 如何让SpringBoot打的jar包可依赖&#xff1f; 2. http 和 https 的区别&#xff1f; 一、安全性 二、连接方式 三、性能影响 四、应用场景 五、总结&#xff1a; 3. GC是什么&#xff0c;为什么要使用…

信息安全入门——网络安全控制

目录 前言信息安全入门&#xff1a;网络安全控制基础1. 用户识别技术&#xff1a;确认你是谁2. 访问控制技术&#xff1a;定义你能做什么3. 访问控制列表&#xff08;ACL&#xff09;&#xff1a;精细的权限管理4. 漏洞控制&#xff1a;防范未然5. 入侵检测系统&#xff08;IDS…