Qt国际化

第一个Qt程序

新建项目

![在这里插入图片描述]( https://img-blog.csdnimg.cn/20201201124615274.png?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0NTE5NDg0,size_16,color_FFFFFF,t_70)

![在这里插入图片描述]( https://img-blog.csdnimg.cn/20201201124647636.png?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0NTE5NDg0,size_16,color_FFFFFF,t_70)

![在这里插入图片描述]( https://img-blog.csdnimg.cn/20201201124712630.png?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0NTE5NDg0,size_16,color_FFFFFF,t_70)
![在这里插入图片描述]( https://img-blog.csdnimg.cn/20201201125617923.png?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0NTE5NDg0,size_16,color_FFFFFF,t_70)

![在这里插入图片描述]( https://img-blog.csdnimg.cn/20201201125806562.png?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0NTE5NDg0,size_16,color_FFFFFF,t_70)

![在这里插入图片描述]( https://img-blog.csdnimg.cn/20201201124738433.png?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0NTE5NDg0,size_16,color_FFFFFF,t_70)

新建完项目之后可以左下角的绿色三角标志运行程序

基于qt语言家的语言切换

Assistant(Qt语言家),类似于MSDN最权威的速应用程序翻译和国际化的工具。Qt 使用单一的源码树
和单一的应用程序二进制包就可同时支持多个语言和书写系统,有助于软件走向国际化,普度众生。

具体步骤:

  1. 生成.ts文件,lupdate
  2. 翻译成制定语言,生成.qm文件,lrelease。
  3. 加载qm文件,load()
  4. install

效果呈现

中文:

![在这里插入图片描述]( https://img-blog.csdnimg.cn/2020120112500337.png?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0NTE5NDg0,size_16,color_FFFFFF,t_70)

英文:

![在这里插入图片描述]( https://img-blog.csdnimg.cn/20201201125010886.png?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0NTE5NDg0,size_16,color_FFFFFF,t_70)

UI设计

![在这里插入图片描述]( https://img-blog.csdnimg.cn/20201201125025301.png?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0NTE5NDg0,size_16,color_FFFFFF,t_70)

拖动Lable控件和日历控件到界面

![在这里插入图片描述]( https://img-blog.csdnimg.cn/20201201125034183.png?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0NTE5NDg0,size_16,color_FFFFFF,t_70)

双击Lable控件修改名字,设置字体以及字体大小

![在这里插入图片描述]( https://img-blog.csdnimg.cn/20201201125044423.png?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0NTE5NDg0,size_16,color_FFFFFF,t_70)

双击菜单栏添加一级菜单,二级菜单无法直接输入中文,在属性里面修改

多语言

step1:pro文件底部添加对翻译的支持
TRANSLATIONS = Translate_EN.ts \
Translate_CN.ts
step2:生成.ts文件

![在这里插入图片描述]( https://img-blog.csdnimg.cn/20201201125109206.png?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0NTE5NDg0,size_16,color_FFFFFF,t_70)

step3:使用qt语言家翻译ts文件

将Translate_CN.ts文件用Qt Linguist打开
汉语­(一般不用改,我们使用的是汉语,只需要改汉译英的)

![在这里插入图片描述]( https://img-blog.csdnimg.cn/20201201125120837.png?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0NTE5NDg0,size_16,color_FFFFFF,t_70)

![在这里插入图片描述]( https://img-blog.csdnimg.cn/20201201125131148.png?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0NTE5NDg0,size_16,color_FFFFFF,t_70)

Translate_EN.ts

汉语­>英语

![在这里插入图片描述]( https://img-blog.csdnimg.cn/20201201125151477.png?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0NTE5NDg0,size_16,color_FFFFFF,t_70)

step4:生成.qm

![在这里插入图片描述]( https://img-blog.csdnimg.cn/2020120112521350.png?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0NTE5NDg0,size_16,color_FFFFFF,t_70)

step5:加载qm文件
定义QTranslator对象(类内定义)
QTranslator *m_Translator;

分配对象(构造中分配)

m_Translator = new QTranslator(this);

设置菜单响应

![在这里插入图片描述]( https://img-blog.csdnimg.cn/20201201125240681.png?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0NTE5NDg0,size_16,color_FFFFFF,t_70)

信号与槽后面介绍,按步骤来就行

切换英文

m_Translator‐>load("Translate_EN.qm");

切换中文

m_Translator->load("Translate_CN.qm");
step6:安装qm文件
qApp‐>installTranslator(m_Translator);
问题:日历还没有修改
ui‐>calendarWidget‐>setLocale(QLocale::Chinese);	//设置日历英文
ui‐>calendarWidget‐>setLocale(QLocale::C);			//设置日历中文

注意事项:

![在这里插入图片描述]( https://img-blog.csdnimg.cn/20201201125331977.png?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0NTE5NDg0,size_16,color_FFFFFF,t_70)

如果加载不成功,说明路径错了,QtCreator生成的.qm文件是在.pro目录下,需要移至debug目录下,才能正确读取.qm文件,实现语言的切换

源代码以及示例程序github地址:

https://github.com/ADeRoy/Qt-controls/tree/master/language

posted @ 2020-12-01 12:59  DeRoy  阅读(576)  评论(0)    收藏  举报