Qt Unicode

Unicode is a multi-byte character set, portable across all major computing platforms and with decent coverage over most of the world. It is also single-locale; it includes no code pages or other complexities that make software harder to write and test. There is no competing character set that's reasonably cross-platform. For these reasons, Unicode 4.0 is used as the native character set for Qt.

用于操控字符串的 Qt 类

这些类是相关的,当操控字符串数据时。渲染文本的有关信息,见 富文本处理 概述;若字符串数据采用 XML (可扩展标记语言) 方式,见 XML 处理 概述。

QByteArray 字节数组
QByteArrayMatcher 保持在字节数组中可以快速匹配的字节序列
QChar 16 位 Unicode 字符
QLatin1Char 8 位 ASCII/Latin-1 字符
QLatin1String 围绕 US-ASCII/Latin-1 编码字符串文字的瘦包裹器
QLocale 在数字及其各种语言的字符串表示之间转换
QString Unicode 字符串
QStringList 字符串列表
QStringMatcher 保持可以在 Unicode 字符串中快速匹配的字符序列
QStringRef 围绕 QString 子字符串的瘦包裹器
QTextBoundaryFinder 在字符串中查找 Unicode 文本边界的办法
QTextStream 用于读写文本的方便接口

Web 中 Unicode 的有关信息

The Unicode 联盟 有很多可用文档,包括

The Standard

The current version of the standard is Unicode 5.1.0 .

Previous printed versions of the specification:

Qt Unicode

在 Qt 中,在使用 Qt 的大多数应用程序中,用户可见的大多数 (或所有) 字符串都是使用 Unicode 存储的。Qt 提供:

  • Translation to/from legacy encodings for file I/O: see QTextCodec and QTextStream .
  • Translation from Input Methods and 8-bit keyboard input.
  • Translation to legacy character sets for on-screen display.
  • 字符串类 QString , that stores Unicode characters, with support for migrating from C strings including fast (cached) translation to and from US-ASCII, and all the usual string operations.
  • Unicode-aware widgets where appropriate.
  • Unicode support detection on Windows, so that Qt provides Unicode even on Windows platforms that do not support it natively.

为完全利用 Unicode 的好处,推荐使用 QString 为存储所有用户可见字符串,而履行所有文本文件 I/O 使用 QTextStream 。使用 QKeyEvent::text () for keyboard input in any custom widgets you write; it does not make much difference for slow typists in Western Europe or North America, but for fast typists or people using special input methods using text() is beneficial.

Qt 中的所有函数自变量可以是用户可见字符串, QLabel::setText () 及很多其它,接受 const QString & QString 提供隐式铸造从 const char * 所以事情像

label->setText("Password:");
					

会工作。还有函数 QObject::tr (),提供翻译支持,像这样:

label->setText(tr("Password:"));
					

QObject::tr () 映射从 const char * 到 Unicode 字符串,和使用可安装 QTranslator 对象来做映射。

Qt 提供了很多内置 QTextCodec classes, that is, classes that know how to translate between Unicode and legacy encodings to support programs that must talk to other programs or read/write files in legacy file formats.

By default, conversion to/from const char * uses a locale-dependent codec. However, applications can easily find codecs for other locales, and set any open file or network connection to use a special codec. It is also possible to install new codecs, for encodings that the built-in ones do not support. (At the time of writing, Vietnamese/VISCII is one such example.)

Since US-ASCII and ISO-8859-1 are so common, there are also especially fast functions for mapping to and from them. For example, to open an application's icon one might do this:

QFile file(QString::fromLatin1("appicon.png"));
					

or

QFile file(QLatin1String("appicon.png"));
					

Regarding output, Qt will do a best-effort conversion from Unicode to whatever encoding the system and fonts provide. Depending on operating system, locale, font availability, and Qt's support for the characters used, this conversion may be good or bad. We will extend this in upcoming versions, with emphasis on the most common locales first.

另请参阅 Qt 国际化 .