Warning

This section contains snippets that were automatically translated from C++ to Python and may contain errors.

Unicode in Qt#

Information about support for Unicode in Qt.

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’s Classes for Working with Strings#

These classes are relevant when working with string data. For information about rendering text, see the Rich Text Processing overview, and if your string data is in XML, see the XML Processing overview.

PySide6.QtCore.QTextStream

The QTextStream class provides a convenient interface for reading and writing text.

PySide6.QtCore.QAnyStringView

The QAnyStringView class provides a unified view on Latin-1, UTF-8, or UTF-16 strings with a read-only subset of the QString API.

PySide6.QtCore.QByteArray

The QByteArray class provides an array of bytes.

QByteArrayList

The QByteArrayList class provides a list of byte arrays.

PySide6.QtCore.QByteArrayMatcher

The QByteArrayMatcher class holds a sequence of bytes that can be quickly matched in a byte array.

QStaticByteArrayMatcher

The QStaticByteArrayMatcher class is a compile-time version of QByteArrayMatcher.

PySide6.QtCore.QByteArrayView

The QByteArrayView class provides a view on an array of bytes with a read-only subset of the QByteArray API.

QLatin1Char

The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.

PySide6.QtCore.QChar

The QChar class provides a 16-bit Unicode character.

PySide6.QtCore.QCollator

The QCollator class compares strings according to a localized collation algorithm.

PySide6.QtCore.QCollatorSortKey

The QCollatorSortKey class can be used to speed up string collation.

QLatin1StringMatcher

Optimized search for substring in Latin-1 text.

PySide6.QtCore.QLocale

The QLocale class converts between numbers and their string representations in various languages.

PySide6.QtCore.QString

The QString class provides a Unicode character string.

QLatin1StringView

The QLatin1StringView class provides a thin wrapper around a US-ASCII/Latin-1 encoded string literal.

PySide6.QtCore.QStringList

The QStringList class provides a list of strings.

QStringMatcher

The QStringMatcher class holds a sequence of characters that can be quickly matched in a Unicode string.

QStringTokenizer

The QStringTokenizer class splits strings into tokens along given separators.

PySide6.QtCore.QStringView

The QStringView class provides a unified view on UTF-16 strings with a read-only subset of the QString API.

PySide6.QtCore.QTextBoundaryFinder

The QTextBoundaryFinder class provides a way of finding Unicode text boundaries in a string.

QUtf8StringView

The QUtf8StringView class provides a unified view on UTF-8 strings with a read-only subset of the QString API.

Information about Unicode on the Web#

The Unicode Consortium has a number of documents available, including

Unicode in Qt#

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 encoding 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, 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, tr() , that provides translation support, like this:

label.setText(tr("Password:"))

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:

file = QFile("appicon.png")

or

file = QFile("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.