Chinaunix首页 | 论坛 | 博客
  • 博客访问: 98557
  • 博文数量: 33
  • 博客积分: 731
  • 博客等级: 上士
  • 技术积分: 757
  • 用 户 组: 普通用户
  • 注册时间: 2012-09-02 22:51
文章存档

2018年(1)

2017年(2)

2013年(1)

2012年(29)

我的朋友

分类: AIX

2018-01-18 15:15:39

Windows平台与Mac平台下qt国际化有点区别,cpp代码中含有tr字符则在
*name.pro 下增加
 TRANSLATIONS += Resource/multilanguage_zh.ts \
 Resource/multilanguage_en.ts
执行lupdate会生成一个multilanguage_zh.ts文件,linguist打开这个ts文件,进行翻译,生成qm文件,回到工程添加一个qrc资源文件,资源文件加载qm文件。main.cpp代码中加入:
 
QTranslator qtTranslator;
 qtTranslator.load(":/multilanguage_zh.qm");
 a.installTranslator(&qtTranslator);
编译即可显示中文了。其它语言同理
文件代码:
.pro

#-------------------------------------------------
#
# Project created by QtCreator 2018-01-17T09:36:03
#
#-------------------------------------------------

QT += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = MultiLanguage
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0


SOURCES += \
 multilanguage.cpp \
 main.cpp

HEADERS += \
 multilanguage.h

FORMS += \
 multilanguage.ui
TRANSLATIONS += Resource/multilanguage_zh.ts \
 Resource/multilanguage_en.ts

RESOURCES += \
 translator.qrc

 
 
main.cpp
 

#include "multilanguage.h"
#include 
#include
#include

int main(int argc, char *argv[])
{

 QApplication a(argc, argv);
 QTranslator qtTranslator;
 qtTranslator.load(":/multilanguage_zh.qm");
 a.installTranslator(&qtTranslator);
 MultiLanguage w;
 w.show();

 return a.exec();
}

功能代码cpp
 
切记Qrc要加入qm文件,以及加载的时候加上":/qm文件名" 
                                   
阅读(753) | 评论(0) | 转发(0) |
0

上一篇:Qt安装之后出现qmake命令无法找到

下一篇:没有了

给主人留下些什么吧!~~