diff options
Diffstat (limited to 'src/corelib/doc/snippets/code')
12 files changed, 162 insertions, 322 deletions
diff --git a/src/corelib/doc/snippets/code/doc_src_containers.cpp b/src/corelib/doc/snippets/code/doc_src_containers.cpp index 84935580c9..9b23a9056c 100644 --- a/src/corelib/doc/snippets/code/doc_src_containers.cpp +++ b/src/corelib/doc/snippets/code/doc_src_containers.cpp @@ -205,35 +205,35 @@ for (i = splitter->sizes().begin(); //! [15] -QLinkedList<QString> list; +QVector<QString> values; ... QString str; -foreach (str, list) +foreach (str, values) qDebug() << str; //! [15] //! [16] -QLinkedList<QString> list; +QVector<QString> values; ... -QLinkedListIterator<QString> i(list); +QVectorIterator<QString> i(values); while (i.hasNext()) qDebug() << i.next(); //! [16] //! [17] -QLinkedList<QString> list; +QVector<QString> values; ... -foreach (const QString &str, list) +foreach (const QString &str, values) qDebug() << str; //! [17] //! [18] -QLinkedList<QString> list; +QVector<QString> values; ... -foreach (const QString &str, list) { +foreach (const QString &str, values) { if (str.isEmpty()) break; qDebug() << str; diff --git a/src/corelib/doc/snippets/code/doc_src_properties.cpp b/src/corelib/doc/snippets/code/doc_src_properties.cpp index a67945bbcf..e026a47e23 100644 --- a/src/corelib/doc/snippets/code/doc_src_properties.cpp +++ b/src/corelib/doc/snippets/code/doc_src_properties.cpp @@ -54,7 +54,7 @@ Q_PROPERTY(type name MEMBER memberName [(READ getFunction | WRITE setFunction)]) [RESET resetFunction] [NOTIFY notifySignal] - [REVISION int] + [REVISION int | REVISION(int[, int])] [DESIGNABLE bool] [SCRIPTABLE bool] [STORED bool] diff --git a/src/corelib/doc/snippets/code/doc_src_qiterator.cpp b/src/corelib/doc/snippets/code/doc_src_qiterator.cpp index 58f4166c3e..0d0b864a83 100644 --- a/src/corelib/doc/snippets/code/doc_src_qiterator.cpp +++ b/src/corelib/doc/snippets/code/doc_src_qiterator.cpp @@ -64,24 +64,6 @@ while (i.hasPrevious()) qDebug() << i.previous(); //! [1] - -//! [2] -QLinkedList<float> list; -... -QLinkedListIterator<float> i(list); -while (i.hasNext()) - qDebug() << i.next(); -//! [2] - - -//! [3] -QLinkedListIterator<float> i(list); -i.toBack(); -while (i.hasPrevious()) - qDebug() << i.previous(); -//! [3] - - //! [4] QVector<float> vector; ... @@ -145,37 +127,6 @@ while (i.hasNext()) { } //! [10] - -//! [11] -QLinkedList<float> list; -... -QMutableLinkedListIterator<float> i(list); -while (i.hasNext()) - qDebug() << i.next(); -//! [11] - - -//! [12] -QMutableLinkedListIterator<float> i(list); -i.toBack(); -while (i.hasPrevious()) - qDebug() << i.previous(); -//! [12] - - -//! [13] -QMutableLinkedListIterator<int> i(list); -while (i.hasNext()) { - int val = i.next(); - if (val < 0) { - i.setValue(-val); - } else if (val == 0) { - i.remove(); - } -} -//! [13] - - //! [14] QVector<float> vector; ... @@ -184,7 +135,6 @@ while (i.hasNext()) qDebug() << i.next(); //! [14] - //! [15] QMutableVectorIterator<float> i(vector); i.toBack(); @@ -232,17 +182,6 @@ while (i.hasNext()) { } //! [19] - -//! [20] -QMutableLinkedListIterator<int> i(list); -while (i.hasNext()) { - int val = i.next(); - if (val < -32768 || val > 32767) - i.remove(); -} -//! [20] - - //! [21] QMutableVectorIterator<int> i(vector); while (i.hasNext()) { @@ -271,16 +210,6 @@ while (i.hasNext()) { } //! [23] - -//! [24] -QMutableLinkedListIterator<double> i(list); -while (i.hasNext()) { - double val = i.next(); - i.setValue(std::sqrt(val)); -} -//! [24] - - //! [25] QMutableVectorIterator<double> i(list); while (i.hasNext()) { diff --git a/src/corelib/doc/snippets/code/src_corelib_io_qdebug.cpp b/src/corelib/doc/snippets/code/src_corelib_io_qdebug.cpp index 14e72e99dd..5154dc5d68 100644 --- a/src/corelib/doc/snippets/code/src_corelib_io_qdebug.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_io_qdebug.cpp @@ -93,3 +93,8 @@ ba = QByteArray("a\0b", 3); qDebug() << ba // prints: "\a\x00""b" //! [1] + +//! [toString] + QTRY_VERIFY2(list.isEmpty(), qPrintable(QString::fromLatin1( + "Expected list to be empty, but it has the following items: %1")).arg(QDebug::toString(list))); +//! [toString] diff --git a/src/corelib/doc/snippets/code/src_corelib_serialization_qcborstream.cpp b/src/corelib/doc/snippets/code/src_corelib_serialization_qcborstream.cpp index 6ddb5a9365..973ae75847 100644 --- a/src/corelib/doc/snippets/code/src_corelib_serialization_qcborstream.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_serialization_qcborstream.cpp @@ -208,10 +208,10 @@ //! [19] //! [20] - void appendList(QCborStreamWriter &writer, const QLinkedList<QString> &list) + void appendList(QCborStreamWriter &writer, const QVector<QString> &values) { writer.startArray(); - for (const QString &s : list) + for (const QString &s : values) writer.append(s); writer.endArray(); } @@ -228,10 +228,10 @@ //! [21] //! [22] - void appendMap(QCborStreamWriter &writer, const QLinkedList<QPair<int, QString>> &list) + void appendMap(QCborStreamWriter &writer, const QVector<QPair<int, QString>> &values) { writer.startMap(); - for (const auto pair : list) { + for (const auto pair : values) { writer.append(pair.first) writer.append(pair.second); } 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..66fa62f6b3 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,106 @@ 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] + +//! [5] +QFuture<int> future = ...; + future.then([](QFuture<int> f) { + try { + ... + auto result = f.result(); + ... + } catch (QException &e) { + // handle the exception + } + }).then(...); +//! [5] + +//! [6] +QFuture<int> parentFuture = ...; +auto continuation = parentFuture.then([](int res1){ ... }).then([](int res2){ ... })... +... +// parentFuture throws an exception +try { + auto result = continuation.result(); +} catch (QException &e) { + // handle the exception +} +//! [6] + +//! [7] +QFuture<int> future = ...; +auto resultFuture = future.then([](int res) { + ... + throw Error(); + ... +}).onFailed([](const Error &e) { + // Handle exceptions of type Error + ... + return -1; +}).onFailed([] { + // Handle all other types of errors + ... + return -1; +}); + +auto result = resultFuture.result(); // result is -1 +//! [7] + +//! [8] +QFuture<int> future = ...; +future.then([](int res) { + ... + throw std::runtime_error("message"); + ... +}).onFailed([](const std::exception &e) { + // This handler will be invoked +}).onFailed([](const std::runtime_error &e) { + // This handler won't be invoked, because of the handler above. +}); +//! [8] + +//! [9] +QFuture<int> future = ...; +auto resultFuture = future.then([](int res) { + ... + throw Error("message"); + ... +}).onFailed([](const std::exception &e) { + // Won't be invoked +}).onFailed([](const QException &e) { + // Won't be invoked +}); + +try { + auto result = resultFuture.result(); +} catch(...) { + // Handle the exception +} +//! [9] diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qbytearray.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qbytearray.cpp index 01f620cf08..9c07a2e92c 100644 --- a/src/corelib/doc/snippets/code/src_corelib_tools_qbytearray.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qbytearray.cpp @@ -372,16 +372,13 @@ a = str.toFloat(&ok); // a == 0, ok == false //! [39] QByteArray text("Qt is great!"); text.toBase64(); // returns "UXQgaXMgZ3JlYXQh" -//! [39] -//! [39bis] QByteArray text("<p>Hello?</p>"); text.toBase64(QByteArray::Base64Encoding | QByteArray::OmitTrailingEquals); // returns "PHA+SGVsbG8/PC9wPg" text.toBase64(QByteArray::Base64Encoding); // returns "PHA+SGVsbG8/PC9wPg==" text.toBase64(QByteArray::Base64UrlEncoding); // returns "PHA-SGVsbG8_PC9wPg==" text.toBase64(QByteArray::Base64UrlEncoding | QByteArray::OmitTrailingEquals); // returns "PHA-SGVsbG8_PC9wPg" -//! [39bis] - +//! [39] //! [40] QByteArray ba; @@ -422,9 +419,7 @@ QDataStream in(&data, QIODevice::ReadOnly); //! [44] QByteArray text = QByteArray::fromBase64("UXQgaXMgZ3JlYXQh"); text.data(); // returns "Qt is great!" -//! [44] -//! [44bis] QByteArray::fromBase64("PHA+SGVsbG8/PC9wPg==", QByteArray::Base64Encoding); // returns "<p>Hello?</p>" QByteArray::fromBase64("PHA-SGVsbG8_PC9wPg==", QByteArray::Base64UrlEncoding); // returns "<p>Hello?</p>" //! [44bis] @@ -442,7 +437,6 @@ if (result.decodingStatus == QByteArray::Base64DecodingStatus::Ok) process(result.decoded); //! [44quater] - //! [45] QByteArray text = QByteArray::fromHex("517420697320677265617421"); text.data(); // returns "Qt is great!" diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qhash.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qhash.cpp index d30ad50ffc..1f2c505a02 100644 --- a/src/corelib/doc/snippets/code/src_corelib_tools_qhash.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qhash.cpp @@ -149,7 +149,7 @@ inline bool operator==(const Employee &e1, const Employee &e2) && e1.dateOfBirth() == e2.dateOfBirth(); } -inline uint qHash(const Employee &key, uint seed) +inline size_t qHash(const Employee &key, size_t seed) { return qHash(key.name(), seed) ^ key.dateOfBirth().day(); } @@ -312,7 +312,7 @@ qDeleteAll(hash2.keyBegin(), hash2.keyEnd()); //! [28] //! [qhashbits] -inline uint qHash(const std::vector<int> &key, uint seed = 0) +inline size_t qHash(const std::vector<int> &key, size_t seed = 0) { if (key.empty()) return seed; @@ -322,14 +322,14 @@ inline uint qHash(const std::vector<int> &key, uint seed = 0) //! [qhashbits] //! [qhashrange] -inline uint qHash(const std::vector<int> &key, uint seed = 0) +inline size_t qHash(const std::vector<int> &key, size_t seed = 0) { return qHashRange(key.begin(), key.end(), seed); } //! [qhashrange] //! [qhashrangecommutative] -inline uint qHash(const std::unordered_set<int> &key, uint seed = 0) +inline size_t qHash(const std::unordered_set<int> &key, size_t seed = 0) { return qHashRangeCommutative(key.begin(), key.end(), seed); } @@ -348,9 +348,9 @@ qHash(qMakePair(key.first, key.second), seed); //! [31] //! [32] -uint qHash(K key); -uint qHash(const K &key); +size_t qHash(K key); +size_t qHash(const K &key); -uint qHash(K key, uint seed); -uint qHash(const K &key, uint seed); +size_t qHash(K key, size_t seed); +size_t qHash(const K &key, size_t seed); //! [32] diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qlinkedlist.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qlinkedlist.cpp deleted file mode 100644 index e8754a5f34..0000000000 --- a/src/corelib/doc/snippets/code/src_corelib_tools_qlinkedlist.cpp +++ /dev/null @@ -1,214 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the documentation of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -//! [0] -QLinkedList<int> integerList; -QLinkedList<QTime> timeList; -//! [0] - - -//! [1] -QLinkedList<QString> list; -list << "one" << "two" << "three"; -// list: ["one", "two", "three"] -//! [1] - - -//! [2] -QLinkedList<QWidget *> list; -... -while (!list.isEmpty()) - delete list.takeFirst(); -//! [2] - - -//! [3] -QLinkedList<QString> list; -list.append("one"); -list.append("two"); -list.append("three"); -// list: ["one", "two", "three"] -//! [3] - - -//! [4] -QLinkedList<QString> list; -list.prepend("one"); -list.prepend("two"); -list.prepend("three"); -// list: ["three", "two", "one"] -//! [4] - - -//! [5] -QList<QString> list; -list << "sun" << "cloud" << "sun" << "rain"; -list.removeAll("sun"); -// list: ["cloud", "rain"] -//! [5] - - -//! [6] -QList<QString> list; -list << "sun" << "cloud" << "sun" << "rain"; -list.removeOne("sun"); -// list: ["cloud", "sun", "rain"] -//! [6] - - -//! [7] -QLinkedList<QString> list; -list.append("January"); -list.append("February"); -... -list.append("December"); - -QLinkedList<QString>::iterator i; -for (i = list.begin(); i != list.end(); ++i) - cout << *i << Qt::endl; -//! [7] - - -//! [8] -QLinkedList<QString> list; -... -QLinkedList<QString>::iterator it = std::find(list.begin(), - list.end(), "Joel"); -if (it != list.end()) - cout << "Found Joel" << Qt::endl; -//! [8] - - -//! [9] -QLinkedList<int>::iterator i; -for (i = list.begin(); i != list.end(); ++i) - *i += 2; -//! [9] - - -//! [10] -QLinkedList<QString> list; -... -QLinkedList<QString>::iterator i = list.begin(); -while (i != list.end()) { - if ((*i).startsWith('_')) - i = list.erase(i); - else - ++i; -} -//! [10] - - -//! [11] -QLinkedList<QString>::iterator i = list.begin(); -while (i != list.end()) { - QLinkedList<QString>::iterator previous = i; - ++i; - if ((*previous).startsWith('_')) - list.erase(previous); -} -//! [11] - - -//! [12] -// WRONG -while (i != list.end()) { - if ((*i).startsWith('_')) - list.erase(i); - ++i; -} -//! [12] - - -//! [13] -if (*it == "Hello") - *it = "Bonjour"; -//! [13] - - -//! [14] -QLinkedList<QString> list; -list.append("January"); -list.append("February"); -... -list.append("December"); - -QLinkedList<QString>::const_iterator i; -for (i = list.constBegin(); i != list.constEnd(); ++i) - cout << *i << Qt::endl; -//! [14] - - -//! [15] -QLinkedList<QString> list; -... -QLinkedList<QString>::const_iterator it = std::find(list.constBegin(), - list.constEnd(), "Joel"); -if (it != list.constEnd()) - cout << "Found Joel" << Qt::endl; -//! [15] - - -//! [16] -std::list<double> stdlist; -list.push_back(1.2); -list.push_back(0.5); -list.push_back(3.14); - -QLinkedList<double> list = QLinkedList<double>::fromStdList(stdlist); -//! [16] - - -//! [17] -QLinkedList<double> list; -list << 1.2 << 0.5 << 3.14; - -std::list<double> stdlist = list.toStdList(); -//! [17] diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qregexp.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qregexp.cpp index 433b01b21a..8339ea413e 100644 --- a/src/corelib/doc/snippets/code/src_corelib_tools_qregexp.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qregexp.cpp @@ -232,3 +232,12 @@ s2 = QRegExp::escape("f(x)"); // s2 == "f\\(x\\)" QRegExp rx("(" + QRegExp::escape(name) + "|" + QRegExp::escape(alias) + ")"); //! [20] + +{ +//! [21] +QString p("a .*|pattern"); + +// re matches exactly the pattern string p +QRegularExpression re(QRegularExpression::anchoredPattern(p)); +//! [21] +} diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qregularexpression.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qregularexpression.cpp index 99cd4ea7a2..b451ed5253 100644 --- a/src/corelib/doc/snippets/code/src_corelib_tools_qregularexpression.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qregularexpression.cpp @@ -285,15 +285,6 @@ if (!invalidRe.isValid()) { } { -//! [24] -QString p("a .*|pattern"); - -// re matches exactly the pattern string p -QRegularExpression re(QRegularExpression::anchoredPattern(p)); -//! [24] -} - -{ //! [26] QString escaped = QRegularExpression::escape("a(x) = f(x) + g(x)"); // escaped == "a\\(x\\)\\ \\=\\ f\\(x\\)\\ \\+\\ g\\(x\\)" diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qvector.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qvector.cpp index a05233049f..76a8d68f64 100644 --- a/src/corelib/doc/snippets/code/src_corelib_tools_qvector.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qvector.cpp @@ -115,6 +115,29 @@ vector.append(std::move(three)); //! [move-append] +//! [emplace] +QVector<QString> vector{"a", "ccc"}; +vector.emplace(1, 2, 'b'); +// vector: ["a", "bb", "ccc"] +//! [emplace] + + +//! [emplace-back] +QVector<QString> vector{"one", "two"}; +vector.emplaceBack(3, 'a'); +qDebug() << vector; +// vector: ["one", "two", "aaa"] +//! [emplace-back] + + +//! [emplace-back-ref] +QVector<QString> vector; +auto &ref = vector.emplaceBack(); +ref = "one"; +// vector: ["one"] +//! [emplace-back-ref] + + //! [8] QVector<QString> vector; vector.prepend("one"); |