The QStringEncoder class provides a state-based encoder for text. More

Inheritance diagram of PySide6.QtCore.QStringEncoder




This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE

Detailed Description#


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

A text encoder converts text from Qt’s internal representation into an encoded text format using a specific encoding.

Converting a string from Unicode to the local encoding can be achieved using the following code:

string = "..."
fromUtf16 = QStringEncoder(QStringEncoder.Utf8)
encodedString = fromUtf16(string)

The encoder remembers any state that is required between calls, so converting data received in chunks, for example, when receiving it over a network, is just as easy, by calling the encoder whenever new data is available:

fromUtf16 = QStringEncoder(QStringEncoder.Utf8)
encoded = QByteArray()
while new_data_available():
    chunk = get_new_data()
    encoded += fromUtf16(chunk)

The QStringEncoder object maintains state between chunks and therefore works correctly even if a UTF-16 surrogate character is split between chunks.

QStringEncoder objects can’t be copied because of their internal state, but can be moved.

class PySide6.QtCore.QStringEncoder#

PySide6.QtCore.QStringEncoder(encoding[, flags=QStringConverterBase.Flag.Default])

PySide6.QtCore.QStringEncoder(name[, flags=QStringConverterBase.Flag.Default])

  • name – str

  • encodingEncoding

  • flagsFlags

Default constructs an encoder. The default encoder is not valid, and can’t be used for converting text.



Return type:


Returns the maximum amount of characters required to be able to process inputLength decoded data.

See also