Qt Unicode

Unicode is the standard for encoding text in almost all languages spoken in the world. It is nowadays used as the native encoding for text on most modern operating systems. The major exception is Microsoft Windows that still has a dual system supporting code pages and Unicode for applications.

Qt 5.0 uses and fully supports version 6.2 of the Unicode standard.

Qt's Classes for Working with Strings

These classes are relevant when working with string data. For information about rendering text, see the 富文本处理 overview, and if your string data is in XML, see the XML 处理 概述。

QTextStream

用于读写文本的方便接口

QByteArray

字节数组

QByteArrayList

字节数组列表

QByteArrayMatcher

保持在字节数组中可以快速匹配的字节序列

QStaticByteArrayMatcher

QByteArrayMatcher 的编译时版本

QChar

16 位 Unicode 字符

QLatin1Char

8 位 ASCII/Latin-1 字符

QCollator

根据本地整理算法比较字符串

QCollatorSortKey

可以被用于加速字符串整理

QLocale

在数字及其各种语言的字符串表示之间转换

QLatin1String

围绕 US-ASCII/Latin-1 编码字符串文字的瘦包裹器

QString

Unicode 字符串

QStringRef

围绕 QString 子字符串的瘦包裹器

QStringList

字符串列表

QStringMatcher

保持可以在 Unicode 字符串中快速匹配的字符序列

QTextBoundaryFinder

在字符串中查找 Unicode 文本边界的办法

Information about Unicode on the Web

Unicode Consortium has a number of documents available, including

Qt Unicode

In Qt, and in most applications that use Qt, most or all user-visible strings are stored using Unicode. Qt provides:

  • Translation to/from legacy encodings for file I/O: see QTextCodec and QTextStream .
  • Support for locale specific Input Methods and keyboards.
  • A string class, QString , that stores Unicode characters, with support for migrating from C strings including fast translation to and from UTF-8, ISO8859-1 and US-ASCII, and all the usual string operations.
  • Unicode-aware UI controls.
  • Unicode compliant text segmentation ( QTextBoundaryFinder )
  • Unicode compliant line breaking and text rendering

To fully benefit from Unicode, we recommend using QString for storing all user-visible strings, and performing all text file I/O using QTextStream .

All the function arguments in Qt that may be user-visible strings, QLabel::setText () and a many others, take const QString & s. QString provides implicit casting from const char * so that things like

label->setText("Password:");
					

will work. There is also a function, QObject::tr (), that provides translation support, like this:

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

QObject::tr () maps from const char * to a Unicode string, and uses installable QTranslator objects to do the mapping.

Qt provides a number of built-in 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.

Conversion to/from const char * uses a UTF-8. However, applications can easily find codecs for other locales, and set any open file or network connection to use a special codec.

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"));
					

Qt supports rendering text in most languages written in the world. The detailed list of supported writing systems depends a bit on operating system support and font availability on the target system.

另请参阅 Qt 国际化 .