diff options
Diffstat (limited to 'src/concurrent/doc/snippets')
-rw-r--r-- | src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentfilter.cpp | 56 | ||||
-rw-r--r-- | src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentmap.cpp | 37 |
2 files changed, 90 insertions, 3 deletions
diff --git a/src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentfilter.cpp b/src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentfilter.cpp index 3cc1fe836c..ef87a60080 100644 --- a/src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentfilter.cpp +++ b/src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentfilter.cpp @@ -158,8 +158,6 @@ struct StartsWith StartsWith(const QString &string) : m_string(string) { } - typedef bool result_type; - bool operator()(const QString &testString) { return testString.startsWith(m_string); @@ -183,3 +181,57 @@ QFuture<QString> fooString = StartsWith(QLatin1String("Foo")), StringTransform()); //! [14] + +//! [15] +// keep only even integers +QVector<int> vector { 1, 2, 3, 4 }; +QtConcurrent::blockingFilter(vector, [](int n) { return (n & 1) == 0; }); + +// retrieve only even integers +QVector<int> vector2 { 1, 2, 3, 4 }; +QFuture<int> future = QtConcurrent::filtered(vector2, [](int x) { + return (x & 1) == 0; +}); +QVector<int> results = future.results(); + +// add up all even integers +QVector<int> vector3 { 1, 2, 3, 4 }; +int sum = QtConcurrent::filteredReduced<int>(vector3, + [](int x) { + return (x & 1) == 0; + }, + [](int &sum, int x) { + sum += x; + } +); +//! [15] + +//! [16] +void intSumReduce(int &sum, int x) +{ + sum += x; +} + +QVector<int> vector { 1, 2, 3, 4 }; +int sum = QtConcurrent::filteredReduced(vector, + [] (int x) { + return (x & 1) == 0; + }, + intSumReduce +); +//! [16] + +//! [17] +bool keepEvenIntegers(int x) +{ + return (x & 1) == 0; +} + +QVector<int> vector { 1, 2, 3, 4 }; +int sum = QtConcurrent::filteredReduced<int>(vector, + keepEvenIntegers, + [](int &sum, int x) { + sum += x; + } +); +//! [17] diff --git a/src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentmap.cpp b/src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentmap.cpp index fc574302d2..dd3e0103bb 100644 --- a/src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentmap.cpp +++ b/src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentmap.cpp @@ -130,7 +130,8 @@ QFuture<void> squeezedStrings = QtConcurrent::map(strings, &QString::squeeze); // Swap the rgb values of all pixels on a list of images. QList<QImage> images = ...; -QFuture<QImage> bgrImages = QtConcurrent::mapped(images, &QImage::rgbSwapped); +QFuture<QImage> bgrImages = QtConcurrent::mapped(images, + static_cast<QImage (QImage::*)() const &>(&QImage::rgbSwapped)); // Create a set of the lengths of all strings in a list. QStringList strings = ...; @@ -197,3 +198,37 @@ struct Scaled QList<QImage> images = ...; QFuture<QImage> thumbnails = QtConcurrent::mapped(images, Scaled(100)); //! [14] + +//! [15] +QVector<int> vector { 1, 2, 3, 4 }; +QtConcurrent::blockingMap(vector, [](int &x) { x *= 2; }); + +int size = 100; +QVector<QImage> images = ...; + +QVector<QImage> thumbnails = QtConcurrent::mapped(images, + [&size](const QImage &image) { + return image.scaled(size, size); + } + ).results(); +//! [15] + +//! [16] +QVector<QImage> collage = QtConcurrent::mappedReduced(images, + [&size](const QImage &image) { + return image.scaled(size, size); + }, + addToCollage + ).results(); +//! [16] + +//! [17] +QVector<QImage> collage = QtConcurrent::mappedReduced<QImage>(images, + [&size](const QImage &image) { + return image.scaled(size, size); + }, + [](QImage &result, const QImage &value) { + // do some transformation + } + ).results(); +//! [17] |