diff options
| author | Oleksii Zbykovskyi <Oleksii.Zbykovskyi@qt.io> | 2025-07-28 11:53:03 +0200 |
|---|---|---|
| committer | Oleksii Zbykovskyi <Oleksii.Zbykovskyi@qt.io> | 2025-07-29 15:20:01 +0200 |
| commit | c7a7783668f5ad92b30daf18ff52184a2aa5b4ca (patch) | |
| tree | f3f4f61c87e38414b26b5d540d4b4fbf6ad64aa0 /src/corelib/doc/snippets | |
| parent | c1889bd953892428a474822168c10a4f983fb3ae (diff) | |
Fix of files under tools prefix
Made each file compilable and added them to the build system.
Task-number: QTBUG-137566
Change-Id: I51aac946a1bfd8922c6a3669483d4e6f591795b5
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Diffstat (limited to 'src/corelib/doc/snippets')
20 files changed, 1867 insertions, 1379 deletions
diff --git a/src/corelib/doc/snippets/code/CMakeLists.txt b/src/corelib/doc/snippets/code/CMakeLists.txt index 859f3338e30..c657c8b966e 100644 --- a/src/corelib/doc/snippets/code/CMakeLists.txt +++ b/src/corelib/doc/snippets/code/CMakeLists.txt @@ -72,6 +72,24 @@ add_library(corelib_snippets_code OBJECT src_corelib_thread_qthread.cpp src_corelib_thread_qwaitcondition_unix.cpp src_corelib_time_qdatetime.cpp + src_corelib_tools_qbitarray.cpp + src_corelib_tools_qcommandlineoption.cpp + src_corelib_tools_qcommandlineparser_main.cpp + src_corelib_tools_qcommandlineparser.cpp + src_corelib_tools_qcontiguouscache.cpp + src_corelib_tools_qeasingcurve.cpp + src_corelib_tools_qhash.cpp + src_corelib_tools_qlist.cpp + src_corelib_tools_qmap.cpp + src_corelib_tools_qmultimap.cpp + src_corelib_tools_qpoint.cpp + src_corelib_tools_qqueue.cpp + src_corelib_tools_qrect.cpp + src_corelib_tools_qscopedpointer.cpp + src_corelib_tools_qscopeguard.cpp + src_corelib_tools_qshareddata.cpp + src_corelib_tools_qsharedpointer.cpp + src_corelib_tools_qsize.cpp ) target_link_libraries(corelib_snippets_code PRIVATE @@ -92,6 +110,7 @@ qt_internal_extend_target(corelib_snippets_code CONDITION QT_FEATURE_widgets src_corelib_global_qglobal_widgets.cpp src_corelib_kernel_qobject.cpp src_corelib_kernel_qtimer.cpp + src_corelib_tools_qtimeline.cpp ) qt_internal_extend_target(corelib_snippets_code CONDITION QT_FEATURE_gui diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qbitarray.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qbitarray.cpp index 7c98b6d8ff0..bb2bb57908b 100644 --- a/src/corelib/doc/snippets/code/src_corelib_tools_qbitarray.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qbitarray.cpp @@ -1,146 +1,169 @@ // Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -//! [0] -QBitArray ba(200); -//! [0] - - -//! [1] -QBitArray ba; -ba.resize(3); -ba[0] = true; -ba[1] = false; -ba[2] = true; -//! [1] - - -//! [2] -QBitArray ba(3); -ba.setBit(0, true); -ba.setBit(1, false); -ba.setBit(2, true); -//! [2] - - -//! [3] -QBitArray x(5); -x.setBit(3, true); -// x: [ 0, 0, 0, 1, 0 ] - -QBitArray y(5); -y.setBit(4, true); -// y: [ 0, 0, 0, 0, 1 ] - -x |= y; -// x: [ 0, 0, 0, 1, 1 ] -//! [3] - - -//! [4] -QBitArray().isNull(); // returns true -QBitArray().isEmpty(); // returns true - -QBitArray(0).isNull(); // returns false -QBitArray(0).isEmpty(); // returns true - -QBitArray(3).isNull(); // returns false -QBitArray(3).isEmpty(); // returns false -//! [4] - - -//! [5] -QBitArray().isNull(); // returns true -QBitArray(0).isNull(); // returns false -QBitArray(3).isNull(); // returns false -//! [5] - - -//! [6] -QBitArray ba(8); -ba.fill(true); -// ba: [ 1, 1, 1, 1, 1, 1, 1, 1 ] - -ba.fill(false, 2); -// ba: [ 0, 0 ] -//! [6] - - -//! [7] -QBitArray a(3); -a[0] = false; -a[1] = true; -a[2] = a[0] ^ a[1]; -//! [7] - - -//! [8] -QBitArray a(3); -QBitArray b(2); -a[0] = 1; a[1] = 0; a[2] = 1; // a: [ 1, 0, 1 ] -b[0] = 1; b[1] = 1; // b: [ 1, 1 ] -a &= b; // a: [ 1, 0, 0 ] -//! [8] - - -//! [9] -QBitArray a(3); -QBitArray b(2); -a[0] = 1; a[1] = 0; a[2] = 1; // a: [ 1, 0, 1 ] -b[0] = 1; b[1] = 1; // b: [ 1, 1 ] -a |= b; // a: [ 1, 1, 1 ] -//! [9] - - -//! [10] -QBitArray a(3); -QBitArray b(2); -a[0] = 1; a[1] = 0; a[2] = 1; // a: [ 1, 0, 1 ] -b[0] = 1; b[1] = 1; // b: [ 1, 1 ] -a ^= b; // a: [ 0, 1, 1 ] -//! [10] - - -//! [11] -QBitArray a(3); -QBitArray b; -a[0] = 1; a[1] = 0; a[2] = 1; // a: [ 1, 0, 1 ] -b = ~a; // b: [ 0, 1, 0 ] -//! [11] - - -//! [12] -QBitArray a(3); -QBitArray b(2); -QBitArray c; -a[0] = 1; a[1] = 0; a[2] = 1; // a: [ 1, 0, 1 ] -b[0] = 1; b[1] = 1; // b: [ 1, 1 ] -c = a & b; // c: [ 1, 0, 0 ] -//! [12] - - -//! [13] -QBitArray a(3); -QBitArray b(2); -QBitArray c; -a[0] = 1; a[1] = 0; a[2] = 1; // a: [ 1, 0, 1 ] -b[0] = 1; b[1] = 1; // b: [ 1, 1 ] -c = a | b; // c: [ 1, 1, 1 ] -//! [13] - - -//! [14] -QBitArray a(3); -QBitArray b(2); -QBitArray c; -a[0] = 1; a[1] = 0; a[2] = 1; // a: [ 1, 0, 1 ] -b[0] = 1; b[1] = 1; // b: [ 1, 1 ] -c = a ^ b; // c: [ 0, 1, 1 ] -//! [14] - -//! [15] -QBitArray ba(4); -ba.fill(true, 1, 2); // ba: [ 0, 1, 0, 0 ] -ba.fill(true, 1, 3); // ba: [ 0, 1, 1, 0 ] -ba.fill(true, 1, 4); // ba: [ 0, 1, 1, 1 ] -//! [15] +#include <QBitArray> + +void wrapInFunction() +{ + { + //! [0] + QBitArray ba(200); + //! [0] + } + + { + //! [1] + QBitArray ba; + ba.resize(3); + ba[0] = true; + ba[1] = false; + ba[2] = true; + //! [1] + } + + { + //! [2] + QBitArray ba(3); + ba.setBit(0, true); + ba.setBit(1, false); + ba.setBit(2, true); + //! [2] + } + + { + //! [3] + QBitArray x(5); + x.setBit(3, true); + // x: [ 0, 0, 0, 1, 0 ] + + QBitArray y(5); + y.setBit(4, true); + // y: [ 0, 0, 0, 0, 1 ] + + x |= y; + // x: [ 0, 0, 0, 1, 1 ] + //! [3] + } + + { + //! [4] + QBitArray().isNull(); // returns true + QBitArray().isEmpty(); // returns true + + QBitArray(0).isNull(); // returns false + QBitArray(0).isEmpty(); // returns true + + QBitArray(3).isNull(); // returns false + QBitArray(3).isEmpty(); // returns false + //! [4] + } + + { + //! [5] + QBitArray().isNull(); // returns true + QBitArray(0).isNull(); // returns false + QBitArray(3).isNull(); // returns false + //! [5] + } + + { + //! [6] + QBitArray ba(8); + ba.fill(true); + // ba: [ 1, 1, 1, 1, 1, 1, 1, 1 ] + + ba.fill(false, 2); + // ba: [ 0, 0 ] + //! [6] + } + + { + //! [7] + QBitArray a(3); + a[0] = false; + a[1] = true; + a[2] = a[0] ^ a[1]; + //! [7] + } + + { + //! [8] + QBitArray a(3); + QBitArray b(2); + a[0] = 1; a[1] = 0; a[2] = 1; // a: [ 1, 0, 1 ] + b[0] = 1; b[1] = 1; // b: [ 1, 1 ] + a &= b; // a: [ 1, 0, 0 ] + //! [8] + } + + { + //! [9] + QBitArray a(3); + QBitArray b(2); + a[0] = 1; a[1] = 0; a[2] = 1; // a: [ 1, 0, 1 ] + b[0] = 1; b[1] = 1; // b: [ 1, 1 ] + a |= b; // a: [ 1, 1, 1 ] + //! [9] + } + + { + //! [10] + QBitArray a(3); + QBitArray b(2); + a[0] = 1; a[1] = 0; a[2] = 1; // a: [ 1, 0, 1 ] + b[0] = 1; b[1] = 1; // b: [ 1, 1 ] + a ^= b; // a: [ 0, 1, 1 ] + //! [10] + } + + { + //! [11] + QBitArray a(3); + QBitArray b; + a[0] = 1; a[1] = 0; a[2] = 1; // a: [ 1, 0, 1 ] + b = ~a; // b: [ 0, 1, 0 ] + //! [11] + } + + { + //! [12] + QBitArray a(3); + QBitArray b(2); + QBitArray c; + a[0] = 1; a[1] = 0; a[2] = 1; // a: [ 1, 0, 1 ] + b[0] = 1; b[1] = 1; // b: [ 1, 1 ] + c = a & b; // c: [ 1, 0, 0 ] + //! [12] + } + + { + //! [13] + QBitArray a(3); + QBitArray b(2); + QBitArray c; + a[0] = 1; a[1] = 0; a[2] = 1; // a: [ 1, 0, 1 ] + b[0] = 1; b[1] = 1; // b: [ 1, 1 ] + c = a | b; // c: [ 1, 1, 1 ] + //! [13] + } + + { + //! [14] + QBitArray a(3); + QBitArray b(2); + QBitArray c; + a[0] = 1; a[1] = 0; a[2] = 1; // a: [ 1, 0, 1 ] + b[0] = 1; b[1] = 1; // b: [ 1, 1 ] + c = a ^ b; // c: [ 0, 1, 1 ] + //! [14] + } + + { + //! [15] + QBitArray ba(4); + ba.fill(true, 1, 2); // ba: [ 0, 1, 0, 0 ] + ba.fill(true, 1, 3); // ba: [ 0, 1, 1, 0 ] + ba.fill(true, 1, 4); // ba: [ 0, 1, 1, 1 ] + //! [15] + } +} diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qcommandlineoption.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qcommandlineoption.cpp index dbc83127ec6..1abaa9659ce 100644 --- a/src/corelib/doc/snippets/code/src_corelib_tools_qcommandlineoption.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qcommandlineoption.cpp @@ -6,20 +6,24 @@ int main() { + { + //! [0] + QCommandLineOption verboseOption("verbose", "Verbose mode. Prints out more information."); + QCommandLineOption outputOption(QStringList() << "o" << "output", "Write generated data into <file>.", "file"); + //! [0] + } -//! [0] -QCommandLineOption verboseOption("verbose", "Verbose mode. Prints out more information."); -QCommandLineOption outputOption(QStringList() << "o" << "output", "Write generated data into <file>.", "file"); -//! [0] - -//! [cxx11-init] -QCommandLineParser parser; -parser.addOption({"verbose", "Verbose mode. Prints out more information."}); -//! [cxx11-init] - -//! [cxx11-init-list] -QCommandLineParser parser; -parser.addOption({{"o", "output"}, "Write generated data into <file>.", "file"}); -//! [cxx11-init-list] + { + //! [cxx11-init] + QCommandLineParser parser; + parser.addOption({"verbose", "Verbose mode. Prints out more information."}); + //! [cxx11-init] + } + { + //! [cxx11-init-list] + QCommandLineParser parser; + parser.addOption({{"o", "output"}, "Write generated data into <file>.", "file"}); + //! [cxx11-init-list] + } } diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qcommandlineparser.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qcommandlineparser.cpp index cc22ba88ced..1b9128c22bc 100644 --- a/src/corelib/doc/snippets/code/src_corelib_tools_qcommandlineparser.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qcommandlineparser.cpp @@ -1,93 +1,92 @@ // Copyright (C) 2016 David Faure <faure@kde.org> // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -#include <qcommandlineparser.h> +#include <QCommandLineParser> int main(int argc, char **argv) { - -{ -QCommandLineParser parser; -//! [0] -bool verbose = parser.isSet("verbose"); -//! [0] -} - -{ -//! [1] -QCoreApplication app(argc, argv); -QCommandLineParser parser; -QCommandLineOption verboseOption("verbose"); -parser.addOption(verboseOption); -parser.process(app); -bool verbose = parser.isSet(verboseOption); -//! [1] -} - -{ -QCommandLineParser parser; -//! [2] -// Usage: image-editor file -// -// Arguments: -// file The file to open. -parser.addPositionalArgument("file", QCoreApplication::translate("main", "The file to open.")); - -// Usage: web-browser [urls...] -// -// Arguments: -// urls URLs to open, optionally. -parser.addPositionalArgument("urls", QCoreApplication::translate("main", "URLs to open, optionally."), "[urls...]"); - -// Usage: cp source destination -// -// Arguments: -// source Source file to copy. -// destination Destination directory. -parser.addPositionalArgument("source", QCoreApplication::translate("main", "Source file to copy.")); -parser.addPositionalArgument("destination", QCoreApplication::translate("main", "Destination directory.")); -//! [2] -} - -{ -//! [3] -QCoreApplication app(argc, argv); -QCommandLineParser parser; - -parser.addPositionalArgument("command", "The command to execute."); - -// Call parse() to find out the positional arguments. -parser.parse(QCoreApplication::arguments()); - -const QStringList args = parser.positionalArguments(); -const QString command = args.isEmpty() ? QString() : args.first(); -if (command == "resize") { - parser.clearPositionalArguments(); - parser.addPositionalArgument("resize", "Resize the object to a new size.", "resize [resize_options]"); - parser.addOption(QCommandLineOption("size", "New size.", "new_size")); - parser.process(app); - // ... -} - -/* -This code results in context-dependent help: - -$ tool --help -Usage: tool command - -Arguments: - command The command to execute. - -$ tool resize --help -Usage: tool resize [resize_options] - -Options: - --size <size> New size. - -Arguments: - resize Resize the object to a new size. -*/ -//! [3] -} - + { + QCommandLineParser parser; + + //! [0] + bool verbose = parser.isSet("verbose"); + //! [0] + } + + { + //! [1] + QCoreApplication app(argc, argv); + QCommandLineParser parser; + QCommandLineOption verboseOption("verbose"); + parser.addOption(verboseOption); + parser.process(app); + bool verbose = parser.isSet(verboseOption); + //! [1] + } + + { + QCommandLineParser parser; + //! [2] + // Usage: image-editor file + // + // Arguments: + // file The file to open. + parser.addPositionalArgument("file", QCoreApplication::translate("main", "The file to open.")); + + // Usage: web-browser [urls...] + // + // Arguments: + // urls URLs to open, optionally. + parser.addPositionalArgument("urls", QCoreApplication::translate("main", "URLs to open, optionally."), "[urls...]"); + + // Usage: cp source destination + // + // Arguments: + // source Source file to copy. + // destination Destination directory. + parser.addPositionalArgument("source", QCoreApplication::translate("main", "Source file to copy.")); + parser.addPositionalArgument("destination", QCoreApplication::translate("main", "Destination directory.")); + //! [2] + } + + { + //! [3] + QCoreApplication app(argc, argv); + QCommandLineParser parser; + + parser.addPositionalArgument("command", "The command to execute."); + + // Call parse() to find out the positional arguments. + parser.parse(QCoreApplication::arguments()); + + const QStringList args = parser.positionalArguments(); + const QString command = args.isEmpty() ? QString() : args.first(); + if (command == "resize") { + parser.clearPositionalArguments(); + parser.addPositionalArgument("resize", "Resize the object to a new size.", "resize [resize_options]"); + parser.addOption(QCommandLineOption("size", "New size.", "new_size")); + parser.process(app); + // ... + } + + /* + This code results in context-dependent help: + + $ tool --help + Usage: tool command + + Arguments: + command The command to execute. + + $ tool resize --help + Usage: tool resize [resize_options] + + Options: + --size <size> New size. + + Arguments: + resize Resize the object to a new size. + */ + //! [3] + } } diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qcommandlineparser_main.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qcommandlineparser_main.cpp index 7c06e36313a..b756e9a4b4a 100644 --- a/src/corelib/doc/snippets/code/src_corelib_tools_qcommandlineparser_main.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qcommandlineparser_main.cpp @@ -1,7 +1,7 @@ // Copyright (C) 2016 David Faure <faure@kde.org> // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -#include <qcommandlineparser.h> +#include <QCommandLineParser> //! [0] int main(int argc, char *argv[]) @@ -43,22 +43,22 @@ int main(int argc, char *argv[]) QString targetDir = parser.value(targetDirectoryOption); // ... } - //! [0] -void f() { -//! [cxx11] - parser.addOptions({ - // A boolean option with a single name (-p) - {"p", - QCoreApplication::translate("main", "Show progress during copy")}, - // A boolean option with multiple names (-f, --force) - {{"f", "force"}, - QCoreApplication::translate("main", "Overwrite existing files.")}, - // An option with a value - {{"t", "target-directory"}, - QCoreApplication::translate("main", "Copy all source files into <directory>."), - QCoreApplication::translate("main", "directory")}, - }); -//! [cxx11] +void f(QCommandLineParser parser) +{ + //! [cxx11] + parser.addOptions({ + // A boolean option with a single name (-p) + {"p", + QCoreApplication::translate("main", "Show progress during copy")}, + // A boolean option with multiple names (-f, --force) + {{"f", "force"}, + QCoreApplication::translate("main", "Overwrite existing files.")}, + // An option with a value + {{"t", "target-directory"}, + QCoreApplication::translate("main", "Copy all source files into <directory>."), + QCoreApplication::translate("main", "directory")}, + }); + //! [cxx11] } diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qcontiguouscache.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qcontiguouscache.cpp index 891ec1f5493..abc3d640478 100644 --- a/src/corelib/doc/snippets/code/src_corelib_tools_qcontiguouscache.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qcontiguouscache.cpp @@ -1,10 +1,21 @@ // Copyright (C) 2018 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause +#include <QContiguousCache> + +class MyRecord {}; + +QContiguousCache<MyRecord> cache{100}; + +MyRecord &slowFetchRecord(int index) +{ + return cache[index]; +} + //! [0] -MyRecord record(int row) const +MyRecord record(int row) { - Q_ASSERT(row >= 0 && row < count()); + Q_ASSERT(row >= 0 && row < cache.count()); while (row > cache.lastIndex()) cache.append(slowFetchRecord(cache.lastIndex()+1)); @@ -15,9 +26,12 @@ MyRecord record(int row) const } //! [0] -//! [1] -QContiguousCache<int> cache(10); -cache.insert(INT_MAX, 1); // cache contains one value and has valid indexes, INT_MAX to INT_MAX -cache.append(2); // cache contains two values but does not have valid indexes. -cache.normalizeIndexes(); // cache has two values, 1 and 2. New first index will be in the range of 0 to capacity(). -//! [1] +void example() +{ + //! [1] + QContiguousCache<int> cache(10); + cache.insert(INT_MAX, 1); // cache contains one value and has valid indexes, INT_MAX to INT_MAX + cache.append(2); // cache contains two values but does not have valid indexes. + cache.normalizeIndexes(); // cache has two values, 1 and 2. New first index will be in the range of 0 to capacity(). + //! [1] +} diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qeasingcurve.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qeasingcurve.cpp index e0be0c3d9a7..fac5267acc8 100644 --- a/src/corelib/doc/snippets/code/src_corelib_tools_qeasingcurve.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qeasingcurve.cpp @@ -1,22 +1,29 @@ // Copyright (C) 2018 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause +#include <QEasingCurve> +#include <QDebug> // check if it is needed +#include <QPropertyAnimation> + //! [typedef] qreal myEasingFunction(qreal progress); //! [typedef] -//! [0] +void examples() +{ + //! [0] QEasingCurve easing(QEasingCurve::InOutQuad); for (qreal t = 0.0; t < 1.0; t += 0.1) qWarning() << "Effective progress" << t << "is" - << easing.valueForProgress(t); -//! [0] + << easing.valueForProgress(t); + //! [0] -//! [1] + //! [1] QPropertyAnimation animation; animation.setStartValue(0); animation.setEndValue(1000); animation.setDuration(1000); animation.setEasingCurve(QEasingCurve::InOutQuad); -//! [1] + //! [1] +} 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 ddb0da5e0f4..ec9e704fb17 100644 --- a/src/corelib/doc/snippets/code/src_corelib_tools_qhash.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qhash.cpp @@ -1,81 +1,105 @@ // Copyright (C) 2020 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -//! [0] -QHash<QString, int> hash; -//! [0] +#include <QHash> +#include <QHashIterator> +#include <iostream> +#include <QDate> +using namespace std; -//! [1] -hash["one"] = 1; -hash["three"] = 3; -hash["seven"] = 7; -//! [1] - - -//! [2] -hash.insert("twelve", 12); -//! [2] +void examples() +{ + //! [0] + QHash<QString, int> hash; + //! [0] + + { + //! [1] + hash["one"] = 1; + hash["three"] = 3; + hash["seven"] = 7; + //! [1] + } + { + //! [2] + hash.insert("twelve", 12); + //! [2] + } -//! [3] -int num1 = hash["thirteen"]; -int num2 = hash.value("thirteen"); -//! [3] + { + //! [3] + int num1 = hash["thirteen"]; + int num2 = hash.value("thirteen"); + //! [3] + } + { + //! [4] + int timeout = 30; + if (hash.contains("TIMEOUT")) + timeout = hash.value("TIMEOUT"); + //! [4] + } -//! [4] -int timeout = 30; -if (hash.contains("TIMEOUT")) - timeout = hash.value("TIMEOUT"); -//! [4] + { + //! [5] + int timeout = hash.value("TIMEOUT", 30); + //! [5] + } + { + //! [7] + QHashIterator<QString, int> i(hash); + while (i.hasNext()) { + i.next(); + cout << qPrintable(i.key()) << ": " << i.value() << endl; + } + //! [7] + } -//! [5] -int timeout = hash.value("TIMEOUT", 30); -//! [5] + { + //! [8] + for (auto i = hash.cbegin(), end = hash.cend(); i != end; ++i) + cout << qPrintable(i.key()) << ": " << i.value() << endl; + //! [8] + } + { + //! [9] + hash.insert("plenty", 100); + hash.insert("plenty", 2000); + // hash.value("plenty") == 2000 + //! [9] + } -//! [6] -// WRONG -QHash<int, QWidget *> hash; -... -for (int i = 0; i < 1000; ++i) { - if (hash[i] == okButton) - cout << "Found button at index " << i << endl; + { + //! [12] + QHash<QString, int> hash; + //... + for (int value : std::as_const(hash)) + cout << value << endl; + //! [12] + } } -//! [6] +#if __has_include(<QWidget>) +#include <QWidget> -//! [7] -QHashIterator<QString, int> i(hash); -while (i.hasNext()) { - i.next(); - cout << qPrintable(i.key()) << ": " << i.value() << endl; +void widget_example(QWidget *okButton) +{ + //! [6] + // WRONG + QHash<int, QWidget *> hash; + //... + for (int i = 0; i < 1000; ++i) { + if (hash[i] == okButton) + cout << "Found button at index " << i << endl; + } + //! [6] } -//! [7] - - -//! [8] -for (auto i = hash.cbegin(), end = hash.cend(); i != end; ++i) - cout << qPrintable(i.key()) << ": " << i.value() << endl; -//! [8] - - -//! [9] -hash.insert("plenty", 100); -hash.insert("plenty", 2000); -// hash.value("plenty") == 2000 -//! [9] - - -//! [12] -QHash<QString, int> hash; -... -for (int value : std::as_const(hash)) - cout << value << endl; -//! [12] - +#endif //! [13] #ifndef EMPLOYEE_H @@ -86,7 +110,9 @@ class Employee public: Employee() {} Employee(const QString &name, QDate dateOfBirth); - ... + QString name() const { return myName; } + QDate dateOfBirth() const { return myDateOfBirth; } + //... private: QString myName; @@ -108,139 +134,186 @@ inline size_t qHash(const Employee &key, size_t seed) //! [13] -//! [14] -QHash<QString, int> hash; -hash.reserve(20000); -for (int i = 0; i < 20000; ++i) - hash.insert(keys[i], values[i]); -//! [14] - - -//! [15] -QHash<QObject *, int> objectHash; -... -QHash<QObject *, int>::iterator i = objectHash.find(obj); -while (i != objectHash.end() && i.key() == obj) { - if (i.value() == 0) { - i = objectHash.erase(i); - } else { - ++i; - } -} -//! [15] -//! [15multihash] -QMultiHash<QObject *, int> objectHash; -... -QMultiHash<QObject *, int>::iterator i = objectHash.find(obj); -while (i != objectHash.end() && i.key() == obj) { - if (i.value() == 0) { - i = objectHash.erase(i); - } else { - ++i; +void wrap() +{ + { + QString keys[1]; + int values[1]; + + //! [14] + QHash<QString, int> hash; + hash.reserve(20000); + for (int i = 0; i < 20000; ++i) + hash.insert(keys[i], values[i]); + //! [14] } -} -//! [15multihash] + { + QObject *obj; + + //! [15] + QHash<QObject *, int> objectHash; + //... + QHash<QObject *, int>::iterator i = objectHash.find(obj); + while (i != objectHash.end() && i.key() == obj) { + if (i.value() == 0) { + i = objectHash.erase(i); + } else { + ++i; + } + } + //! [15] + } -//! [16] -QHash<QString, int> hash; -... -QHash<QString, int>::const_iterator i = hash.find("HDR"); -while (i != hash.end() && i.key() == "HDR") { - cout << i.value() << endl; - ++i; -} -//! [16] + { + QObject *obj; + + //! [15multihash] + QMultiHash<QObject *, int> objectHash; + //... + QMultiHash<QObject *, int>::iterator i = objectHash.find(obj); + while (i != objectHash.end() && i.key() == obj) { + if (i.value() == 0) { + i = objectHash.erase(i); + } else { + ++i; + } + } + //! [15multihash] + } + { + //! [16] + QHash<QString, int> hash; + //... + QHash<QString, int>::const_iterator i = hash.find("HDR"); + while (i != hash.end() && i.key() == "HDR") { + cout << i.value() << endl; + ++i; + } + //! [16] + } -//! [17] -QHash<QString, int> hash; -hash.insert("January", 1); -hash.insert("February", 2); -... -hash.insert("December", 12); + { + //! [17] + QHash<QString, int> hash; + hash.insert("January", 1); + hash.insert("February", 2); + //... + hash.insert("December", 12); + + for (auto i = hash.cbegin(), end = hash.cend(); i != end; ++i) + cout << qPrintable(i.key()) << ": " << i.value() << endl; + //! [17] + } -for (auto i = hash.cbegin(), end = hash.cend(); i != end; ++i) - cout << qPrintable(key()) << ": " << i.value() << endl; -//! [17] + { + QHash<QString, int> hash; + //! [18] + for (auto i = hash.begin(), end = hash.end(); i != end; ++i) + i.value() += 2; + //! [18] + } -//! [18] -for (auto i = hash.begin(), end = hash.end(); i != end; ++i) - i.value() += 2; -//! [18] + { + QHash<QString, int> hash; -//! [21] -erase_if(hash, [](const QHash<QString, int>::iterator it) { return it.value() > 10; }); -//! [21] -} + //! [21] + erase_if(hash, [](const QHash<QString, int>::iterator it) { return it.value() > 10; }); + //! [21] + } -//! [22] -if (i.key() == "Hello") - i.value() = "Bonjour"; -//! [22] + { + QHash<QString, QString> hash; + auto i = hash.begin(); + //! [22] + if (i.key() == "Hello") + i.value() = "Bonjour"; + //! [22] + } -//! [23] -QHash<QString, int> hash; -hash.insert("January", 1); -hash.insert("February", 2); -... -hash.insert("December", 12); + { + //! [23] + QHash<QString, int> hash; + hash.insert("January", 1); + hash.insert("February", 2); + //... + hash.insert("December", 12); + + for (auto i = hash.cbegin(), end = hash.cend(); i != end; ++i) + cout << qPrintable(i.key()) << ": " << i.value() << endl; + //! [23] + } -for (auto i = hash.cbegin(), end = hash.cend(); i != end; ++i) - cout << qPrintable(i.key()) << ": " << i.value() << endl; -//! [23] + { + //! [24] + QMultiHash<QString, int> hash1, hash2, hash3; + hash1.insert("plenty", 100); + hash1.insert("plenty", 2000); + // hash1.size() == 2 -//! [24] -QMultiHash<QString, int> hash1, hash2, hash3; + hash2.insert("plenty", 5000); + // hash2.size() == 1 -hash1.insert("plenty", 100); -hash1.insert("plenty", 2000); -// hash1.size() == 2 + hash3 = hash1 + hash2; + // hash3.size() == 3 + //! [24] + } -hash2.insert("plenty", 5000); -// hash2.size() == 1 + { + QMultiHash<QString, int> hash; -hash3 = hash1 + hash2; -// hash3.size() == 3 -//! [24] + //! [25] + QList<int> values = hash.values("plenty"); + for (auto i : std::as_const(values)) + cout << i << endl; + //! [25] + } + { + QMultiHash<QString, int> hash; -//! [25] -QList<int> values = hash.values("plenty"); -for (auto i : std::as_const(values)) - cout << i << endl; -//! [25] + //! [26] + auto i = hash.constFind("plenty"); + while (i != hash.cend() && i.key() == "plenty") { + cout << i.value() << endl; + ++i; + } + //! [26] + } + { + QMultiHash<int, QString> hash; -//! [26] -auto i = hash.constFind("plenty"); -while (i != hash.cend() && i.key() == "plenty") { - cout << i.value() << endl; - ++i; -} -//! [26] + //! [27] + for (auto it = hash.cbegin(), end = hash.cend(); it != end; ++it) { + cout << "The key: " << it.key() << endl; + cout << "The value: " << qPrintable(it.value()) << endl; + cout << "Also the value: " << qPrintable(*it) << endl; + } + //! [27] + } -//! [27] -for (auto it = hash.cbegin(), end = hash.cend(); it != end; ++it) { - cout << "The key: " << it.key() << endl; - cout << "The value: " << qPrintable(it.value()) << endl; - cout << "Also the value: " << qPrintable(*it) << endl; + { + QHash<int, int> hash; + QHash<QObject *, int> hash2; + auto isPrimeNumber = [](int num) { return true; }; + + //! [28] + // Inefficient, keys() is expensive + QList<int> keys = hash.keys(); + int numPrimes = std::count_if(keys.cbegin(), keys.cend(), isPrimeNumber); + qDeleteAll(hash2.keys()); + + // Efficient, no memory allocation needed + int primeNums = std::count_if(hash.keyBegin(), hash.keyEnd(), isPrimeNumber); + qDeleteAll(hash2.keyBegin(), hash2.keyEnd()); + //! [28] + } } -//! [27] - -//! [28] -// Inefficient, keys() is expensive -QList<int> keys = hash.keys(); -int numPrimes = std::count_if(keys.cbegin(), keys.cend(), isPrimeNumber); -qDeleteAll(hash2.keys()); - -// Efficient, no memory allocation needed -int numPrimes = std::count_if(hash.keyBegin(), hash.keyEnd(), isPrimeNumber); -qDeleteAll(hash2.keyBegin(), hash2.keyEnd()); -//! [28] //! [qhashbits] inline size_t qHash(const std::vector<int> &key, size_t seed = 0) @@ -252,20 +325,27 @@ inline size_t qHash(const std::vector<int> &key, size_t seed = 0) } //! [qhashbits] -//! [qhashrange] -inline size_t qHash(const std::vector<int> &key, size_t seed = 0) +namespace Repetition_1 { - return qHashRange(key.begin(), key.end(), seed); + //! [qhashrange] + inline size_t qHash(const std::vector<int> &key, size_t seed = 0) + { + return qHashRange(key.begin(), key.end(), seed); + } + //! [qhashrange] } -//! [qhashrange] -//! [qhashrangecommutative] -inline size_t qHash(const std::unordered_set<int> &key, size_t seed = 0) +namespace Repetition_2 { - return qHashRangeCommutative(key.begin(), key.end(), seed); + //! [qhashrangecommutative] + inline size_t qHash(const std::unordered_set<int> &key, size_t seed = 0) + { + return qHashRangeCommutative(key.begin(), key.end(), seed); + } + //! [qhashrangecommutative] } -//! [qhashrangecommutative] +#if TEXT //! [30] {0, 1, 2} //! [30] @@ -273,47 +353,60 @@ inline size_t qHash(const std::unordered_set<int> &key, size_t seed = 0) //! [31] {1, 2, 0} //! [31] +#endif -//! [32] -size_t qHash(K key, size_t seed); -size_t qHash(const K &key, size_t seed); - -size_t qHash(K key); // deprecated, do not use -size_t qHash(const K &key); // deprecated, do not use -//! [32] +struct K {}; -//! [33] -namespace std { -template <> struct hash<K> +void snippets() { - // seed is optional - size_t operator()(const K &key, size_t seed = 0) const; -}; -} -//! [33] + { + //! [32] + size_t qHash(K key, size_t seed); + size_t qHash(const K &key, size_t seed); + + size_t qHash(K key); // deprecated, do not use + size_t qHash(const K &key); // deprecated, do not use + //! [32] + } -//! [34] -QHash<QString, int> hash; -hash.insert("January", 1); -hash.insert("February", 2); -// ... -hash.insert("December", 12); + { + //! [34] + QHash<QString, int> hash; + hash.insert("January", 1); + hash.insert("February", 2); + // ... + hash.insert("December", 12); + + for (auto [key, value] : hash.asKeyValueRange()) { + cout << qPrintable(key) << ": " << value << endl; + --value; // convert to JS month indexing + } + //! [34] + } -for (auto [key, value] : hash.asKeyValueRange()) { - cout << qPrintable(key) << ": " << value << endl; - --value; // convert to JS month indexing + { + //! [35] + QMultiHash<QString, int> hash; + hash.insert("January", 1); + hash.insert("February", 2); + // ... + hash.insert("December", 12); + + for (auto [key, value] : hash.asKeyValueRange()) { + cout << qPrintable(key) << ": " << value << endl; + --value; // convert to JS month indexing + } + //! [35] + } } -//! [34] - -//! [35] -QMultiHash<QString, int> hash; -hash.insert("January", 1); -hash.insert("February", 2); -// ... -hash.insert("December", 12); - -for (auto [key, value] : hash.asKeyValueRange()) { - cout << qPrintable(key) << ": " << value << endl; - --value; // convert to JS month indexing + +//! [33] +namespace std +{ + template <> struct hash<K> + { + // seed is optional + size_t operator()(const K &key, size_t seed = 0) const; + }; } -//! [35] +//! [33] diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qlist.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qlist.cpp index 499e8fe480e..ebf2512d3fc 100644 --- a/src/corelib/doc/snippets/code/src_corelib_tools_qlist.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qlist.cpp @@ -1,142 +1,176 @@ // Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -//! [0] -QList<int> integerList; -QList<QString> stringList; -//! [0] - - -//! [1] -QList<QString> list(200); -//! [1] - - -//! [2] -QList<QString> list(200, "Pass"); -//! [2] - - -//! [3] -if (list[0] == "Liz") - list[0] = "Elizabeth"; -//! [3] - - -//! [4] -for (qsizetype i = 0; i < list.size(); ++i) { - if (list.at(i) == "Alfonso") - cout << "Found Alfonso at position " << i << endl; +#include <QList> +#include <iostream> +#include <QDebug> + +using namespace std; + +void examples() +{ + { + //! [0] + QList<int> integerList; + QList<QString> stringList; + //! [0] + } + + { + //! [1] + QList<QString> list(200); + //! [1] + } + + { + //! [2] + QList<QString> list(200, "Pass"); + //! [2] + } + + { + QList<QString> list; + + //! [3] + if (list[0] == "Liz") + list[0] = "Elizabeth"; + //! [3] + } + + { + QList<QString> list; + + //! [4] + for (qsizetype i = 0; i < list.size(); ++i) { + if (list.at(i) == "Alfonso") + cout << "Found Alfonso at position " << i << endl; + } + //! [4] + } + + { + QList<QString> list; + + //! [5] + qsizetype i = list.indexOf("Harumi"); + if (i != -1) + cout << "First occurrence of Harumi is at position " << i << endl; + //! [5] + } + + { + //! [6] + QList<int> list(10); + int *data = list.data(); + for (qsizetype i = 0; i < 10; ++i) + data[i] = 2 * i; + //! [6] + } + + { + //! [7] + QList<QString> list; + list.append("one"); + list.append("two"); + QString three = "three"; + list.append(three); + // list: ["one", "two", "three"] + // three: "three" + //! [7] + } + + { + //! [move-append] + QList<QString> list; + list.append("one"); + list.append("two"); + QString three = "three"; + list.append(std::move(three)); + // list: ["one", "two", "three"] + // three: "" + //! [move-append] + } + + { + //! [emplace] + QList<QString> list{"a", "ccc"}; + list.emplace(1, 2, 'b'); + // list: ["a", "bb", "ccc"] + //! [emplace] + } + + { + //! [emplace-back] + QList<QString> list{"one", "two"}; + list.emplaceBack(3, 'a'); + qDebug() << list; + // list: ["one", "two", "aaa"] + //! [emplace-back] + } + + { + //! [emplace-back-ref] + QList<QString> list; + auto &ref = list.emplaceBack(); + ref = "one"; + // list: ["one"] + //! [emplace-back-ref] + } + + { + //! [8] + QList<QString> list; + list.prepend("one"); + list.prepend("two"); + list.prepend("three"); + // list: ["three", "two", "one"] + //! [8] + } + + { + //! [9] + QList<QString> list = {"alpha", "beta", "delta"}; + list.insert(2, "gamma"); + // list: ["alpha", "beta", "gamma", "delta"] + //! [9] + } + + { + //! [10] + QList<double> list = {2.718, 1.442, 0.4342}; + list.insert(1, 3, 9.9); + // list: [2.718, 9.9, 9.9, 9.9, 1.442, 0.4342] + //! [10] + } + + { + //! [11] + QList<QString> list(3); + list.fill("Yes"); + // list: ["Yes", "Yes", "Yes"] + + list.fill("oh", 5); + // list: ["oh", "oh", "oh", "oh", "oh"] + //! [11] + } + + { + //! [12] + QList<QString> list{"A", "B", "C", "B", "A"}; + list.indexOf("B"); // returns 1 + list.indexOf("B", 1); // returns 1 + list.indexOf("B", 2); // returns 3 + list.indexOf("X"); // returns -1 + //! [12] + } + + { + //! [13] + QList<QString> list = {"A", "B", "C", "B", "A"}; + list.lastIndexOf("B"); // returns 3 + list.lastIndexOf("B", 3); // returns 3 + list.lastIndexOf("B", 2); // returns 1 + list.lastIndexOf("X"); // returns -1 + //! [13] + } } -//! [4] - - -//! [5] -qsizetype i = list.indexOf("Harumi"); -if (i != -1) - cout << "First occurrence of Harumi is at position " << i << endl; -//! [5] - - -//! [6] -QList<int> list(10); -int *data = list.data(); -for (qsizetype i = 0; i < 10; ++i) - data[i] = 2 * i; -//! [6] - - -//! [7] -QList<QString> list; -list.append("one"); -list.append("two"); -QString three = "three"; -list.append(three); -// list: ["one", "two", "three"] -// three: "three" -//! [7] - - -//! [move-append] -QList<QString> list; -list.append("one"); -list.append("two"); -QString three = "three"; -list.append(std::move(three)); -// list: ["one", "two", "three"] -// three: "" -//! [move-append] - - -//! [emplace] -QList<QString> list{"a", "ccc"}; -list.emplace(1, 2, 'b'); -// list: ["a", "bb", "ccc"] -//! [emplace] - - -//! [emplace-back] -QList<QString> list{"one", "two"}; -list.emplaceBack(3, 'a'); -qDebug() << list; -// list: ["one", "two", "aaa"] -//! [emplace-back] - - -//! [emplace-back-ref] -QList<QString> list; -auto &ref = list.emplaceBack(); -ref = "one"; -// list: ["one"] -//! [emplace-back-ref] - - -//! [8] -QList<QString> list; -list.prepend("one"); -list.prepend("two"); -list.prepend("three"); -// list: ["three", "two", "one"] -//! [8] - - -//! [9] -QList<QString> list = {"alpha", "beta", "delta"}; -list.insert(2, "gamma"); -// list: ["alpha", "beta", "gamma", "delta"] -//! [9] - - -//! [10] -QList<double> list = {2.718, 1.442, 0.4342}; -list.insert(1, 3, 9.9); -// list: [2.718, 9.9, 9.9, 9.9, 1.442, 0.4342] -//! [10] - - -//! [11] -QList<QString> list(3); -list.fill("Yes"); -// list: ["Yes", "Yes", "Yes"] - -list.fill("oh", 5); -// list: ["oh", "oh", "oh", "oh", "oh"] -//! [11] - - -//! [12] -QList<QString> list{"A", "B", "C", "B", "A"}; -list.indexOf("B"); // returns 1 -list.indexOf("B", 1); // returns 1 -list.indexOf("B", 2); // returns 3 -list.indexOf("X"); // returns -1 -//! [12] - - -//! [13] -QList<QString> list = {"A", "B", "C", "B", "A"}; -list.lastIndexOf("B"); // returns 3 -list.lastIndexOf("B", 3); // returns 3 -list.lastIndexOf("B", 2); // returns 1 -list.lastIndexOf("X"); // returns -1 -//! [13] diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qmap.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qmap.cpp index 5f872119683..a7df32aa943 100644 --- a/src/corelib/doc/snippets/code/src_corelib_tools_qmap.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qmap.cpp @@ -1,204 +1,260 @@ // Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -//! [0] -QMap<QString, int> map; -//! [0] +#include <QMap> +#include <QMapIterator> +#include <iostream> +#include <QDate> +using namespace std; -//! [1] -map["one"] = 1; -map["three"] = 3; -map["seven"] = 7; -//! [1] - - -//! [2] -map.insert("twelve", 12); -//! [2] - - -//! [3] -int num1 = map["thirteen"]; -int num2 = map.value("thirteen"); -//! [3] - - -//! [4] -int timeout = 30; -if (map.contains("TIMEOUT")) - timeout = map.value("TIMEOUT"); -//! [4] - - -//! [5] -int timeout = map.value("TIMEOUT", 30); -//! [5] - - -//! [6] -// WRONG -QMap<int, QWidget *> map; -... -for (int i = 0; i < 1000; ++i) { - if (map[i] == okButton) - cout << "Found button at index " << i << endl; -} -//! [6] - - -//! [7] -QMapIterator<QString, int> i(map); -while (i.hasNext()) { - i.next(); - cout << qPrintable(i.key()) << ": " << i.value() << endl; -} -//! [7] - - -//! [8] -for (auto i = map.cbegin(), end = map.cend(); i != end; ++i) - cout << qPrintable(i.key()) << ": " << i.value() << endl; -//! [8] - - -//! [9] -map.insert("plenty", 100); -map.insert("plenty", 2000); -// map.value("plenty") == 2000 -//! [9] - - -//! [12] -QMap<QString, int> map; -... -for (int value : std::as_const(map)) - cout << value << endl; -//! [12] - - -//! [13] -#ifndef EMPLOYEE_H -#define EMPLOYEE_H - -class Employee +void examples() { -public: - Employee() {} - Employee(const QString &name, QDate dateOfBirth); - ... + //! [0] + QMap<QString, int> map; + //! [0] + + { + //! [1] + map["one"] = 1; + map["three"] = 3; + map["seven"] = 7; + //! [1] + } + + { + //! [2] + map.insert("twelve", 12); + //! [2] + } + + { + //! [3] + int num1 = map["thirteen"]; + int num2 = map.value("thirteen"); + //! [3] + } + + { + //! [4] + int timeout = 30; + if (map.contains("TIMEOUT")) + timeout = map.value("TIMEOUT"); + //! [4] + } + + { + //! [5] + int timeout = map.value("TIMEOUT", 30); + //! [5] + } + + { + //! [7] + QMapIterator<QString, int> i(map); + while (i.hasNext()) { + i.next(); + cout << qPrintable(i.key()) << ": " << i.value() << endl; + } + //! [7] + } + + { + //! [8] + for (auto i = map.cbegin(), end = map.cend(); i != end; ++i) + cout << qPrintable(i.key()) << ": " << i.value() << endl; + //! [8] + } + + { + //! [9] + map.insert("plenty", 100); + map.insert("plenty", 2000); + // map.value("plenty") == 2000 + //! [9] + } + + { + //! [12] + QMap<QString, int> map; + //... + for (int value : std::as_const(map)) + cout << value << endl; + //! [12] + } +} -private: - QString myName; - QDate myDateOfBirth; -}; +#if __has_include(<QWidget>) +#include <QWidget> -inline bool operator<(const Employee &e1, const Employee &e2) +void widget_example(QWidget *okButton) { - if (e1.name() != e2.name()) - return e1.name() < e2.name(); - return e1.dateOfBirth() < e2.dateOfBirth(); + //! [6] + // WRONG + QMap<int, QWidget *> map; + //... + for (int i = 0; i < 1000; ++i) { + if (map[i] == okButton) + cout << "Found button at index " << i << endl; + } + //! [6] } - -#endif // EMPLOYEE_H -//! [13] - - -//! [17] -QMap<int, QString> map; -map.insert(1, "one"); -map.insert(5, "five"); -map.insert(10, "ten"); - -map.upperBound(0); // returns iterator to (1, "one") -map.upperBound(1); // returns iterator to (5, "five") -map.upperBound(2); // returns iterator to (5, "five") -map.upperBound(10); // returns end() -map.upperBound(999); // returns end() -//! [17] +#endif -//! [18] -QMap<QString, int> map; -map.insert("January", 1); -map.insert("February", 2); -... -map.insert("December", 12); + //! [13] + #ifndef EMPLOYEE_H + #define EMPLOYEE_H -for (auto i = map.cbegin(), end = map.cend(); i != end; ++i) - cout << qPrintable(i.key()) << ": " << i.value() << endl; -//! [18] + class Employee + { + public: + Employee() {} + Employee(const QString &name, QDate dateOfBirth); + QString name() const { return myName; } + QDate dateOfBirth() const { return myDateOfBirth; } + //... + private: + QString myName; + QDate myDateOfBirth; + }; -//! [19] -for (auto i = map.begin(), end = map.end(); i != end; ++i) - i.value() += 2; -//! [19] + inline bool operator<(const Employee &e1, const Employee &e2) + { + if (e1.name() != e2.name()) + return e1.name() < e2.name(); + return e1.dateOfBirth() < e2.dateOfBirth(); + } + #endif // EMPLOYEE_H + //! [13] void erase() { -QMap<QString, int> map; -//! [20] -QMap<QString, int>::const_iterator i = map.cbegin(); -while (i != map.cend()) { - if (i.value() > 10) - i = map.erase(i); - else - ++i; -} -//! [20] -//! [21] -erase_if(map, [](const QMap<QString, int>::iterator it) { return it.value() > 10; }); -//! [21] -} - -//! [23] -if (i.key() == "Hello") - i.value() = "Bonjour"; -//! [23] - - -//! [24] -QMap<QString, int> map; -map.insert("January", 1); -map.insert("February", 2); -... -map.insert("December", 12); - -for (auto i = map.cbegin(), end = map.cend(); i != end; ++i) - cout << qPrintable(i.key()) << ": " << i.value() << endl; -//! [24] - - -//! [keyiterator1] -for (QMap<int, QString>::const_iterator it = map.cbegin(), end = map.cend(); it != end; ++it) { - cout << "The key: " << it.key() << endl; - cout << "The value: " << qPrintable(it.value()) << endl; - cout << "Also the value: " << qPrintable(*it) << endl; -} -//! [keyiterator1] - -//! [keyiterator2] -// Inefficient, keys() is expensive -QList<int> keys = map.keys(); -int numPrimes = std::count_if(map.cbegin(), map.cend(), isPrimeNumber); -qDeleteAll(map2.keys()); - -// Efficient, no memory allocation needed -int numPrimes = std::count_if(map.keyBegin(), map.keyEnd(), isPrimeNumber); -qDeleteAll(map2.keyBegin(), map2.keyEnd()); -//! [keyiterator2] - -//! [28] -QMap<QString, int> map; -map.insert("January", 1); -map.insert("February", 2); -// ... -map.insert("December", 12); - -for (auto [key, value] : map.asKeyValueRange()) { - cout << qPrintable(key) << ": " << value << endl; - --value; // convert to JS month indexing + { + //! [17] + QMap<int, QString> map; + map.insert(1, "one"); + map.insert(5, "five"); + map.insert(10, "ten"); + + map.upperBound(0); // returns iterator to (1, "one") + map.upperBound(1); // returns iterator to (5, "five") + map.upperBound(2); // returns iterator to (5, "five") + map.upperBound(10); // returns end() + map.upperBound(999); // returns end() + //! [17] + } + + { + //! [18] + QMap<QString, int> map; + map.insert("January", 1); + map.insert("February", 2); + //... + map.insert("December", 12); + + for (auto i = map.cbegin(), end = map.cend(); i != end; ++i) + cout << qPrintable(i.key()) << ": " << i.value() << endl; + //! [18] + } + + { + QMap<QString, int> map; + + //! [19] + for (auto i = map.begin(), end = map.end(); i != end; ++i) + i.value() += 2; + //! [19] + } + + { + QMap<QString, int> map; + + //! [20] + QMap<QString, int>::const_iterator i = map.cbegin(); + while (i != map.cend()) { + if (i.value() > 10) + i = map.erase(i); + else + ++i; + } + //! [20] + } + + { + QMap<QString, int> map; + + //! [21] + erase_if(map, [](const QMap<QString, int>::iterator it) { return it.value() > 10; }); + //! [21] + } + + { + QMap<QString, QString> map; + auto i = map.begin(); + + //! [23] + if (i.key() == "Hello") + i.value() = "Bonjour"; + //! [23] + } + + { + //! [24] + QMap<QString, int> map; + map.insert("January", 1); + map.insert("February", 2); + //... + map.insert("December", 12); + + for (auto i = map.cbegin(), end = map.cend(); i != end; ++i) + cout << qPrintable(i.key()) << ": " << i.value() << endl; + //! [24] + } + + { + QMap<int, QString> map; + + //! [keyiterator1] + for (QMap<int, QString>::const_iterator it = map.cbegin(), end = map.cend(); it != end; ++it) { + cout << "The key: " << it.key() << endl; + cout << "The value: " << qPrintable(it.value()) << endl; + cout << "Also the value: " << qPrintable(*it) << endl; + } + //! [keyiterator1] + } + + { + QMap<int, int> map; + QMap<QObject *, int> map2; + auto isPrimeNumber = [](int num) { return true; }; + + //! [keyiterator2] + // Inefficient, keys() is expensive + QList<int> keys = map.keys(); + int numPrimes = std::count_if(map.cbegin(), map.cend(), isPrimeNumber); + qDeleteAll(map2.keys()); + + // Efficient, no memory allocation needed + int primeNums = std::count_if(map.keyBegin(), map.keyEnd(), isPrimeNumber); + qDeleteAll(map2.keyBegin(), map2.keyEnd()); + //! [keyiterator2] + } + + { + //! [28] + QMap<QString, int> map; + map.insert("January", 1); + map.insert("February", 2); + // ... + map.insert("December", 12); + + for (auto [key, value] : map.asKeyValueRange()) { + cout << qPrintable(key) << ": " << value << endl; + --value; // convert to JS month indexing + } + //! [28] + } } -//! [28] diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qmultimap.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qmultimap.cpp index 42ec46585b5..70f7efdf944 100644 --- a/src/corelib/doc/snippets/code/src_corelib_tools_qmultimap.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qmultimap.cpp @@ -2,99 +2,122 @@ // Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -//! [0] -QMultiMap<QString, int> multimap; -//! [0] - - -//! [2] -multimap.insert("a", 1); -multimap.insert("b", 3); -multimap.insert("c", 7); -multimap.insert("c", -5); -//! [2] - - -//! [3] -int num2 = multimap.value("a"); // 1 -int num3 = multimap.value("thirteen"); // not found; 0 -int num3 = 0; -auto it = multimap.constFind("b"); -if (it != multimap.cend()) { - num3 = it.value(); -} -//! [3] - - -//! [4] -int timeout = 30; -if (multimap.contains("TIMEOUT")) - timeout = multimap.value("TIMEOUT"); - -// better: -auto it = multimap.find("TIMEOUT"); -if (it != multimap.end()) - timeout = it.value(); -//! [4] - - -//! [5] -int timeout = multimap.value("TIMEOUT", 30); -//! [5] - - -//! [7] -QMultiMapIterator<QString, int> i(multimap); -while (i.hasNext()) { - i.next(); - cout << qPrintable(i.key()) << ": " << i.value() << endl; -} -//! [7] - +#include <QMultiMap> +#include <QMultiMapIterator> +#include <iostream> +#include <QDate> -//! [8] -for (auto i = multimap.cbegin(), end = multimap.cend(); i != end; ++i) - cout << qPrintable(i.key()) << ": " << i.value() << endl; -//! [8] +using namespace std; - -//! [9] -multimap.insert("plenty", 100); -multimap.insert("plenty", 2000); -// multimap.size() == 2 -//! [9] - - -//! [10] -QList<int> values = multimap.values("plenty"); -for (auto i : std::as_const(values)) - cout << i << endl; -//! [10] - - -//! [11] -auto i = multimap.find("plenty"); -while (i != map.end() && i.key() == "plenty") { - cout << i.value() << endl; - ++i; -} - -// better: -auto [i, end] = multimap.equal_range("plenty"); -while (i != end) { - cout << i.value() << endl; - ++i; +void examples() +{ + //! [0] + QMultiMap<QString, int> multimap; + //! [0] + + { + //! [2] + multimap.insert("a", 1); + multimap.insert("b", 3); + multimap.insert("c", 7); + multimap.insert("c", -5); + //! [2] + } + + { + //! [3] + int num2 = multimap.value("a"); // 1 + int num3 = multimap.value("thirteen"); // not found; 0 + auto it = multimap.constFind("b"); + if (it != multimap.cend()) { + num3 = it.value(); + } + //! [3] + } + + { + //! [4] + int timeout = 30; + if (multimap.contains("TIMEOUT")) + timeout = multimap.value("TIMEOUT"); + + // better: + auto it = multimap.find("TIMEOUT"); + if (it != multimap.end()) + timeout = it.value(); + //! [4] + } + + { + //! [5] + int timeout = multimap.value("TIMEOUT", 30); + //! [5] + } + + { + //! [7] + QMultiMapIterator<QString, int> i(multimap); + while (i.hasNext()) { + i.next(); + cout << qPrintable(i.key()) << ": " << i.value() << endl; + } + //! [7] + } + + { + //! [8] + for (auto i = multimap.cbegin(), end = multimap.cend(); i != end; ++i) + cout << qPrintable(i.key()) << ": " << i.value() << endl; + //! [8] + } + + { + //! [9] + multimap.insert("plenty", 100); + multimap.insert("plenty", 2000); + // multimap.size() == 2 + //! [9] + } + + { + //! [10] + QList<int> values = multimap.values("plenty"); + for (auto i : std::as_const(values)) + cout << i << endl; + //! [10] + } + + { + //! [11] + auto i = multimap.find("plenty"); + while (i != multimap.end() && i.key() == "plenty") { + cout << i.value() << endl; + ++i; + } + + //! [11] + } + + { + //! [11_better] + // better: + auto [i, end] = multimap.equal_range("plenty"); + while (i != end) { + cout << i.value() << endl; + ++i; + } + //! [11_better] + } + + { + //! [12] + QMap<QString, int> multimap; + //... + for (int value : std::as_const(multimap)) + cout << value << endl; + //! [12] + } } -//! [11] - - -//! [12] -QMap<QString, int> multimap; -... -for (int value : std::as_const(multimap)) - cout << value << endl; -//! [12] - //! [13] #ifndef EMPLOYEE_H @@ -105,7 +128,9 @@ class Employee public: Employee() {} Employee(const QString &name, QDate dateOfBirth); - ... + QString name() const { return myName; } + QDate dateOfBirth() const { return myDateOfBirth; } + //... private: QString myName; @@ -123,134 +148,167 @@ inline bool operator<(const Employee &e1, const Employee &e2) //! [13] -//! [15] -QMultiMap<int, QString> multimap; -multimap.insert(1, "one"); -multimap.insert(5, "five"); -multimap.insert(5, "five (2)"); -multimap.insert(10, "ten"); - -multimap.lowerBound(0); // returns iterator to (1, "one") -multimap.lowerBound(1); // returns iterator to (1, "one") -multimap.lowerBound(2); // returns iterator to (5, "five") -multimap.lowerBound(5); // returns iterator to (5, "five") -multimap.lowerBound(6); // returns iterator to (10, "ten") -multimap.lowerBound(10); // returns iterator to (10, "ten") -multimap.lowerBound(999); // returns end() -//! [15] - - -//! [16] -QMap<QString, int> multimap; -... -QMap<QString, int>::const_iterator i = multimap.lowerBound("HDR"); -QMap<QString, int>::const_iterator upperBound = multimap.upperBound("HDR"); -while (i != upperBound) { - cout << i.value() << endl; - ++i; -} -//! [16] - - -//! [17] -QMultiMap<int, QString> multimap; -multimap.insert(1, "one"); -multimap.insert(5, "five"); -multimap.insert(5, "five (2)"); -multimap.insert(10, "ten"); - -multimap.upperBound(0); // returns iterator to (1, "one") -multimap.upperBound(1); // returns iterator to (5, "five") -multimap.upperBound(2); // returns iterator to (5, "five") -multimap.lowerBound(5); // returns iterator to (5, "five (2)") -multimap.lowerBound(6); // returns iterator to (10, "ten") -multimap.upperBound(10); // returns end() -multimap.upperBound(999); // returns end() -//! [17] - -//! [19] -for (auto it = multimap.begin(), end = multimap.end(); i != end; ++i) - i.value() += 2; -//! [19] - void erase() { -QMultiMap<QString, int> multimap; -//! [20] -QMultiMap<QString, int>::const_iterator i = multimap.cbegin(); -while (i != multimap.cend()) { - if (i.value() > 10) - i = multimap.erase(i); - else - ++i; -} -//! [20] -//! [21] -erase_if(multimap, [](const QMultiMap<QString, int>::iterator it) { return it.value() > 10; }); -//! [21] -} - - -//! [23] -if (i.key() == "Hello") - i.value() = "Bonjour"; -//! [23] - - -//! [24] -QMultiMap<QString, int> multi; -multimap.insert("January", 1); -multimap.insert("February", 2); -... -multimap.insert("December", 12); - -for (auto i = multimap.cbegin(), end = multimap.cend(); i != end; ++i) - cout << qPrintable(i.key()) << ": " << i.value() << endl; -//! [24] - - -//! [25] -QMultiMap<QString, int> map1, map2, map3; - -map1.insert("plenty", 100); -map1.insert("plenty", 2000); -// map1.size() == 2 - -map2.insert("plenty", 5000); -// map2.size() == 1 - -map3 = map1 + map2; -// map3.size() == 3 -//! [25] - -//! [keyiterator1] -for (auto it = multimap.cbegin(), end = multimap.cend(); it != end; ++it) { - cout << "The key: " << it.key() << endl - cout << "The value: " << qPrintable(it.value()) << endl; - cout << "Also the value: " << qPrintable(*it) << endl; -} -//! [keyiterator1] - -//! [keyiterator2] -// Inefficient, keys() is expensive -QList<int> keys = multimap.keys(); -int numPrimes = std::count_if(multimap.cbegin(), multimap.cend(), isPrimeNumber); -qDeleteAll(multimap2.keys()); - -// Efficient, no memory allocation needed -int numPrimes = std::count_if(multimap.keyBegin(), multimap.keyEnd(), isPrimeNumber); -qDeleteAll(multimap2.keyBegin(), multimap2.keyEnd()); -//! [keyiterator2] - -//! [26] -QMultiMap<QString, int> map; -map.insert("January", 1); -map.insert("February", 2); -// ... -map.insert("December", 12); - -for (auto [key, value] : map.asKeyValueRange()) { - cout << qPrintable(key) << ": " << value << endl; - --value; // convert to JS month indexing + { + //! [15] + QMultiMap<int, QString> multimap; + multimap.insert(1, "one"); + multimap.insert(5, "five"); + multimap.insert(5, "five (2)"); + multimap.insert(10, "ten"); + + multimap.lowerBound(0); // returns iterator to (1, "one") + multimap.lowerBound(1); // returns iterator to (1, "one") + multimap.lowerBound(2); // returns iterator to (5, "five") + multimap.lowerBound(5); // returns iterator to (5, "five") + multimap.lowerBound(6); // returns iterator to (10, "ten") + multimap.lowerBound(10); // returns iterator to (10, "ten") + multimap.lowerBound(999); // returns end() + //! [15] + } + + { + //! [16] + QMap<QString, int> multimap; + //... + QMap<QString, int>::const_iterator i = multimap.lowerBound("HDR"); + QMap<QString, int>::const_iterator upperBound = multimap.upperBound("HDR"); + while (i != upperBound) { + cout << i.value() << endl; + ++i; + } + //! [16] + } + + { + //! [17] + QMultiMap<int, QString> multimap; + multimap.insert(1, "one"); + multimap.insert(5, "five"); + multimap.insert(5, "five (2)"); + multimap.insert(10, "ten"); + + multimap.upperBound(0); // returns iterator to (1, "one") + multimap.upperBound(1); // returns iterator to (5, "five") + multimap.upperBound(2); // returns iterator to (5, "five") + multimap.lowerBound(5); // returns iterator to (5, "five (2)") + multimap.lowerBound(6); // returns iterator to (10, "ten") + multimap.upperBound(10); // returns end() + multimap.upperBound(999); // returns end() + //! [17] + } + + { + QMultiMap<QString, int> multimap; + + //! [19] + for (auto i = multimap.begin(), end = multimap.end(); i != end; ++i) + i.value() += 2; + //! [19] + } + + { + QMultiMap<QString, int> multimap; + + //! [20] + QMultiMap<QString, int>::const_iterator i = multimap.cbegin(); + while (i != multimap.cend()) { + if (i.value() > 10) + i = multimap.erase(i); + else + ++i; + } + //! [20] + } + + { + QMultiMap<QString, int> multimap; + + //! [21] + erase_if(multimap, [](const QMultiMap<QString, int>::iterator it) { return it.value() > 10; }); + //! [21] + } + + { + auto i = QMultiMap<QString, QString>::iterator(); + + //! [23] + if (i.key() == "Hello") + i.value() = "Bonjour"; + //! [23] + } + + { + //! [24] + QMultiMap<QString, int> multimap; + multimap.insert("January", 1); + multimap.insert("February", 2); + //... + multimap.insert("December", 12); + + for (auto i = multimap.cbegin(), end = multimap.cend(); i != end; ++i) + cout << qPrintable(i.key()) << ": " << i.value() << endl; + //! [24] + } + + { + //! [25] + QMultiMap<QString, int> map1, map2, map3; + + map1.insert("plenty", 100); + map1.insert("plenty", 2000); + // map1.size() == 2 + + map2.insert("plenty", 5000); + // map2.size() == 1 + + map3 = map1 + map2; + // map3.size() == 3 + //! [25] + } + + { + QMultiMap<int, QString> multimap; + + //! [keyiterator1] + for (auto it = multimap.cbegin(), end = multimap.cend(); it != end; ++it) { + cout << "The key: " << it.key() << endl; + cout << "The value: " << qPrintable(it.value()) << endl; + cout << "Also the value: " << qPrintable(*it) << endl; + } + //! [keyiterator1] + } + + { + QMultiMap<int, int> multimap; + QMultiMap<QObject *, int> multimap2; + auto isPrimeNumber = [](int num) { return true; }; + + //! [keyiterator2] + // Inefficient, keys() is expensive + QList<int> keys = multimap.keys(); + int numPrimes = std::count_if(multimap.cbegin(), multimap.cend(), isPrimeNumber); + qDeleteAll(multimap2.keys()); + + // Efficient, no memory allocation needed + int primeNums = std::count_if(multimap.keyBegin(), multimap.keyEnd(), isPrimeNumber); + qDeleteAll(multimap2.keyBegin(), multimap2.keyEnd()); + //! [keyiterator2] + } + + { + //! [26] + QMultiMap<QString, int> map; + map.insert("January", 1); + map.insert("February", 2); + // ... + map.insert("December", 12); + + for (auto [key, value] : map.asKeyValueRange()) { + cout << qPrintable(key) << ": " << value << endl; + --value; // convert to JS month indexing + } + //! [26] + } } -//! [26] diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qpoint.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qpoint.cpp index f7c06c17cc7..fe211d6d735 100644 --- a/src/corelib/doc/snippets/code/src_corelib_tools_qpoint.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qpoint.cpp @@ -1,126 +1,162 @@ // Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -//! [0] -QPoint p; +#include <QPoint> +#include <QMouseEvent> +#include <iostream> -p.setX(p.x() + 1); -p += QPoint(1, 0); -p.rx()++; -//! [0] +int x() { return 0; } +int y() { return 0; } -//! [1] -QPoint p(1, 2); -p.rx()--; // p becomes (0, 2) -//! [1] - - -//! [2] -QPoint p(1, 2); -p.ry()++; // p becomes (1, 3) -//! [2] - - -//! [3] -QPoint p( 3, 7); -QPoint q(-1, 4); -p += q; // p becomes (2, 11) -//! [3] - - -//! [4] -QPoint p( 3, 7); -QPoint q(-1, 4); -p -= q; // p becomes (4, 3) -//! [4] - - -//! [5] -QPoint p(-1, 4); -p *= 2.5; // p becomes (-3, 10) -//! [5] - - -//! [16] -QPoint p( 3, 7); -QPoint q(-1, 4); -int dotProduct = QPoint::dotProduct(p, q); // dotProduct becomes 25 -//! [16] - +void examples() +{ -//! [6] -QPoint p(-3, 10); -p /= 2.5; // p becomes (-1, 4) -//! [6] + { + //! [0] + QPoint p; + + p.setX(p.x() + 1); + p += QPoint(1, 0); + p.rx()++; + //! [0] + } + + { + //! [1] + QPoint p(1, 2); + p.rx()--; // p becomes (0, 2) + //! [1] + } + + { + //! [2] + QPoint p(1, 2); + p.ry()++; // p becomes (1, 3) + //! [2] + } + + { + //! [3] + QPoint p( 3, 7); + QPoint q(-1, 4); + p += q; // p becomes (2, 11) + //! [3] + } + + { + //! [4] + QPoint p( 3, 7); + QPoint q(-1, 4); + p -= q; // p becomes (4, 3) + //! [4] + } + + { + //! [5] + QPoint p(-1, 4); + p *= 2.5; // p becomes (-3, 10) + //! [5] + } + + { + //! [16] + QPoint p( 3, 7); + QPoint q(-1, 4); + int dotProduct = QPoint::dotProduct(p, q); // dotProduct becomes 25 + //! [16] + } + + { + //! [6] + QPoint p(-3, 10); + p /= 2.5; // p becomes (-1, 4) + //! [6] + } + + { + //! [8] + double trueLength = std::sqrt(std::pow(x(), 2) + std::pow(y(), 2)); + //! [8] + } + + { + //! [9] + QPointF p; + + p.setX(p.x() + 1.0); + p += QPointF(1.0, 0.0); + p.rx()++; + //! [9] + } + + { + //! [10] + QPointF p(1.1, 2.5); + p.rx()--; // p becomes (0.1, 2.5) + //! [10] + } + + { + //! [11] + QPointF p(1.1, 2.5); + p.ry()++; // p becomes (1.1, 3.5) + //! [11] + } + + { + //! [12] + QPointF p( 3.1, 7.1); + QPointF q(-1.0, 4.1); + p += q; // p becomes (2.1, 11.2) + //! [12] + } + + { + //! [13] + QPointF p( 3.1, 7.1); + QPointF q(-1.0, 4.1); + p -= q; // p becomes (4.1, 3.0) + //! [13] + } + + { + //! [14] + QPointF p(-1.1, 4.1); + p *= 2.5; // p becomes (-2.75, 10.25) + //! [14] + } + + { + //! [15] + QPointF p(-2.75, 10.25); + p /= 2.5; // p becomes (-1.1, 4.1) + //! [15] + } + + { + //! [17] + QPointF p( 3.1, 7.1); + QPointF q(-1.0, 4.1); + qreal dotProduct = QPointF::dotProduct(p, q); // dotProduct becomes 26.01 + //! [17] + } +} +class MyWidget +{ +public: + void mouseMoveEvent(QMouseEvent *event); +}; //! [7] QPoint oldPosition; -MyWidget::mouseMoveEvent(QMouseEvent *event) +void MyWidget::mouseMoveEvent(QMouseEvent *event) { QPoint point = event->pos() - oldPosition; - if (point.manhattanLength() > 3) + if (point.manhattanLength() > 3){ // the mouse has moved more than 3 pixels since the oldPosition + } } //! [7] - - -//! [8] -double trueLength = std::sqrt(std::pow(x(), 2) + std::pow(y(), 2)); -//! [8] - - -//! [9] -QPointF p; - -p.setX(p.x() + 1.0); -p += QPointF(1.0, 0.0); -p.rx()++; -//! [9] - - -//! [10] - QPointF p(1.1, 2.5); - p.rx()--; // p becomes (0.1, 2.5) -//! [10] - - -//! [11] -QPointF p(1.1, 2.5); -p.ry()++; // p becomes (1.1, 3.5) -//! [11] - - -//! [12] -QPointF p( 3.1, 7.1); -QPointF q(-1.0, 4.1); -p += q; // p becomes (2.1, 11.2) -//! [12] - - -//! [13] -QPointF p( 3.1, 7.1); -QPointF q(-1.0, 4.1); -p -= q; // p becomes (4.1, 3.0) -//! [13] - - -//! [14] -QPointF p(-1.1, 4.1); -p *= 2.5; // p becomes (-2.75, 10.25) -//! [14] - - -//! [15] -QPointF p(-2.75, 10.25); -p /= 2.5; // p becomes (-1.1, 4.1) -//! [15] - - -//! [17] -QPointF p( 3.1, 7.1); -QPointF q(-1.0, 4.1); -qreal dotProduct = QPointF::dotProduct(p, q); // dotProduct becomes 26.01 -//! [17] diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qqueue.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qqueue.cpp index c59ec1060ae..fa20ad73ed0 100644 --- a/src/corelib/doc/snippets/code/src_corelib_tools_qqueue.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qqueue.cpp @@ -1,11 +1,19 @@ // Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -//! [0] -QQueue<int> queue; -queue.enqueue(1); -queue.enqueue(2); -queue.enqueue(3); -while (!queue.isEmpty()) - cout << queue.dequeue() << endl; -//! [0] +#include <QQueue> +#include <iostream> + +using namespace std; + +void example() +{ + //! [0] + QQueue<int> queue; + queue.enqueue(1); + queue.enqueue(2); + queue.enqueue(3); + while (!queue.isEmpty()) + cout << queue.dequeue() << endl; + //! [0] +} diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qrect.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qrect.cpp index 403d1888065..e5de3197897 100644 --- a/src/corelib/doc/snippets/code/src_corelib_tools_qrect.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qrect.cpp @@ -1,23 +1,36 @@ // Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -//! [0] -QRect r1(100, 200, 11, 16); -QRect r2(QPoint(100, 200), QSize(11, 16)); -//! [0] +#include <QRect> +#include <QPoint> +void examples() +{ + { + //! [0] + QRect r1(100, 200, 11, 16); + QRect r2(QPoint(100, 200), QSize(11, 16)); + //! [0] + } -//! [1] -QRectF r1(100.0, 200.1, 11.2, 16.3); -QRectF r2(QPointF(100.0, 200.1), QSizeF(11.2, 16.3)); -//! [1] + { + //! [1] + QRectF r1(100.0, 200.1, 11.2, 16.3); + QRectF r2(QPointF(100.0, 200.1), QSizeF(11.2, 16.3)); + //! [1] + } -//! [2] -QRect r = {15, 51, 42, 24}; -r = r.transposed(); // r == {15, 51, 24, 42} -//! [2] + { + //! [2] + QRect r = {15, 51, 42, 24}; + r = r.transposed(); // r == {15, 51, 24, 42} + //! [2] + } -//! [3] -QRectF r = {1.5, 5.1, 4.2, 2.4}; -r = r.transposed(); // r == {1.5, 5.1, 2.4, 4.2} -//! [3] + { + //! [3] + QRectF r = {1.5, 5.1, 4.2, 2.4}; + r = r.transposed(); // r == {1.5, 5.1, 2.4, 4.2} + //! [3] + } +} diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qscopedpointer.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qscopedpointer.cpp index ee0e9eafa10..c67241dde6a 100644 --- a/src/corelib/doc/snippets/code/src_corelib_tools_qscopedpointer.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qscopedpointer.cpp @@ -1,6 +1,18 @@ // Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause +#include <QIODevice> +#include <QScopedPointer> +#include <QScopedPointerArrayDeleter> +#include <QScopedPointerPodDeleter> + +class MyClass {}; +class MySubClass : public MyClass {}; + +QIODevice *handsOverOwnership(); +void process(QIODevice *device); +int m_value; + //! [0] void myFunction(bool useSubClass) { @@ -27,58 +39,92 @@ void myFunction(bool useSubClass) } //! [0] -//! [1] -void myFunction(bool useSubClass) +namespace repetition { - // assuming that MyClass has a virtual destructor - QScopedPointer<MyClass> p(useSubClass ? new MyClass() : new MySubClass); - QScopedPointer<QIODevice> device(handsOverOwnership()); + class MyClass {}; + class MySubClass : public MyClass {}; - if (m_value > 3) - return; + //! [1] + void myFunction(bool useSubClass) + { + // assuming that MyClass has a virtual destructor + QScopedPointer<MyClass> p(useSubClass ? new MyClass() : new MySubClass); + QScopedPointer<QIODevice> device(handsOverOwnership()); + + if (m_value > 3) + return; - process(device); + process(device.data()); + } + //! [1] } -//! [1] -//! [2] - const QWidget *const p = new QWidget(); - // is equivalent to: - const QScopedPointer<const QWidget> p(new QWidget()); +#if __has_include(<QWidget>) +#include <QWidget> - QWidget *const p = new QWidget(); - // is equivalent to: - const QScopedPointer<QWidget> p(new QWidget()); +void QWidget_snippets() +{ + { + //! [2.0] + const QWidget *const p = new QWidget(); + // is equivalent to: + const QScopedPointer<const QWidget> p1(new QWidget()); - const QWidget *p = new QWidget(); - // is equivalent to: - QScopedPointer<const QWidget> p(new QWidget()); -//! [2] + //! [2.0] + } -//! [3] -if (scopedPointer) { - ... -} -//! [3] + { + //! [2.1] + QWidget *const p = new QWidget(); + // is equivalent to: + const QScopedPointer<QWidget> p1(new QWidget()); -//! [4] -class MyPrivateClass; // forward declare MyPrivateClass + //! [2.1] + } + + { + //! [2.2] + const QWidget *p = new QWidget(); + // is equivalent to: + QScopedPointer<const QWidget> p1(new QWidget()); + //! [2.2] + } + bool scopedPointer; + + //! [3] + if (scopedPointer) { + //... + } + //! [3] +} +#endif -class MyClass +namespace class_repetition { -private: - QScopedPointer<MyPrivateClass> privatePtr; // QScopedPointer to forward declared class + //! [4] + class MyPrivateClass; // forward declare MyPrivateClass -public: - MyClass(); // OK - inline ~MyClass() {} // VIOLATION - Destructor must not be inline + class MyClass + { + private: + QScopedPointer<MyPrivateClass> privatePtr; // QScopedPointer to forward declared class -private: - Q_DISABLE_COPY(MyClass) // OK - copy constructor and assignment operators - // are now disabled, so the compiler won't implicitly - // generate them. -}; -//! [4] + public: + MyClass(); // OK + inline ~MyClass() {} // VIOLATION - Destructor must not be inline + + private: + Q_DISABLE_COPY(MyClass) // OK - copy constructor and assignment operators + // are now disabled, so the compiler won't implicitly + // generate them. + }; + //! [4] + + class MyPrivateClass {}; +} + +class MyCustomClass {}; +void myCustomDeallocator(MyCustomClass *pointer) {} //! [5] // this QScopedPointer deletes its data using the delete[] operator: diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qscopeguard.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qscopeguard.cpp index 8a0d8a8012a..85f3b245b8e 100644 --- a/src/corelib/doc/snippets/code/src_corelib_tools_qscopeguard.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qscopeguard.cpp @@ -1,11 +1,15 @@ // Copyright (C) 2018 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Sérgio Martins <sergio.martins@kdab.com> // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause +#include <QScopeGuard> + +int code_that_might_throw_exceptions() { return 0; }; + //! [0] void myComplexCodeWithMultipleReturnPoints(int v) { // The lambda will be executed right before your function returns - auto cleanup = qScopeGuard([] { code you want executed goes HERE; }); + auto cleanup = qScopeGuard([] { /* code you want executed goes HERE; */ }); if (v == -1) return; @@ -15,6 +19,6 @@ void myComplexCodeWithMultipleReturnPoints(int v) if (v2 == -1) return; - (...) + //... } //! [0] diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qshareddata.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qshareddata.cpp index fa45a604546..f66f7ff05ff 100644 --- a/src/corelib/doc/snippets/code/src_corelib_tools_qshareddata.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qshareddata.cpp @@ -1,14 +1,22 @@ // Copyright (C) 2018 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause +#include <QSharedDataPointer> + //! [0] - class EmployeeData; +class EmployeeData; //! [0] +class EmployeeData +{ +public: + virtual EmployeeData *clone() const; +}; + //! [1] - template<> - EmployeeData *QSharedDataPointer<EmployeeData>::clone() - { - return d->clone(); - } +template<> +EmployeeData *QSharedDataPointer<EmployeeData>::clone() +{ + return d->clone(); +} //! [1] diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qsharedpointer.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qsharedpointer.cpp index fd0612590e9..86a208a27c9 100644 --- a/src/corelib/doc/snippets/code/src_corelib_tools_qsharedpointer.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qsharedpointer.cpp @@ -1,6 +1,11 @@ // Copyright (C) 2018 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause +#include <QSharedPointer> +#include <QWeakPointer> +#include <QObject> +#include <QDebug> + //! [0] class Y: public QEnableSharedFromThis<Y> { @@ -35,76 +40,110 @@ }; //! [1] +class MyObject : public QObject +{ + Q_OBJECT + public: + MyObject() { /* ... */ } + ~MyObject() { /* ... */ } + +//! [2] +static void doDeleteLater(MyObject *obj) +{ + obj->deleteLater(); +} + +void otherFunction() +{ + QSharedPointer<MyObject> obj = + QSharedPointer<MyObject>(new MyObject, doDeleteLater); + + // continue using obj + obj.clear(); // calls obj->deleteLater(); +} //! [2] - static void doDeleteLater(MyObject *obj) + + template <typename T> + void someFunc() + { + T *t = new T; + auto deleter = [](T *p) { delete p; }; + { + //! [6] + QSharedPointer<T> other(t); this->swap(other); + //! [6] + } + + { + //! [7] + QSharedPointer<T> other(t, deleter); this->swap(other); + //! [7] + } + } + + template <typename T> + void swap(QSharedPointer<T> &other) {} +}; + +void examples() +{ + QSharedPointer<int> sharedptr; + QWeakPointer<int> weakref; + { - obj->deleteLater(); + //! [2] + QSharedPointer<int> sharedptr(new int(42)); + //! [2] } - void otherFunction() { + //! [1] + QSharedPointer<int> sharedptr(new int(42), [](int *p) { delete p; }); + //! [1] + } + + { + //! [3] QSharedPointer<MyObject> obj = - QSharedPointer<MyObject>(new MyObject, doDeleteLater); + QSharedPointer<MyObject>(new MyObject, &QObject::deleteLater); + //! [3] + } - // continue using obj - obj.clear(); // calls obj->deleteLater(); + { + //! [4] + if (sharedptr) { /*...*/ } + //! [4] } -//! [2] -//! [3] - QSharedPointer<MyObject> obj = - QSharedPointer<MyObject>(new MyObject, &QObject::deleteLater); -//! [3] - -//! [4] - if (sharedptr) { ... } -//! [4] - -//! [5] - if (!sharedptr) { ... } -//! [5] - -//! [6] - QSharedPointer<T> other(t); this->swap(other); -//! [6] - -//! [7] - QSharedPointer<T> other(t, deleter); this->swap(other); -//! [7] - -//! [8] - if (weakref) { ... } -//! [8] - -//! [9] - if (!weakref) { ... } -//! [9] - -//! [10] - qDebug("Tracking %p", weakref.data()); -//! [10] - -//! [11] - // this pointer cannot be used in another thread - // so other threads cannot delete it - QWeakPointer<int> weakref = obtainReference(); - - Object *obj = weakref.data(); - if (obj) { - // if the pointer wasn't deleted yet, we know it can't get - // deleted by our own code here nor the functions we call - otherFunction(obj); + { + //! [5] + if (!sharedptr) { /*...*/ } + //! [5] } -//! [11] -//! [12] - QWeakPointer<int> weakref; + { + //! [8] + if (weakref) { /*...*/ } + //! [8] + } - // ... + { + //! [9] + if (!weakref) { /*...*/ } + //! [9] + } - QSharedPointer<int> strong = weakref.toStrongRef(); - if (strong) - qDebug() << "The value is:" << *strong; - else - qDebug() << "The value has already been deleted"; -//! [12] + { + //! [12] + QWeakPointer<int> weakref; + + // ... + + QSharedPointer<int> strong = weakref.toStrongRef(); + if (strong) + qDebug() << "The value is:" << *strong; + else + qDebug() << "The value has already been deleted"; + //! [12] + } +} diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qsize.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qsize.cpp index ee3b7b32685..822a51e7e9b 100644 --- a/src/corelib/doc/snippets/code/src_corelib_tools_qsize.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qsize.cpp @@ -1,99 +1,115 @@ // Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -//! [0] -QSize t1(10, 12); -t1.scale(60, 60, Qt::IgnoreAspectRatio); -// t1 is (60, 60) - -QSize t2(10, 12); -t2.scale(60, 60, Qt::KeepAspectRatio); -// t2 is (50, 60) - -QSize t3(10, 12); -t3.scale(60, 60, Qt::KeepAspectRatioByExpanding); -// t3 is (60, 72) -//! [0] - - -//! [1] -QSize size(100, 10); -size.rwidth() += 20; - -// size becomes (120,10) -//! [1] - - -//! [2] -QSize size(100, 10); -size.rheight() += 5; - -// size becomes (100,15) -//! [2] - - -//! [3] -QSize s( 3, 7); -QSize r(-1, 4); -s += r; - -// s becomes (2,11) -//! [3] - - -//! [4] -QSize s( 3, 7); -QSize r(-1, 4); -s -= r; - -// s becomes (4,3) -//! [4] - - -//! [5] -QSizeF t1(10, 12); -t1.scale(60, 60, Qt::IgnoreAspectRatio); -// t1 is (60, 60) - -QSizeF t2(10, 12); -t2.scale(60, 60, Qt::KeepAspectRatio); -// t2 is (50, 60) - -QSizeF t3(10, 12); -t3.scale(60, 60, Qt::KeepAspectRatioByExpanding); -// t3 is (60, 72) -//! [5] - - -//! [6] -QSizeF size(100.3, 10); -size.rwidth() += 20.5; - - // size becomes (120.8,10) -//! [6] - - -//! [7] -QSizeF size(100, 10.2); -size.rheight() += 5.5; - -// size becomes (100,15.7) -//! [7] - - -//! [8] -QSizeF s( 3, 7); -QSizeF r(-1, 4); -s += r; - -// s becomes (2,11) -//! [8] - - -//! [9] -QSizeF s( 3, 7); -QSizeF r(-1, 4); -s -= r; - -// s becomes (4,3) -//! [9] +#include <QSize> + +void examples() +{ + { + //! [0] + QSize t1(10, 12); + t1.scale(60, 60, Qt::IgnoreAspectRatio); + // t1 is (60, 60) + + QSize t2(10, 12); + t2.scale(60, 60, Qt::KeepAspectRatio); + // t2 is (50, 60) + + QSize t3(10, 12); + t3.scale(60, 60, Qt::KeepAspectRatioByExpanding); + // t3 is (60, 72) + //! [0] + } + + { + //! [1] + QSize size(100, 10); + size.rwidth() += 20; + + // size becomes (120,10) + //! [1] + } + + { + //! [2] + QSize size(100, 10); + size.rheight() += 5; + + // size becomes (100,15) + //! [2] + } + + { + //! [3] + QSize s( 3, 7); + QSize r(-1, 4); + s += r; + + // s becomes (2,11) + //! [3] + } + + { + //! [4] + QSize s( 3, 7); + QSize r(-1, 4); + s -= r; + + // s becomes (4,3) + //! [4] + } + + { + //! [5] + QSizeF t1(10, 12); + t1.scale(60, 60, Qt::IgnoreAspectRatio); + // t1 is (60, 60) + + QSizeF t2(10, 12); + t2.scale(60, 60, Qt::KeepAspectRatio); + // t2 is (50, 60) + + QSizeF t3(10, 12); + t3.scale(60, 60, Qt::KeepAspectRatioByExpanding); + // t3 is (60, 72) + //! [5] + } + + { + //! [6] + QSizeF size(100.3, 10); + size.rwidth() += 20.5; + + // size becomes (120.8,10) + //! [6] + } + + { + //! [7] + QSizeF size(100, 10.2); + size.rheight() += 5.5; + + // size becomes (100,15.7) + //! [7] + } + + { + //! [8] + QSizeF s( 3, 7); + QSizeF r(-1, 4); + s += r; + + // s becomes (2,11) + //! [8] + } + + { + //! [9] + QSizeF s( 3, 7); + QSizeF r(-1, 4); + s -= r; + + // s becomes (4,3) + //! [9] + } +} diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qtimeline.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qtimeline.cpp index 39bcd4faf09..2d32356eaeb 100644 --- a/src/corelib/doc/snippets/code/src_corelib_tools_qtimeline.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qtimeline.cpp @@ -1,18 +1,29 @@ // Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -//! [0] -... -progressBar = new QProgressBar(this); -progressBar->setRange(0, 100); +#include <QWidget> +#include <QProgressBar> +#include <QPushButton> +#include <QTimeLine> -// Construct a 1-second timeline with a frame range of 0 - 100 -QTimeLine *timeLine = new QTimeLine(1000, this); -timeLine->setFrameRange(0, 100); -connect(timeLine, &QTimeLine::frameChanged, progressBar, &QProgressBar::setValue); +struct MyObject : public QWidget +{ + void examples() + { + //! [0] + //... + auto progressBar = new QProgressBar(this); + progressBar->setRange(0, 100); -// Clicking the push button will start the progress bar animation -pushButton = new QPushButton(tr("Start animation"), this); -connect(pushButton, &QPushButton::clicked, timeLine, &QTimeLine::start); -... -//! [0] + // Construct a 1-second timeline with a frame range of 0 - 100 + QTimeLine *timeLine = new QTimeLine(1000, this); + timeLine->setFrameRange(0, 100); + connect(timeLine, &QTimeLine::frameChanged, progressBar, &QProgressBar::setValue); + + // Clicking the push button will start the progress bar animation + auto pushButton = new QPushButton(tr("Start animation"), this); + connect(pushButton, &QPushButton::clicked, timeLine, &QTimeLine::start); + //... + //! [0] + } +}; |
