Learn what Felgo offers to help your business succeed. Start your free evaluation today! Felgo for Your Business

QStringEncoder Class

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

Header: #include <QStringEncoder>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core
Inherits: QStringConverter

Note: All functions in this class are reentrant.

Public Functions

QStringEncoder(const char *name, QStringConverter::Flags flags = Flag::Default)
QStringEncoder(QStringConverter::Encoding encoding, QStringConverter::Flags flags = Flag::Default)
QStringEncoder()
char * appendToBuffer(char *out, QStringView in)
QByteArray encode(const QString &in)
QByteArray encode(QStringView in)
qsizetype requiredSpace(qsizetype inputLength) const
QByteArray operator()(const QString &in)
QByteArray operator()(QStringView in)

Detailed Description

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:

QString string = "...";
auto fromUtf16 = QStringEncoder(QStringEncoder::Utf8);
QByteArray 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:

auto fromUtf16 = QStringEncoder(QStringEncoder::Utf8);

QByteArray encoded;
while (new_data_available()) {
    QString 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.

See also QStringConverter and QStringDecoder.

Member Function Documentation

QByteArray QStringEncoder::encode(QStringView in)

QByteArray QStringEncoder::encode(const QString &in)

QByteArray QStringEncoder::operator()(QStringView in)

QByteArray QStringEncoder::operator()(const QString &in)

Converts in and returns the data as a byte array.

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

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

See also isValid().

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

Creates an encoder object using encoding and flags.

QStringEncoder::QStringEncoder()

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

char *QStringEncoder::appendToBuffer(char *out, QStringView in)

Encodes in and writes the encoded result into the buffer starting at out. Returns a pointer to the end of the data written.

Note: out must be large enough to be able to hold all the decoded data. Use requiredSpace() to determine the maximum size requirement to be able to encode in.

See also requiredSpace().

qsizetype QStringEncoder::requiredSpace(qsizetype inputLength) const

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

See also appendToBuffer().

Qt_Technology_Partner_RGB_475 Qt_Service_Partner_RGB_475_padded