diff options
Diffstat (limited to 'src/corelib/doc/snippets/code/src_corelib_text_qstringconverter.cpp')
-rw-r--r-- | src/corelib/doc/snippets/code/src_corelib_text_qstringconverter.cpp | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/corelib/doc/snippets/code/src_corelib_text_qstringconverter.cpp b/src/corelib/doc/snippets/code/src_corelib_text_qstringconverter.cpp index e7bd39c95c..b5bfc9cd55 100644 --- a/src/corelib/doc/snippets/code/src_corelib_text_qstringconverter.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_text_qstringconverter.cpp @@ -34,3 +34,27 @@ while (new_data_available()) { encoded += fromUtf16(chunk); } //! [3] + +{ +//! [4] +QByteArray encodedString = "..."; +auto toUtf16 = QStringDecoder(QStringDecoder::Utf8); +auto data = toUtf16(encodedString); // data's type is QStringDecoder::EncodedData<const QByteArray &> +QString string = toUtf16(encodedString); // Implicit conversion to QString + +// Here you have to cast "data" to QString +auto func = [&]() { return !toUtf16.hasError() ? QString(data) : u"foo"_s; } +//! [4] +} + +{ +//! [5] +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; } +//! [5] +} |