现在的位置: 首页 > 综合 > 正文

QT多语言支持+交叉编译

2017年10月28日 ⁄ 综合 ⁄ 共 1698字 ⁄ 字号 评论关闭

新手上路,第一篇:感觉还是需要记录一下自己学过的东西好一点

普通的多语言支持不多说了,网上有很多例子,下面一个就不错

http://www.cnblogs.com/chuncn/archive/2012/06/04/2534898.html

不过上面例子可能即使点击更新语言,页面并不会更新,因为在最后少了更新操作

this->ui->retranslateUi(this);

而且在你点进其他页面,翻译又没了。

如果想一直都有翻译,应该在main里边进行一次总的设置,如下:

#include "stdafx.h"
#include <QtGui/QApplication>
#include <QtGui/QtGui>
#include "DialogLogin.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QTextCodec::setCodecForLocale(QTextCodec::codecForLocale());

// 安装QT运行库翻译器
QTranslator translatorQT;
{
QStringList environment = QProcess::systemEnvironment();
QString str;
bool bFinded = false;

foreach(str, environment)
{
if(str.startsWith("QTDIR="))
{
bFinded = true;
break;
}
}

if(bFinded)
{
str = str.mid(6);
bFinded = translatorQT.load("qt_" + QLocale::system().name(),str.append("/translations/"));

if(bFinded)
app.installTranslator(&translatorQT);
else
qDebug() <<QObject::tr("Can't find the translation file for Chinese!");
}
else
{
qDebug() << QObject::tr("Please set the environment variable QTDIR");
}
}

// 安装程序自身翻译器
QTranslator translatorApp;
{
QString strLanguageDir = QCoreApplication::applicationDirPath();
strLanguageDir.append("/Language/");

QString strFilePath = QApplication::applicationFilePath();
QString strFileName = strFilePath.right(strFilePath.size() - strFilePath.lastIndexOf('/') - 1);

strFileName = strFileName.left(strFileName.indexOf('.'));
strFileName.append('_');
strFileName.append(QLocale::system().name());
bool bFinded = translatorApp.load(strFileName,strLanguageDir);

if(bFinded)
app.installTranslator(&translatorApp);
else
{
qDebug() << QObject::tr("Can't Find The Translation's File For Chinese!");
}
}

CDialogLogin dlg;

return dlg.exec();
}

如果是交叉编译的话,可能会遇到没有.pro文件,这时候如果直接去点QTcreator的 update translations也没用,
它不知道该把目标文件放哪,这时候弄出个.pro文件就行了。在你工程目录下运行qmake -project,

就可以得到对应工程的.pro,然后下面工作跟linux环境下一样了

抱歉!评论已关闭.