更改Qt中的区域设置 [英] Changing locale in Qt

查看:1061
本文介绍了更改Qt中的区域设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图改变locale使用QLocale和setDefault函数,但似乎它不工作。这里是使用C本地化库和QLocale更改locale的示例。对于C本地化库似乎它的工作,但对于QLocale似乎setDefault函数调用被忽略。

I tried to change locale using QLocale and setDefault function but it seems that it doesn't work. Here is example of changing locale using C localization library and QLocale. For C localization library it seems that it works, but for QLocale it seems that setDefault function call is ignored.

QLocale curLocale(QLocale("pl_PL"));
QLocale::setDefault(curLocale);
QDate date = QDate::currentDate();
QString dateString = date.toString();
// prints "Fri Nov 9 2012" but that was not expected
std::cout << dateString.toStdString() << std::endl;
// prints "en_US", but shouldn't it be "pl_PL"?
std::cout << QLocale::system().name().toStdString() << std::endl;

std::setlocale(LC_ALL, "pl_PL");
// prints "pl_PL"
std::cout << std::setlocale(LC_ALL, 0) << std::endl;
std::time_t currentTime;
std::time(&currentTime);
std::tm* timeinfo = std::localtime(&currentTime);
char charArray[40];
std::strftime(charArray, 40, "%a %b %d %Y", timeinfo);
// prints "pi lis 09 2012" and that's cool
std::cout << charArray << std::endl;

如何在Qt中更改正确的区域设置,以便影响程序?

How to change properly locale in Qt so it affects the program?

推荐答案

QLocale :: setDefault()不更改系统区域设置。它改变了使用默认构造函数创建的 QLocale 对象。

QLocale::setDefault() does not change the system locale. It changes what QLocale object created with default constructor is.

想象一下,系统区域设置只能通过系统控制面板/偏好。

Supposedly, system locale can only be changed via system control panel/preferences by the user. If you want to format something that is not in the system locale, you need to specifically do that with a locale object.

此代码:

QLocale curLocale(QLocale("pl_PL"));
QLocale::setDefault(curLocale);
QDate date = QDate::currentDate();
QString dateString = QLocale().toString(date);
qDebug() << dateString;
qDebug() << QLocale().name();

打印此:

"piątek, 9 listopada 2012" 
"pl_PL" 

这篇关于更改Qt中的区域设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆