Skip to content

PythonQt.Qt.QTextCodec

QTextCodec

PythonQtInstanceWrapper object

Native bases: PythonQtInstanceWrapper

Supplemental documentation: Qt 5 reference:

Detailed Description

Qt uses Unicode to store, draw and manipulate strings. In many situations you may wish to deal with data that uses a different encoding. For example, most Japanese documents are still stored in Shift-JIS or ISO 2022-JP, while Russian users often have their documents in KOI8-R or Windows-1251.

Qt provides a set of QTextCodec classes to help with converting non-Unicode formats to and from Unicode. You can also create your own codec classes.

The supported encodings are:

  • Big5
  • Big5-HKSCS
  • CP949
  • EUC-JP
  • EUC-KR
  • GB18030
  • HP-ROMAN8
  • IBM 850
  • IBM 866
  • IBM 874
  • ISO 2022-JP
  • ISO 8859-1 to 10
  • ISO 8859-13 to 16
  • Iscii-Bng, Dev, Gjr, Knd, Mlm, Ori, Pnj, Tlg, and Tml
  • KOI8-R
  • KOI8-U
  • Macintosh
  • Shift-JIS
  • TIS-620
  • TSCII
  • UTF-8
  • UTF-16
  • UTF-16BE
  • UTF-16LE
  • UTF-32
  • UTF-32BE
  • UTF-32LE
  • Windows-1250 to 1258

If Qt is compiled with ICU support enabled, most codecs supported by ICU will also be available to the application.

QTextCodecs can be used as follows to convert some locally encoded string to Unicode. Suppose you have some string encoded in Russian KOI8-R encoding, and want to convert it to Unicode. The simple way to do it is like this:

QByteArray encodedString = "...";
QTextCodec *codec = QTextCodec::codecForName("KOI8-R");
QString string = codec->toUnicode(encodedString);

After this, string holds the text converted to Unicode. Converting a string from Unicode to the local encoding is just as easy:

QString string = "...";
QTextCodec *codec = QTextCodec::codecForName("KOI8-R");
QByteArray encodedString = codec->fromUnicode(string);

To read or write files in various encodings, use QTextStream and its setCodec() function. See the Codecs example for an application of QTextCodec to file I/O.

Some care must be taken when trying to convert the data in chunks, for example, when receiving it over a network. In such cases it is possible that a multi-byte character will be split over two chunks. At best this might result in the loss of a character and at worst cause the entire conversion to fail.

The approach to use in these situations is to create a QTextDecoder object for the codec and use this QTextDecoder for the whole decoding process, as shown below:

QTextCodec *codec = QTextCodec::codecForName("Shift-JIS");
QTextDecoder *decoder = codec->makeDecoder();

QString string;
while (new_data_available()) {
    QByteArray chunk = get_new_data();
    string += decoder->toUnicode(chunk);
}
delete decoder;

The QTextDecoder object maintains state between chunks and therefore works correctly even if a multi-byte character is split between chunks.

Creating Your Own Codec Class

Support for new text encodings can be added to Qt by creating QTextCodec subclasses.

The pure virtual functions describe the encoder to the system and the coder is used as required in the different text file formats supported by QTextStream, and under X11, for the locale-specific character input and output.

To add support for another encoding to Qt, make a subclass of QTextCodec and implement the functions listed in the table below.

Function Description
name() Returns the official name for the encoding. If the encoding is listed in the IANA character-sets encoding file, the name should be the preferred MIME name for the encoding.
aliases() Returns a list of alternative names for the encoding. QTextCodec provides a default implementation that returns an empty list. For example, "ISO-8859-1" has "latin1", "CP819", "IBM819", and "iso-ir-100" as aliases.
mibEnum() Return the MIB enum for the encoding if it is listed in the IANA character-sets encoding file.
convertToUnicode() Converts an 8-bit character string to Unicode.
convertFromUnicode() Converts a Unicode string to an 8-bit character string.

Source: https://doc.qt.io/archives/qt-5.15/qtextcodec.html

Complete member index

Every captured member retains its own identity. Identical captured declaration text is linked once; this does not establish native inheritance or interchangeable objects.

Member Kind Signature Summary
ConversionFlag attribute type Convert a number or string to an integer, or return 0 if no arguments are given. (shared captured text)
ConversionFlags attribute type Convert a number or string to an integer, or return 0 if no arguments are given. (shared captured text)
ConvertInvalidToNull attribute Any (shared captured text)
DefaultConversion attribute Any (shared captured text)
FreeFunction attribute Any (shared captured text)
IgnoreHeader attribute Any (shared captured text)
__init__ function (self, *args: Any, **kwargs: Any) -> None Native constructor; consult class documentation for supported overloads. (shared captured text)
aliases function (self) -> Any Subclasses can return a number of aliases for the codec in question. (shared captured text)
availableCodecs function (self) -> Any Returns the list of all available codecs, by name. (shared captured text)
availableMibs function (self) -> tuple X.availableMibs() -> tuple (shared captured text)
canEncode function (self, arg__1: str) -> bool X.canEncode(arg__1) -> bool (shared captured text)
className function (self, *args: Any, **kwargs: Any) -> Any Return the classname of the object (shared captured text)
codecForHtml function (self, ba: Any, defaultCodec: Any) -> Any X.codecForHtml(ba, defaultCodec) -> PythonQt.QtCore.QTextCodec (shared captured text)
codecForLocale function (self) -> Any X.codecForLocale() -> PythonQt.QtCore.QTextCodec (shared captured text)
codecForMib function (self, mib: int) -> Any X.codecForMib(mib) -> PythonQt.QtCore.QTextCodec (shared captured text)
codecForName function (self, name: Any) -> Any X.codecForName(name) -> PythonQt.QtCore.QTextCodec (shared captured text)
codecForUtfText function (self, ba: Any, defaultCodec: Any) -> Any X.codecForUtfText(ba, defaultCodec) -> PythonQt.QtCore.QTextCodec (shared captured text)
convertFromUnicode function (self, *args: Any, **kwargs: Any) -> Any X.convertFromUnicode(in, length, state) -> PythonQt.QtCore.QByteArray (shared captured text)
convertToUnicode function (self, *args: Any, **kwargs: Any) -> str X.convertToUnicode(in, length, state) -> str (shared captured text)
delete function (self, *args: Any, **kwargs: Any) -> Any Deletes the C++ object (at your own risk, my friend!) (shared captured text)
fromUnicode function (self, uc: str) -> Any X.fromUnicode(uc) -> PythonQt.QtCore.QByteArray (shared captured text)
help function (self, *args: Any, **kwargs: Any) -> Any Shows the help of available methods for this class (shared captured text)
inherits function (self, *args: Any, **kwargs: Any) -> Any Returns if the class inherits or is of given type name (shared captured text)
makeDecoder function (self, arg0: flags) -> Any X.makeDecoder(flags) -> PythonQt.QtCore.QTextDecoder (shared captured text)
makeEncoder function (self, arg0: flags) -> Any X.makeEncoder(flags) -> PythonQt.QtCore.QTextEncoder (shared captured text)
mibEnum function (self) -> int X.mibEnum() -> int (shared captured text)
name function (self) -> Any X.name() -> PythonQt.QtCore.QByteArray (shared captured text)
setCodecForLocale function (self, c: Any) -> None Set the codec to c; this will be returned by [codecForLocale](https://doc.qt.io/archives/qt-5.15/qtextcodec.html#codecForLocale)(). (shared captured text)
toUnicode function (self, arg__1: Any) -> str X.toUnicode(arg__1) -> str (shared captured text)