Qt 为什么使用 QString::number() 而不是 QLocale().toString()? [英] Qt Why use QString::number() over QLocale().toString()?

查看:47
本文介绍了Qt 为什么使用 QString::number() 而不是 QLocale().toString()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发的应用程序将在许多国家/地区推出,需要支持他们的语言.我一直在回顾我的代码并替换以下每个实例:

The application I am working on will be launched in many countries and needs to support their language. I've been going back through my code and replacing every instance of:

QString::number()QString().toDouble()

QLocale().toString()QLocale().toDouble()

我没有在网上找到太多比较这两个类的内容,但我对使用后者的影响很感兴趣,如果没有 - 为什么要使用 QString 函数?

I haven't found much online comparing the two classes, but I am interested in the repercussions of using the latter, and if there are none - why ever use the QString functions?

本质上,我只是想确保在进行所有这些更改之前不会损害我的代码.有人知道吗?

Essentially I just want to make sure I'm not harming my code before i make all these changes. Does anyone have any knowledge?

推荐答案

QString 方法与语言环境无关:它们总是发生在 C 语言环境中.当 I/O 未本地化时,这很有用,例如在应该跨区域设置和/或机器可读的数据文件中.

The QString methods are locale-independent: they always happen in the C locale. That's useful when the I/O is not localized, e.g. in data files that should be portable across locales and/or machine readable.

您绝对不应该随意地将 QString 方法的每次使用替换为来自 QLocale 的本地化对应方法!您需要确定哪些用途应该本地化:通常这些用途包括 UI 和一些文本文件 I/O,其中项目规范指出数字 I/O 应该本地化.如果规范未提及这一点,则值得先修改规范,并在面向用户的文档中记录行为.

You should definitely not haphazardly replace every use of QString methods with localized counterparts from QLocale! You need to determine which uses should be localized: typically those would include the UI and perhaps some text file I/O where the project specification states that the numeric I/O should be localized. If the spec doesn't mention that, it's worthwhile to amend the spec first, and document the behavior in user-facing documentation too.

以下注意事项适用于文本 I/O.

Following considerations apply to text I/O.

  1. 对你接受的东西持宽容态度,对你的输出结果持保守态度.

  1. Be permissive with what you accept, and conservative in what you output.

供人类消费的输出,而不是机器可读的数据提取,例如PDF 和 HTML 报告文件,应该有本地化的数字.

Output that is for human consumption and not meant to be machine-readable for data extraction, e.g. PDF and HTML report files, should have localized numbers.

机器消耗的输出,例如CSV 和 XML 文件,应使用 C 语言环境.

Output for machine consumption, e.g. CSV and XML files, should use the C locale.

文本输入应该允许选择所需的输入区域设置,并且应该是允许的.例如.使用 CSV 时,直接在输入上使用 QString::toDoubleQLocale::toDouble 都有帮助,但首先要预处理输入以检测语言环境并转换为固定 C 语言环境,然后才将其提供给 QString::toDouble.例如

Text input should allow selection of the desired input locale, and should be permissive. E.g. when consuming CSV it helps to use neither QString::toDouble nor QLocale::toDouble directly on the input, but first to preprocess the input to detect the locale and convert to a fixed C locale, and only then feed it to QString::toDouble. E.g.

QPair<double, bool> toDouble(QString in) {
  auto dots = in.count('.');
  auto commas = in.count(',');
  if ((dots > 1 && commas > 1) || (dots == 1 && commas ==1))
    // equivocal input
    return qMakePair(0.0, false);
  if (dots > 1 && commas <=1) {
    // dots are group separators
    in.replace(".", "");
    in.replace(',', '.');
  }
  else if (dots <= 1 && commas > 1) {
    // commas are group separators
    in.replace(",", "");
  }
  else if (commas == 1) {
    // assume commas are decimal points
    in.replace(',', '.');
  }
  bool ok;
  auto dbl = in.toDouble(&ok);
  return qMakePair(dbl, ok);
}

在实际代码中,您希望不是孤立地而是作为一个集合来检查所有数字,并确保您可以检测到组分隔符和小数点的明确选择,否则您将不得不拒绝输入.

In real code you'd want to examine all numbers not in isolation but as a set and make sure that you can detect an unequivocal choice for group separator and decimal point, otherwise you'd have to reject the input.

这篇关于Qt 为什么使用 QString::number() 而不是 QLocale().toString()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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