QStringDecoder 类

QStringDecoder 类提供用于文本基于状态的解码器。 更多...

头: #include <QStringDecoder>
CMake: find_package(Qt6 COMPONENTS Core REQUIRED)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core
继承: QStringConverter

注意: 此类的所有函数 可重入 .

公共函数

  QStringDecoder (const char * name , QStringConverter::Flags flags = Flag::Default)
  QStringDecoder ()
  QStringDecoder (QStringConverter::Encoding encoding , QStringConverter::Flags flags = Flag::Default)
QChar * appendToBuffer (QChar * out , QByteArrayView in )
QString decode (const QByteArray & ba )
QString decode (QByteArrayView ba )
qsizetype requiredSpace (qsizetype inputLength ) const
QString operator() (const QByteArray & ba )
QString operator() (QByteArrayView ba )

详细描述

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.

另请参阅 QStringConverter and QStringEncoder .

成员函数文档编制

QString QStringDecoder:: decode ( QByteArrayView ba )

QString QStringDecoder:: decode (const QByteArray & ba )

QString QStringDecoder:: operator() ( QByteArrayView ba )

QString QStringDecoder:: operator() (const QByteArray & ba )

转换 ba and returns the data as a QString .

QStringDecoder:: QStringDecoder (const char * name , QStringConverter::Flags flags = Flag::Default)

Creates an decoder object using name and flags 。若 name is not the name of a known encoding an invalid converter will get created.

另请参阅 isValid ().

QStringDecoder:: QStringDecoder ()

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

QStringDecoder:: QStringDecoder ( QStringConverter::Encoding encoding , QStringConverter::Flags flags = Flag::Default)

Creates an decoder object using encoding and flags .

QChar *QStringDecoder:: appendToBuffer ( QChar * out , QByteArrayView in )

Decodes the sequence of bytes viewed by in and writes the decoded result into the buffer starting at out . Returns a pointer to the end of data written.

out needs to be large enough to be able to hold all the decoded data. Use requiredSpace to determine the maximum size requirements to decode an encoded data buffer of in.size() 字节。

另请参阅 requiredSpace .

qsizetype QStringDecoder:: requiredSpace ( qsizetype inputLength ) const

Returns the maximum amount of UTF-16 code units required to be able to process inputLength encoded data.

另请参阅 appendToBuffer .