QStringEncoder 类提供用于文本基于状态的编码器。 更多...
| 头: |
#include <QStringEncoder>
|
| CMake: |
find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
|
| qmake: |
QT += core
|
| 继承: | QStringConverter |
注意: 此类的所有函数 可重入 .
| QStringEncoder () | |
| QStringEncoder (QAnyStringView name , QStringConverter::Flags flags = Flag::Default) | |
| QStringEncoder (QStringConverter::Encoding encoding , QStringConverter::Flags flags = Flag::Default) | |
| char * | appendToBuffer (char * out , QStringView in ) |
| QStringEncoder::DecodedData<QStringView> | encode (QStringView in ) |
| QStringEncoder::DecodedData<const QString &> | encode (const QString & in ) |
| qsizetype | requiredSpace (qsizetype inputLength ) const |
| QStringEncoder::DecodedData<QStringView> | operator() (QStringView in ) |
| QStringEncoder::DecodedData<const QString &> | operator() (const QString & in ) |
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.
另请参阅 QStringConverter and QStringDecoder .
转换 in and returns a struct that is implicitly convertible to QByteArray .
QString string = "..."; auto fromUtf16 = QStringEncoder(QStringEncoder::Utf8); auto data = fromUtf16(string); // data's type is QStringEncoder::DecodedData<const QString &> QByteArray encodedString = fromUtf16(string); // Implicit conversion to QByteArray // Here you have to cast "data" to QByteArray auto func = [&]() { return !fromUtf16.hasError() ? QByteArray(data) : "foo"_ba; }
[constexpr noexcept]
QStringEncoder::
QStringEncoder
()
Default constructs an encoder. The default encoder is not valid, and can't be used for converting text.
[explicit]
QStringEncoder::
QStringEncoder
(
QAnyStringView
name
,
QStringConverter::Flags
flags
= Flag::Default)
Creates an encoder object using name and flags 。若 name is not the name of a known encoding an invalid converter will get created.
注意:
In Qt versions prior to 6.8, this function took only a
const char *
, which was expected to be UTF-8-encoded.
另请参阅 isValid ().
[explicit constexpr]
QStringEncoder::
QStringEncoder
(
QStringConverter::Encoding
encoding
,
QStringConverter::Flags
flags
= Flag::Default)
Creates an encoder object using encoding and flags .
Encodes in and writes the encoded result into the buffer starting at out . Returns a pointer to the end of the data written.
注意:
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
. This function may write to any bytes between
out
and
out + requiredSpace()
, including those past the returned end pointer.
另请参阅 requiredSpace ().
Returns the maximum amount of characters required to be able to process inputLength decoded data.
另请参阅 appendToBuffer ().