Qt Concurrent Map Use of Member Functions
-
Hello I am trying to run class method in QtConcurrent::map, pas a QStringList for iteration.
This is how I use the QtConcurrent:
void worker::start() { set_Fl_Name("main"); set_Root_Path("/home/rober"); QStringList temp; QDir dr(root_path); //QStringList temp; foreach(QFileInfo fl,dr.entryInfoList(QDir::NoDotAndDotDot|QDir::Dirs)) { temp.append(fl.filePath()); } temp.append(root_path); QtConcurrent::map(temp,&worker::search_rec); }
And this is the definition of the method I am trying to call:
QStringList search_rec(QString str);
But It doesn't work, and i t gives me this kind of error message:
/usr/include/qt5/QtConcurrent/qtconcurrentmapkernel.h:71: error: no match for call to '(QtConcurrent::MemberFunctionWrapper1<QStringList, worker, QString>) (QString&)'
map(*it); -
Hi, welcome to the forum.
Couple of things wrong here.
First, to be able to pass a method like this it needs to be static, otherwise there's no instance to run it on.
Second, to save you from unnecessary copies it should take the string as a const reference, not by value.
Third, similar to above, to save yourself from copies where not needed you should use a const reference in the foreach variable declaration.
Next, The return type of the function you pass should be the type of the element of the container, not the container itself i.e. QString in this case.
And last but not least - the call tomap()
is not blocking, so it will return immediately and the function will exit. You passed a local string list to it so it means the asynchronous calls will operate on freed memory and most likely crash. If you want to wait for the results you should rather useblockingMap()
instead and if not then you need to store that string list somewhere for the whole duration of themap()
execution.