Using Bound Function Arguments
Note that Qt does not provide support for bound functions. This is provided by 3rd party libraries like Boost or C++ TR1 Library Extensions.
If you want to use a map function that takes more than one argument you can use boost::bind() or std::tr1::bind() to transform it onto a function that takes one argument.
As an example, we'll use QImage::scaledToWidth():
QImage QImage::scaledToWidth(int width, Qt::TransformationMode) const;
scaledToWidth takes three arguments (including the "this" pointer) and can't be used with QtConcurrent::mapped() directly, because QtConcurrent::mapped() expects a function that takes one argument. To use QImage::scaledToWidth() with QtConcurrent::mapped() we have to provide a value for the width and the transformation mode:
boost::bind(&QImage::scaledToWidth, 100 Qt::SmoothTransformation)
The return value from boost::bind() is a function object (functor) with the following signature:
QImage scaledToWith(const QImage &image)
This matches what QtConcurrent::mapped() expects, and the complete example becomes:
QList<QImage> images = ...;
QFuture<QImage> thumbnails = QtConcurrent::mapped(images, boost::bind(&QImage::scaledToWidth, 100 Qt::SmoothTransformation));
Function Documentation
T QtConcurrent::blockingMappedReduced ( const Sequence & sequence, MapFunction mapFunction, ReduceFunction reduceFunction, QtConcurrent::ReduceOptions reduceOptions = UnorderedReduce | SequentialReduce )
Calls mapFunction once for each item in sequence. The return value of each mapFunction is passed to reduceFunction.
Note that while mapFunction is called concurrently, only one thread at a time will call reduceFunction. The order in which reduceFunction is called is determined by reduceOptions.
Note: This function will block until all items in the sequence have been processed.
See also mapped().
T QtConcurrent::blockingMappedReduced ( ConstIterator begin, ConstIterator end, MapFunction mapFunction, ReduceFunction reduceFunction, QtConcurrent::ReduceOptions reduceOptions = UnorderedReduce | SequentialReduce )
This is an overloaded member function, provided for convenience.
Calls mapFunction once for each item from begin to end. The return value of each mapFunction is passed to reduceFunction.
Note that while mapFunction is called concurrently, only one thread at a time will call reduceFunction. The order in which reduceFunction is called is undefined.
Note: This function will block until the iterator reaches the end of the sequence being processed.
See also blockingMappedReduced().
QFuture<void> QtConcurrent::map ( Sequence & sequence, MapFunction function )
Calls function once for each item in sequence. The function is passed a reference to the item, so that any modifications done to the item will appear in sequence.
QFuture<void> QtConcurrent::map ( Iterator begin, Iterator end, MapFunction function )
This is an overloaded member function, provided for convenience.
Calls function once for each item from begin to end. The function is passed a reference to the item, so that any modifications done to the item will appear in the sequence which the iterators belong to.
QFuture<T> QtConcurrent::mapped ( const Sequence & sequence, MapFunction function )
Calls function once for each item in sequence and returns a future with each mapped item as a result. You can use QFuture::const_iterator or QFutureIterator to iterate through the results.
QFuture<T> QtConcurrent::mapped ( ConstIterator begin, ConstIterator end, MapFunction function )
This is an overloaded member function, provided for convenience.
Calls function once for each item from begin to end and returns a future with each mapped item as a result. You can use QFuture::const_iterator or QFutureIterator to iterate through the results.
QFuture<T> QtConcurrent::mappedReduced ( const Sequence & sequence, MapFunction mapFunction, ReduceFunction reduceFunction, QtConcurrent::ReduceOptions reduceOptions = UnorderedReduce | SequentialReduce )
Calls mapFunction once for each item in sequence. The return value of each mapFunction is passed to reduceFunction.
Note that while mapFunction is called concurrently, only one thread at a time will call reduceFunction. The order in which reduceFunction is called is determined by reduceOptions.
QFuture<T> QtConcurrent::mappedReduced ( ConstIterator begin, ConstIterator end, MapFunction mapFunction, ReduceFunction reduceFunction, QtConcurrent::ReduceOptions reduceOptions = UnorderedReduce | SequentialReduce )
This is an overloaded member function, provided for convenience.
Calls mapFunction once for each item from begin to end. The return value of each mapFunction is passed to reduceFunction.
Note that while mapFunction is called concurrently, only one thread at a time will call reduceFunction. By default, the order in which reduceFunction is called is undefined.
Note: QtConcurrent::OrderedReduce results in the ordered reduction.