From c977e74afd18afff8729070f631e6b7a3f2887f5 Mon Sep 17 00:00:00 2001 From: Vitaly Fanaskov Date: Wed, 26 Feb 2020 15:22:40 +0100 Subject: QtConcurrent::run: accept more then five function's arguments MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [ChangeLog][Potentially Source-Incompatible Changes] QtConcurrent::run has the following signatures: run(Function &&f, Args &&...args) and run(QThreadPool *pool, Function &&f, Args &&...args). If f is a member pointer, the first argument of args should be an object for which that member is defined (or a reference, or a pointer to it). See the documentation for more details. Fixes: QTBUG-82383 Change-Id: I18f7fcfb2adbdd9f75b29c346bd3516304e32d31 Reviewed-by: MÃ¥rten Nordheim Reviewed-by: Sona Kurazyan --- .../code/src_concurrent_qtconcurrentrun.cpp | 36 ++++++++++++++++++++-- 1 file changed, 33 insertions(+), 3 deletions(-) (limited to 'src/concurrent/doc/snippets/code') diff --git a/src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentrun.cpp b/src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentrun.cpp index 5437822842f..0b827660540 100644 --- a/src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentrun.cpp +++ b/src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentrun.cpp @@ -93,7 +93,7 @@ QString result = future.result(); //! [4] // call 'QList QByteArray::split(char sep) const' in a separate thread QByteArray bytearray = "hello world"; -QFuture > future = QtConcurrent::run(bytearray, &QByteArray::split, ','); +QFuture > future = QtConcurrent::run(&QByteArray::split, bytearray, ','); ... QList result = future.result(); //! [4] @@ -101,16 +101,46 @@ QList result = future.result(); //! [5] // call 'void QImage::invertPixels(InvertMode mode)' in a separate thread QImage image = ...; -QFuture future = QtConcurrent::run(&image, &QImage::invertPixels, QImage::InvertRgba); +QFuture future = QtConcurrent::run(&QImage::invertPixels, &image, QImage::InvertRgba); ... future.waitForFinished(); // At this point, the pixels in 'image' have been inverted //! [5] - //! [6] QFuture future = QtConcurrent::run([=]() { // Code in this block will run in another thread }); ... //! [6] + +//! [7] +static void addOne(int &n) { ++n; } +... +int n = 42; +QtConcurrent::run(&addOne, std::ref(n)).waitForFinished(); // n == 43 +//! [7] + +//! [8] +struct TestClass +{ + void operator()(int s1) { s = s1; } + int s = 42; +}; + +... + +TestClass o; + +// Modify original object +QtConcurrent::run(std::ref(o), 15).waitForFinished(); // o.s == 15 + +// Modify a copy of the original object +QtConcurrent::run(o, 42).waitForFinished(); // o.s == 15 + +// Use a temporary object +QtConcurrent::run(TestClass(), 42).waitForFinished(); + +// Ill-formed +QtConcurrent::run(&o, 42).waitForFinished(); // compilation error +//! [8] -- cgit v1.2.3