QStringDecoder — PyQt Documentation v6.9.0 (original) (raw)
PyQt6.QtCore.QStringDecoder
Inherits from QStringConverter.
Description¶
The QStringDecoder class provides a state-based decoder for text.
A text decoder converts text an encoded text format that uses a specific encoding into Qt’s internal representation.
Converting encoded data into a QString can be achieved using the following code:
QByteArray encodedString = "...";
auto toUtf16 = QStringDecoder(QStringDecoder::Utf8);
QString string = toUtf16(encodedString);
The decoder 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 decoder whenever new data is available:
auto toUtf16 = QStringDecoder(QStringDecoder::Utf8);
QString string;
while (new_data_available()) {
QByteArray chunk = get_new_data();
string += toUtf16(chunk);
}
The QStringDecoder object maintains state between chunks and therefore works correctly even if chunks are split in the middle of a multi-byte character sequence.
QStringDecoder objects can’t be copied because of their internal state, but can be moved.
Methods¶
__init__()
Default constructs an decoder. The default decoder is not valid, and can’t be used for converting text.
__init__(Encoding, flags: Flag = Default)
TODO
__init__(Union[Union[QByteArray, bytes, bytearray, memoryview], Optional[str]], flags: Flag = Default)
TODO
__call__(Union[QByteArray, bytes, bytearray, memoryview]) → str
TODO
decode(Union[QByteArray, bytes, bytearray, memoryview]) → str
TODO
@staticmethod
decoderForHtml(Union[QByteArray, bytes, bytearray, memoryview]) → QStringDecoder
Tries to determine the encoding of the HTML in data by looking at leading byte order marks or a charset specifier in the HTML meta tag and returns a QStringDecoder matching the encoding. If the returned decoder is not valid, the encoding specified is not supported by QStringConverter. If no encoding is detected, the method returns a decoder for Utf8.