*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文件名"