diff options
author | Sona Kurazyan <sona.kurazyan@qt.io> | 2020-04-17 09:39:35 +0200 |
---|---|---|
committer | Sona Kurazyan <sona.kurazyan@qt.io> | 2020-04-17 15:38:34 +0200 |
commit | 339dd743a9e4db170f843a0ba51e5c93761f5644 (patch) | |
tree | d2c82eaf72a2060c9fbbc7eb05f2d72b773c3b10 /src/corelib/doc/snippets/code | |
parent | 300aaec2f9b72158aa5b9b26a4662b6fa635eec1 (diff) |
Add an example of error handling of QFuture without using exceptions
Also fixed a few unrelated typos in docs.
Task-number: QTBUG-83236
Change-Id: I776cda8f0ef4de6c4a93e94092dc19e94d1884c8
Reviewed-by: Vitaly Fanaskov <vitaly.fanaskov@qt.io>
Reviewed-by: Leena Miettinen <riitta-leena.miettinen@qt.io>
Diffstat (limited to 'src/corelib/doc/snippets/code')
-rw-r--r-- | src/corelib/doc/snippets/code/src_corelib_thread_qfuture.cpp | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/corelib/doc/snippets/code/src_corelib_thread_qfuture.cpp b/src/corelib/doc/snippets/code/src_corelib_thread_qfuture.cpp index dfa9b670e7..4fe3f7e99e 100644 --- a/src/corelib/doc/snippets/code/src_corelib_thread_qfuture.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_thread_qfuture.cpp @@ -72,3 +72,30 @@ i.toBack(); while (i.hasPrevious()) qDebug() << i.previous(); //! [2] + +//! [3] +using NetworkReply = std::variant<QByteArray, QNetworkReply::NetworkError>; + +enum class IOError { FailedToRead, FailedToWrite }; +using IOResult = std::variant<QString, IOError>; +//! [3] + +//! [4] +QFuture<IOResult> future = QtConcurrent::run([url] { + ... + return NetworkReply(QNetworkReply::TimeoutError); +}).then([](NetworkReply reply) { + if (auto error = std::get_if<QNetworkReply::NetworkError>(&reply)) + return IOResult(IOError::FailedToRead); + + auto data = std::get_if<QByteArray>(&reply); + // try to write *data and return IOError::FailedToWrite on failure + ... +}); + +auto result = future.result(); +if (auto filePath = std::get_if<QString>(&result)) { + // do something with *filePath +else + // process the error +//! [4] |