描述
QLocale类可以在多种语言之间进行数字和字符串的转换。
 QLocale类在构造函数中使用语言/国家对进行初始化,并提供类似于QString中的数字转字符串和字符串转数字的转换函数。
 示例:
  QLocale egyptian(QLocale::Arabic, QLocale::Egypt);
  QString s1 = egyptian.toString(1.571429E+07, 'e');
  QString s2 = egyptian.toString(10);
  double d = egyptian.toDouble(s1);
  int i = egyptian.toInt(s2);
QLocale支持默认语言环境的概念,该语言环境在应用程序启动时根据系统的语言环境设置确定。可以通过调用静态成员函数setDefault()来更改默认语言环境。
 设置默认语言环境有以下效果:
 如果使用默认构造函数创建QLocale对象,它将使用默认语言环境的设置。
 QString::toInt()、QString::toDouble()等将根据默认语言环境解释字符串。如果解释失败,则返回"C"语言环境。
 在格式字符串的位置说明符中包含’L’时,QString::arg()使用默认语言环境格式化数字,例如"%L1"。
 以下示例说明了如何直接使用QLocale:
  QLocale::setDefault(QLocale(QLocale::Hebrew, QLocale::Israel));
  QLocale hebrew; // 构造一个默认的QLocale对象
  QString s1 = hebrew.toString(15714.3, 'e');
  bool ok;
  double d;
  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"
- 在构造函数中指定语言/国家对时,会出现以下三种情况之一:
- 如果语言/国家对在数据库中找到,则使用它。
- 如果找到了语言但没有找到国家,或者国家为AnyCountry,则使用具有最合适的可用国家的语言(例如,对于德语,使用德国)。
- 如果既没有找到语言也没有找到国家,则QLocale默认使用默认语言环境(参见setDefault())。
 可以使用()和country()来确定实际使用的语言和国家值。
- 另一种构造QLocale对象的方法是通过指定区域设置名称。
  QLocale korean("ko");
  QLocale swiss("de_CH");
该构造函数将区域设置名称转换为语言/国家对;它不使用系统的区域设置数据库。
 注意:要获取当前键盘输入语言环境,请查看QInputMethod::locale()。
常用函数
枚举
-  Country { AnyCountry, Afghanistan, Albania, Algeria, …, SintMaarten }:国家/地区枚举类型
-  CurrencySymbolFormat { CurrencyIsoCode, CurrencySymbol, CurrencyDisplayName }:货币符号格式枚举类型
-  FloatingPointPrecisionOption { FloatingPointest }:浮点数精度选项枚举类型
-  FormatType { LongFormat, ShortFormat, NarrowFormat }:格式类型枚举类型
-  Language { AnyLanguage, C, Abazian, Oromo, …, UncodedLanguages }:语言枚举类型
-  MeasurementSystem { MetricSystem, ImperialUSSystem, ImperialUKSystem, ImperialSystem }:计量系统枚举类型
-  NumberOption { DefaultNumberOptions, OmitGroupSeparator, RejectGroupSeparator, OmitLeadingZeroInponent, …, RejectTrailingZeroesAfterDot }:数字选项枚举类型
-  QuotationStyle { StandardQuotation, AlternateQuotation }:引用样式枚举类型
-  Script { AnyScript, AdlamScript, AhomScript, AnatolianHieroglyphsScript, YiScript }:脚本枚举类型
公有方法
-  QLocale():默认构造函数。
-  QLocale(const QString &name):通过区域的BCP-47名称来构造QLocale对象。
-  QLocale(Language language, Country country = AnyCountry):通过语言和国家/地区来构造QLocale对象。
-  QLocale(Language language, Script script, Country country):通过语言、脚本和国家/地区来构造QLocale对象。
-  QLocale(const QLocale &other):拷贝构造函数。
-  ~QLocale():析构函数。
-  QString amText() const:获取上午的文本表示。
-  QString bcp47Name() const:获取BCP-47格式的名称。
-  Country country() const:获取国家/地区。
-  QString createSeparatedList(const QStringList &list) const:将列表中的字符串用适当的分隔符连接起来。
-  QString currencySymbol(CurrencySymbolFormat format = CurrencySymbol) const:获取货币符号。
-  QString dateFormatFormatType format = LongFormat) const:获取日期格式。
-  `QString dateTimeFormat(FormatType format = LongFormat) const:获取日期和时间格式。 
-  QString dayName(int day, FormatType type = LongFormat) const:获取星期几的名称。
-  QChar decimalPoint() const:获取小数点符号。
-  QChar exponential() const:获取科学计数法表示中的指数符号。
-  Qt::DayOfWeek firstDayOfWeek() const:获取星期的第一天。
-  QChar groupSeparator() const:获取千位分隔符。
-  Language language() const:获取语言。
-  MeasurementSystem measurementSystem() const:获取测量系统。
-  QString monthName(int month, FormatType type = LongFormat) const:获取月份的名称。
-  QString name() const:获取区域的名称。
-  QString nativeCountryName() const:获取本地化的国家/地区名称。
-  QString nativeLanguageName() const:获取本地化的语言名称。
-  QChar negativeSign() const:获取负号符号。
-  NumberOptions numberOptions() const:获取数字显示的选项。
-  QChar percent() const:获取百分号符号。
-  QString pmText() const:获取下午的文本表示。
-  QChar positiveSign() const:获取正号符号。
-  QString quoteString(const QString &str, QuotationStyle style = StandardQuotation) const:将字符串用引号括起来。
-  QString quoteString(const QStringRef &str, QuotationStyle style = StandardQuotation) const:将字符串引用用引号括起来。
-  Script script() const获取脚本。
-  void setNumberOptions(NumberOptions options)设置数字显示的选项。
-  QString standaloneDayName(int day, FormatType type = LongFormat) const获取独立的星期几的名称。
-  QString standaloneMonthName(int month, FormatType type = LongFormat) const获取独立的月份的名称。
-  void swap(QLocale &other)交换两个QLocale对象的内容。
-  Qt::LayoutDirection textDirection() const获取文本的布局方向。
-  QString timeFormat(FormatType format = LongFormat) const获取时间格式。
-  QString toCurrencyString(qlonglong value, const QString &symbol = QString()) const将长整型数值转换为货币字符串。
-  QString toCurrencyString(qulonglong value, const QString &symbol = QString()) const将无符号长整型数值转换为货币字符串。
-  QString toCurrencyString(short value, const QString &symbol = QString()) const将短整型数值转换为货币字符串。
-  QString toCurrencyString(ushort value, const QString &symbol = QString()) const将无符号短整型数值转换为货币字符串。
-  QString toCurrencyString(int value, const QString &symbol = QString()) const将整型数值转换为货币字符串。
-  QString toCurrencyString(uint value, const QString &symbol = QString()) const将无符号整型数值转换为货币字符串。
-  QString toCurrencyString(double value, const QString &symbol = QString()) const将双精度浮点数值转换为货币字符串。
-  QString toCurrencyString(double value, const QString &symbol, int precision) const将双精度浮点数值以指定精度转换为货币字符串。
-  QString toCurrencyString(float value, const QString &symbol = QString()) const将单精度浮点数值转换为货币字符串。
-  QString toCurrencyString(float value, const QString &symbol, int precision) const将单精度浮点数值以指定精度转换为货币字符串。
-  QDate toDate(const QString &string, FormatType format = LongFormat) const将字符串转换为日期。
-  QDate toDate(const QString &string, const QString &format) const将字符串按指定格式转换为日期。
-  QDateTime toDateTime(const QString &string, FormatType format = LongFormat) const将字符串转换为日期和时间。
-  QDateTime toDateTime(const QString &string, const QString &format) const将字符串按指定格式转换为日期和时间。
-  double toDouble(const QString &s, bool *ok = Q_NULLPTR) const将字符串转换为双精度浮点数。
-  double toDouble(const QStringRef &s, bool *ok = Q_NULLPTR) const将字符串引用转换为双精度浮点数。
-  float toFloat(const QString &s, bool *ok = Q_NULLPTR) const将字符串转换为单精度浮点数。
-  float toFloat(const QStringRef &s, bool *ok = Q_NULLPTR) const将字符串引用转换为单精度浮点数。
-  int toInt(const QString &s, bool *ok = Q_NULLPTR) const将字符串转换为整型数。
-  int toInt(const QStringRef &s, bool *ok = Q_NULLPTR) const将字符串引用转换为整型数。
-  qlonglong toLongLong(const QString &s, bool *ok = Q_NULLPTR) const将字符串转换为长整型数。
-  qlonglong toLongLong(const QStringRef &s, bool *ok = Q_NULLPTR) const将字符串引用转换为长整型数。
-  QString toLower(const QString &str) const将字符串转换为小写形式。
-  short toShort(const QString &s, bool *ok = Q_NULLPTR) const将字符串转换为短整型数。
-  short toShort(const QStringRef &s, bool *ok = Q_NULLPTR) const将字符串引用转换为短整型数。
-  QString toString(qlonglong i) const将长整型数转换为字符串。
-  QString toString(qulonglong i) const将无符号长整型数转换为字符串。
-  QString toString(short i) const将短整型数转换为字符串。
-  QString toString(ushort i) const将无符号短整型数转换为字符串。
-  QString toString(int i) const将整型数转换为字符串。
-  QString toString(uint i) const将无符号整型数转换为字符串。
-  QString toString(double i, char f = 'g', int prec = 6) const将双精度浮点数转换为字符串。
-  QString toString(float i, char f = 'g', int prec = 6) const将单精度浮点数转换为字符串。
-  QString toString(const QDate &date, const QString &format) const将日期按指定格式转换为字符串。
-  QString toString(const QDate &date, FormatType format = LongFormat) const将日期转换为字符串。
-  QString toString(const QTime &time, const QString &format) const将时间按指定格式转换为字符串。
-  QString toString(const QTime &time, FormatType format = LongFormat) const将时间转换为字符串。
-  QString toString(const QDateTime &dateTime, FormatType format = LongFormat) const将日期和时间转换为字符串。
-  QString toString(const QDateTime &dateTime, const QString &format) const将日期和时间按指定格式转换为字符串。
-  QTime toTime(const QString &string, FormatType format = LongFormat) const将字符串转换为时间。
-  QTime toTime(const QString &string, const QString &format) const将字符串按指定格式转换为时间。
-  uint toUInt(const QString &s, bool *ok = Q_NULLPTR) const将字符串转换为无符号整型数。
-  uint toUInt(const QStringRef &s, bool *ok = Q_NULLPTR) const将字符串引用转换为无符号整型数。
-  qulonglong toULongLong(const QString &s, bool *ok = Q_NULLPTR) const将字符串转换为无符号长整型数。
-  qulonglong toULongLong(const QStringRef &s, bool *ok = Q_NULLPTR) const将字符串引用转换为无符号长整型数。
-  ushort toUShort(const QString &s, bool *ok = Q_NULLPTR) const将字符串转换为无符号短整型数。
-  ushort toUShort(const QStringRef &s, bool *ok = Q_NULLPTR) const将字符串引用转换为无符号短整型数。
-  QString toUpper(const QString &str) const将字符串转换为大写形式。
-  QStringList uiLanguages() const获取支持的UI语言列表。
-  QList<Qt::DayOfWeek weekdays() const获取一周的星期几列表。
-  QChar zeroDigit() const获取零字符。
-  bool operator!=(const QLocale &other) const判断两个QLocale对象是否不相等。
-  QLocale &operator=(QLocale &&other)将一个QLocale对象的内容移动赋值给当前对象。
-  QLocale &operator=(const QLocale &other)将一个QLocale对象的内容复制给当前对象。
-  bool operator==(const QLocale &other) const判断两个QLocale对象是否相等。
示例
    // 获取系统的默认区域设置
    QLocale locale = QLocale::system();
    // 将数字转换为字符串
    int number = 123456;
    QString numberString = locale.toString(number);
    qDebug() << "Number:" << numberString; // 输出:"Number: 123,456"(如果系统的默认区域设置是中文或其他使用逗号作为千位分隔符的语言)
    // 将字符串转换为数字
    QString numberString2 = "987,654.32";
    double number2 = locale.toDouble(numberString2);
    qDebug() << "Number2:" << number2; // 输出:"Number2: 987654.32"
    // 格式化日期和时间
    QDateTime dateTime = QDateTime::currentDateTime();
    QString formattedDateTime = locale.toString(dateTime, "yyyy-MM-dd hh:mm:ss");
    qDebug() << "Formatted DateTime:" << formattedDateTime; // 输出:"Formatted DateTime: 2023-10-29 11:31:33"
    // 获取星期几的名称
    for (int i = Qt::Monday; i <= Qt::Sunday; i++) {
        Qt::DayOfWeek dayOfWeek = static_cast<Qt::DayOfWeek>(i);
        QString dayName = locale.standaloneDayName(dayOfWeek, QLocale::ShortFormat);
        qDebug() << "Day Name:" << dayName; // 输出:星期一、星期二等(根据系统的默认区域设置)
    }
结果

使用场景
-  数字和字符串的格式化:QLocale可以将数字格式化为带有千位分隔符、小数位数和货币符号的字符串,也可以将字符串转换为数字。 
-  日期和时间的格式化:QLocale可以将日期和时间格式化为特定的日期格式、时间格式和日期时间格式,以适应不同的地区和语言习惯。 
-  货币格式化:QLocale支持将货币格式化为带有货币符号、千位分隔符和小数位数的字符串,并可以根据特定国家/地区的习惯进行舍入和舍入规则。 
-  语言和地区设置:QLocale可以获取当前系统的默认区域设置信息,包括当前所使用的语言、国家/地区、货币和日期时间的格式等。 
-  语言和地区的切换:QLocale使得应用程序可以根据用户的偏好在不同的语言和地区之间进行切换,以提供本地化的用户界面和内容。 
总之,QLocale是一个重要的工具类,用于处理与地区相关的数据和操作,它可以帮助开发人员实现国际化和本地化的功能,使应用程序能够根据用户的语言和地区偏好提供适当的显示和格式。



















![SHCTF 山河CTF Reverse方向[Week1]全WP 详解](https://img-blog.csdnimg.cn/2d40c31e10b54238b566420c750bb564.png)