QtConcurrency::mapped with a member function
-
21-12-2019 - |
Question
How can I use QtConcurrent::mapped
with a member function as operator? Currently I'm using a callable object (which is ugly solution):
struct Vectorizer {
Vectorizer(DialogCreateSamples* cs)
: m_createSamples(cs) { }
typedef QString result_type;
QString operator()(const QString &input)
{
return m_createSamples->generateVector(input);
}
DialogCreateSamples* m_createSamples;
};
QFuture<QString> future = QtConcurrent::mapped(m_inputs,Vectorizer(this));
Also tried to pass lambda expressions but compiler says there is no result_type
defined in lambda. This works with QtConcurrent::map
because map
do not need result_type
. So if I can add a typedef
in lambda it should work...
Solution
Maybe bind? Either std::bind if you're using C++11 or std::tr1::bind or boost::bind otherwise.
Something like:
QtConcurrent::mapped(m_inputs, std::bind(&Class::member_function, pointerToObjectOfTypeClass /* this? */, _1 /* placeholder for argument of your function filled by data from m_inputs */));
We use this in our code; example at: https://github.com/clementine-player/Clementine/blob/d03c1aa2419a0ceefd7f65114c1ac8991790b716/src/playlist/playlistbackend.cpp#L187
Hope it helps.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow