diff --git a/src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentrun.cpp b/src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentrun.cpp index bf39119c517..59b65dcb3a4 100644 --- a/src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentrun.cpp +++ b/src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentrun.cpp @@ -230,3 +230,21 @@ run(f); // this will select the 2nd overload // run(f); // error, both candidate overloads potentially match //! [14] +//! [15] +void foo(int arg); +void foo(int arg1, int arg2); +... +QFuture future = QtConcurrent::run(foo, 42); +//! [15] + +//! [16] +QFuture future = QtConcurrent::run([] { foo(42); }); +//! [16] + +//! [17] +QFuture future = QtConcurrent::run(static_cast(foo), 42); +//! [17] + +//! [18] +QFuture future = QtConcurrent::run(qOverload(foo), 42); +//! [18] diff --git a/src/concurrent/doc/src/qt6-changes.qdoc b/src/concurrent/doc/src/qt6-changes.qdoc index 9614790bacb..8711209e13d 100644 --- a/src/concurrent/doc/src/qt6-changes.qdoc +++ b/src/concurrent/doc/src/qt6-changes.qdoc @@ -71,6 +71,11 @@ QFuture future = QtConcurrent::run(&QImage::invertPixels, &image, QImage::InvertRgba); \endcode + Another side effect is that \c QtConcurrent::run() will not work with + overloaded functions anymore. For example, the code below won't compile: + + \include qtconcurrentrun.cpp run-with-overload-calls + Other methods of QtConcurrent have no behavioral changes and do not introduce source compatibility breaks. */ diff --git a/src/concurrent/qtconcurrentrun.cpp b/src/concurrent/qtconcurrentrun.cpp index 978bc8cd8dc..8347440b66e 100644 --- a/src/concurrent/qtconcurrentrun.cpp +++ b/src/concurrent/qtconcurrentrun.cpp @@ -89,6 +89,26 @@ the function. Changes made to the arguments after calling QtConcurrent::run() are \e not visible to the thread. + Note that QtConcurrent::run does not support calling overloaded functions + directly. For example, the code below won't compile: + +//! [run-with-overload-calls] + \snippet code/src_concurrent_qtconcurrentrun.cpp 15 + + The easiest workaround is to call the overloaded function through lambda: + + \snippet code/src_concurrent_qtconcurrentrun.cpp 16 + + Or you can tell the compiler which overload to choose by using a + \c static_cast: + + \snippet code/src_concurrent_qtconcurrentrun.cpp 17 + + Or qOverload: + + \snippet code/src_concurrent_qtconcurrentrun.cpp 18 +//! [run-with-overload-calls] + \section2 Returning Values from the Function Any return value from the function is available via QFuture: