实用QLocale

一、描述

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对象转换为字符串时可以使用的格式类型。

  • QLocale::LongFormat:长文本格式。
  • QLocale::ShortFormat:短文本格式。
  • QLocale::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:测量单位。

  • QLocale::MetricSystem。公制单位。例如米、厘米和毫米。 另几个几乎用不到不用管。

5、QLocale::CurrencySymbolFormat:货币格式,见下面第7个函数。

6、QLocale::DataSizeFormat:数据大小格式。

扫描二维码关注公众号,回复: 13129980 查看本文章
  • QLocale::DataSizeIecFormat:使用基本1024和IEC前缀的格式:KiB、MiB、GiB等
  • QLocale::DataSizeTraditionalFormat:使用基本1024和SI前缀的格式:kB、MB、GB等
  • QLocale::DataSizeSIFormat:使用基数1000和SI前缀的格式:kB、MB、GB等

三、实用的成员函数

1、QString QLocale::toCurrencyString(qlonglong value, const QString &symbol = QString()) const。返回货币符号形式,参数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 QLocale::amText() const、QString QLocale::pmText() const。返回使用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 QLocale::bcp47Name() const。返回语言的bcp47字段。

    QLocale locale = QLocale(QLocale::Chinese,QLocale::China);
    qDebug()<<locale.bcp47Name();

    locale = QLocale(QLocale::English,QLocale::UnitedStates);
    qDebug()<<locale.bcp47Name();

4、QLocale::Country QLocale::country() const。返回国家 /地区。

5、[static] QString QLocale::countryToString(QLocale::Country country)。返回国家名称。

    QLocale locale = QLocale(QLocale::Chinese,QLocale::China);
    qDebug()<<QLocale::countryToString(QLocale::China);

    locale = QLocale(QLocale::English,QLocale::UnitedStates);
    qDebug()<<QLocale::countryToString(QLocale::UnitedStates);

6、QString QLocale::createSeparatedList(const QStringList &list) const。返回一个字符串,该字符串表示给定字符串列表与区域设置定义的分隔符的联接。

    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 QLocale::currencySymbol(QLocale::CurrencySymbolFormat format = CurrencySymbol) const。返回本地货币格式。

    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 QLocale::dateFormat(QLocale::FormatType format = LongFormat) const。返回本地的日期格式。只有长短两种格式。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 QLocale::dateTimeFormat(QLocale::FormatType format = LongFormat) const。返回本地的时间日期格式,同上也是长短两种。

    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 QLocale::timeFormat(QLocale::FormatType format = LongFormat) const。返回时间格式。同上。

11、QString QLocale::dayName(int day, QLocale::FormatType type = LongFormat) const。返回一周中的某天在本地的表现形式。 

    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 QLocale::firstDayOfWeek() const。返回当地一周开头是哪天。

  • Qt::Monday:1
  • Qt::Tuesday:2
  • Qt::Wednesday:3
  • Qt::Thursday:4
  • Qt::Friday:5
  • Qt::Saturday:6
  • Qt::Sunday:7

13、QString QLocale::formattedDataSize(qint64 bytes, int precision = 2, QLocale::DataSizeFormats format = DataSizeIecFormat) const。数据大小格式转换,参数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 QLocale::language() const 。返回本地语言。

15、[static] QString QLocale::languageToString(QLocale::Language language)。语言转字符串。

    qDebug()<<QLocale::languageToString(QLocale::Chinese);

16、QString QLocale::monthName(int month, QLocale::FormatType type = LongFormat) const。返回本地月份名称。

    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 QLocale::name() const。返回地区和语言简称。

    QLocale locale = QLocale(QLocale::Chinese,QLocale::China);
    qDebug()<<locale.name();
    locale = QLocale(QLocale::English,QLocale::UnitedStates);
    qDebug()<<locale.name();

18、QString QLocale::nativeCountryName() const和QString QLocale::nativeLanguageName() const。返回国名和语言名。

    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 QLocale::setDefault(const QLocale &locale)。设置应用程序的默认QLocale。

20、[static] QLocale QLocale::system()。返回应用程序的QLocale。

21、Qt::LayoutDirection QLocale::textDirection() const。返回本地语言的文本布局方向。

22、QDate QLocale::toDate(const QString &string, QLocale::FormatType format = LongFormat) const。字符串转日期。

23、QDateTime QLocale::toDateTime(const QString &string, QLocale::FormatType format = LongFormat) const。字符串转日期时间。

24、QTime QLocale::toTime(const QString &string, const QString &format) const。字符串转时间。

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> QLocale::weekdays() const。返回本地工作日的列表。

四、相关非成员

1、uint qHash(const QLocale &key, uint seed = 0)。QLocale对象的哈希值。

猜你喜欢

转载自blog.csdn.net/kenfan1647/article/details/115238551