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 國際化 .