一、描述
1、QLocale类用于在不同语言的数字及其字符串表示形式之间进行转换。
2、QLocale在其构造函数中使用语言/国家对进行初始化,并提供与QString中类似的数字转字符串和字符串转数字的函数。
3、QLocale支持默认区域设置的概念,默认区域设置由应用程序启动时的系统区域设置确定。设置默认区域设置具有以下效果:
如果QLocale对象是用默认构造函数构造的,它将使用默认区域设置。
QString::toInt()、QString::toDouble()等函数根据默认区域设置解释字符串。
当格式字符串中的位置说明符包含“L”(例如“%L1”)时,QString::arg()使用默认区域设置来格式化数字。
如:
QLocale::setDefault(QLocale::C);
d = QString("1234,56").toDouble(&ok); // ok == false
d = QString("1234.56").toDouble(&ok); // ok == true, d == 1234.56
QLocale::setDefault(QLocale::German);
d = QString("1234,56").toDouble(&ok); // ok == true, d == 1234.56
d = QString("1234.56").toDouble(&ok); // ok == true, d == 1234.56
QLocale::setDefault(QLocale(QLocale::English, QLocale::UnitedStates));
str = QString("%1 %L2 %L3").arg(12345).arg(12345).arg(12345, 0, 16); // str == "12345 12,345 3039"
二、实用的类型成员
1、QLocale::Country。国家和地区。太长不列了。
2、QLocale::Language。语言。太长不列了。
3、QLocale::FormatType。此枚举描述将QDate和QTime对象转换为字符串时可以使用的格式类型。
LongFormat:长文本格式。
ShortFormat:短文本格式。
NarrowFormat:极短文本格式
QLocale locale = QLocale(QLocale::Chinese,QLocale::China);
qDebug()<<locale.monthName(1,QLocale::LongFormat);
qDebug()<<locale.monthName(1,QLocale::ShortFormat);
qDebug()<<locale.monthName(1,QLocale::NarrowFormat);
locale = QLocale(QLocale::English,QLocale::UnitedStates);
qDebug()<<locale.monthName(1,QLocale::LongFormat);
qDebug()<<locale.monthName(1,QLocale::ShortFormat);
qDebug()<<locale.monthName(1,QLocale::NarrowFormat);
4、QLocale::MeasurementSystem:测量单位。
MetricSystem。公制单位。例如米、厘米和毫米。 另几个几乎用不到不用管。
5、QLocale::CurrencySymbolFormat:货币格式,见下面第7个函数。
6、QLocale::DataSizeFormat:数据大小格式。
DataSizeIecFormat:使用基本1024和IEC前缀的格式:KiB、MiB、GiB等
DataSizeTraditionalFormat:使用基本1024和SI前缀的格式:kB、MB、GB等
DataSizeSIFormat:使用基数1000和SI前缀的格式:kB、MB、GB等
三、实用的成员函数
1、QString toCurrencyString(qlonglong value, const QString &symbol = QString())
返回货币符号形式,参数1有多个重载版本。
QLocale locale = QLocale(QLocale::Chinese,QLocale::China);
qDebug()<<locale.toCurrencyString(22);
locale = QLocale(QLocale::English,QLocale::UnitedStates);
qDebug()<<locale.toCurrencyString(22);
如果指定了参数2,则使用参数2替换货币符号:
-
QLocale locale = QLocale(QLocale::Chinese,QLocale::China); qDebug()<<locale.toCurrencyString(22,"@@"); locale = QLocale(QLocale::English,QLocale::UnitedStates); qDebug()<<locale.toCurrencyString(22,"@@");
2、QString amText() / QString QLocale::pmText()
返回使用12小时制时间的上午/下午本地化名称
QLocale locale = QLocale(QLocale::Chinese,QLocale::China);
qDebug()<<locale.amText();
qDebug()<<locale.pmText();
locale = QLocale(QLocale::English,QLocale::UnitedStates);
qDebug()<<locale.amText();
qDebug()<<locale.pmText();
3、QString bcp47Name()
返回语言的bcp47字段。
QLocale locale = QLocale(QLocale::Chinese,QLocale::China);
qDebug()<<locale.bcp47Name();
locale = QLocale(QLocale::English,QLocale::UnitedStates);
qDebug()<<locale.bcp47Name();
4、QLocale::Country country()
返回国家 /地区。
5、[static] QString countryToString(QLocale::Country country)
返回国家名称。
6、QString createSeparatedList(const QStringList &list)
返回一个字符串,该字符串表示给定字符串列表与区域设置定义的分隔符的联接。
QStringList list;
list<<"测试1"<<"测试2"<<"测试3"<<"测试4";
QLocale locale = QLocale(QLocale::Chinese,QLocale::China);
qDebug()<<locale.createSeparatedList(list);
locale = QLocale(QLocale::English,QLocale::UnitedStates);
qDebug()<<locale.createSeparatedList(list);
7、QString currencySymbol(QLocale::CurrencySymbolFormat format = CurrencySymbol)
返回本地货币格式。
QLocale locale = QLocale(QLocale::Chinese,QLocale::China);
qDebug()<<locale.currencySymbol(QLocale::CurrencyIsoCode);
qDebug()<<locale.currencySymbol(QLocale::CurrencySymbol);
qDebug()<<locale.currencySymbol(QLocale::CurrencyDisplayName);
locale = QLocale(QLocale::English,QLocale::UnitedStates);
qDebug()<<locale.currencySymbol(QLocale::CurrencyIsoCode);
qDebug()<<locale.currencySymbol(QLocale::CurrencySymbol);
qDebug()<<locale.currencySymbol(QLocale::CurrencyDisplayName);
8、QString dateFormat(QLocale::FormatType format = LongFormat)
返回本地的日期格式。只有长短两种格式。ShortFormat和NarrowFormat都是短日期格式。
QLocale locale = QLocale(QLocale::Chinese,QLocale::China);
qDebug()<<locale.dateFormat(QLocale::LongFormat);
qDebug()<<locale.dateFormat(QLocale::ShortFormat);
qDebug()<<locale.dateFormat(QLocale::NarrowFormat);
locale = QLocale(QLocale::English,QLocale::UnitedStates);
qDebug()<<locale.dateFormat(QLocale::LongFormat);
qDebug()<<locale.dateFormat(QLocale::ShortFormat);
qDebug()<<locale.dateFormat(QLocale::NarrowFormat);
9、QString dateTimeFormat(QLocale::FormatType format = LongFormat)
返回本地的时间日期格式,同上也是长短两种。
QLocale locale = QLocale(QLocale::Chinese,QLocale::China);
qDebug()<<locale.dateTimeFormat(QLocale::LongFormat);
qDebug()<<locale.dateTimeFormat(QLocale::ShortFormat);
qDebug()<<locale.dateTimeFormat(QLocale::NarrowFormat);
locale = QLocale(QLocale::English,QLocale::UnitedStates);
qDebug()<<locale.dateTimeFormat(QLocale::LongFormat);
qDebug()<<locale.dateTimeFormat(QLocale::ShortFormat);
qDebug()<<locale.dateTimeFormat(QLocale::NarrowFormat);
10、 QString timeFormat(QLocale::FormatType format = LongFormat)
返回时间格式。同上。
11、QString dayName(int day, QLocale::FormatType type = LongFormat)
返回一周中的某天在本地的表现形式。
QLocale locale = QLocale(QLocale::Chinese,QLocale::China);
qDebug()<<locale.dayName(4,QLocale::LongFormat);
qDebug()<<locale.dayName(4,QLocale::ShortFormat);
qDebug()<<locale.dayName(4,QLocale::NarrowFormat);
locale = QLocale(QLocale::English,QLocale::UnitedStates);
qDebug()<<locale.dayName(4,QLocale::LongFormat);
qDebug()<<locale.dayName(4,QLocale::ShortFormat);
qDebug()<<locale.dayName(4,QLocale::NarrowFormat);
12、Qt::DayOfWeek firstDayOfWeek()
返回当地一周开头是哪天。
Qt::Monday:1
Qt::Tuesday:2
Qt::Wednesday:3
Qt::Thursday:4
Qt::Friday:5
Qt::Saturday:6
Qt::Sunday:7
13、QString formattedDataSize(qint64 bytes, int precision = 2, QLocale::DataSizeFormats format = DataSizeIecFormat)
数据大小格式转换,参数1是字节。
QLocale locale = QLocale(QLocale::Chinese,QLocale::China);
qDebug()<<locale.formattedDataSize(987654327654,2,QLocale::DataSizeIecFormat);
qDebug()<<locale.formattedDataSize(987654327654,2,QLocale::DataSizeTraditionalFormat);
qDebug()<<locale.formattedDataSize(987654327654,2,QLocale::DataSizeSIFormat);
locale = QLocale(QLocale::English,QLocale::UnitedStates);
qDebug()<<locale.formattedDataSize(987654327654,2,QLocale::DataSizeIecFormat);
qDebug()<<locale.formattedDataSize(987654327654,2,QLocale::DataSizeTraditionalFormat);
qDebug()<<locale.formattedDataSize(987654327654,2,QLocale::DataSizeSIFormat);
14、QLocale::Language language()
返回本地语言。
15、[static] QString languageToString(QLocale::Language language)
语言转字符串。
qDebug()<<QLocale::languageToString(QLocale::Chinese);
16、QString monthName(int month, QLocale::FormatType type = LongFormat)
返回本地月份名称。
QLocale locale = QLocale(QLocale::Chinese,QLocale::China);
qDebug()<<locale.monthName(4,QLocale::LongFormat);
qDebug()<<locale.monthName(4,QLocale::ShortFormat);
qDebug()<<locale.monthName(4,QLocale::NarrowFormat);
locale = QLocale(QLocale::English,QLocale::UnitedStates);
qDebug()<<locale.monthName(4,QLocale::LongFormat);
qDebug()<<locale.monthName(4,QLocale::ShortFormat);
qDebug()<<locale.monthName(4,QLocale::NarrowFormat);
17、QString name()
返回地区和语言简称。
QLocale locale = QLocale(QLocale::Chinese,QLocale::China);
qDebug()<<locale.name();
locale = QLocale(QLocale::English,QLocale::UnitedStates);
qDebug()<<locale.name();
18、QString nativeCountryName() / QString nativeLanguageName()
返回国名和语言名。
QLocale locale = QLocale(QLocale::Chinese,QLocale::China);
qDebug()<<locale.nativeCountryName()<<locale.nativeLanguageName();
locale = QLocale(QLocale::English,QLocale::UnitedStates);
qDebug()<<locale.nativeCountryName()<<locale.nativeLanguageName();
19、[static] void setDefault(const QLocale &locale)
设置应用程序的默认QLocale。
20、[static] QLocale system()
返回应用程序的QLocale。
21、Qt::LayoutDirection textDirection()
返回本地语言的文本布局方向。
22、QDate toDate(const QString &string, QLocale::FormatType format = LongFormat)
字符串转日期。
23、QDateTime toDateTime(const QString &string, QLocale::FormatType format = LongFormat)
字符串转日期时间。
24、QTime toTime(const QString &string, const QString &format)
字符串转时间。
25、toString()
时间、日期、日期时间转字符串。有长短两种格式。
QLocale locale = QLocale(QLocale::Chinese,QLocale::China);
qDebug()<<locale.toString(QDateTime::currentDateTime(),QLocale::LongFormat);
qDebug()<<locale.toString(QDateTime::currentDateTime(),QLocale::ShortFormat);
qDebug()<<locale.toString(QDateTime::currentDateTime(),QLocale::NarrowFormat);
locale = QLocale(QLocale::English,QLocale::UnitedStates);
qDebug()<<locale.toString(QDateTime::currentDateTime(),QLocale::LongFormat);
qDebug()<<locale.toString(QDateTime::currentDateTime(),QLocale::ShortFormat);
qDebug()<<locale.toString(QDateTime::currentDateTime(),QLocale::NarrowFormat);
26、QList<Qt::DayOfWeek> weekdays()
返回本地工作日的列表。