介绍Qt国际化的文章不少,但是真正详细介绍的不多。鄙人专门学习了一下Qt的国际化,在此做一点分享。欢迎大家拍砖。
QPushButton *button = new QPushButton(this);
button->setText(tr("国际化"));上面是将按钮的名称进行国际化,上面也是 tr() 函数的最简单的用法。当然,此种用法还是有其局限性。只有这段代码在类函数里面,并且所属的类继承于 QObject 的时候,才可以使用 tr() 。QPushButton *button = new QPushButton(this);
button->setText(QObject::tr("国际化"));class MyClass
{
Q_DECLARE_TR_FUNCTIONS(MyClass)
public:
MyClass();
...
};QString FriendlyConversation::greeting(int type)
{
static const char *greeting_strings[] = {
QT_TR_NOOP("Hello"),
QT_TR_NOOP("Goodbye")
};
return tr(greeting_strings[type]);
}或者在类内而不在类函数里,代码如下所示:class MyClass
{
Q_DECLARE_TR_FUNCTIONS(MyClass)
static const char * const ids[] = {
//% "This is the first text."
QT_TR_NOOP("qtn_1st_text"),
//% "This is the second text."
QT_TR_NOOP("qtn_2nd_text"),
0
};
public:
MyClass();
void addLabels();
...
};
void MyClass::addLabels()
{
for (int i = 0; ids[i]; ++i)
new QLabel(tr(ids[i]), this);
} QString text() const { return m_message.sourceText(); }改为: QString text() const
{
QString result = m_message.sourceText();
return QString::fromLocal8Bit(result.toLatin1());
}这样,现在我们的显示就变成了如下图所示。
void SourceCodeView::showSourceCode(const QString &absFileName, const int lineNum)
{
……
fileText = QString::fromLatin1(file.readAll());
……
}
改为:
void SourceCodeView::showSourceCode(const QString &absFileName, const int lineNum)
{
……
fileText = QString::fromLocal8Bit(file.readAll());
……
}更改完代码后,我们的程序就变成了如下所示。原文:http://blog.csdn.net/xiao69/article/details/19371699