PDA

View Full Version : QtConcurrent and boost::bind



TheShow
1st December 2009, 19:40
I'm having trouble using QtConcurrent::map with boost::bind. The Qt documentation gives a brief example using a QImage, but we need to do something a little different. After searching and trying many different things, I thought I'd start with the most basic example.


#include <boost/bind.hpp>
#include <qtconcurrentmap.h>

void doSomething(int value)
{
// Want to open a file corresponding to a QString here,
// but how does it get passed in?
}

int main(int argc, char *argv[])
{
QStringList filenames;
for (int i=0; i<100; i++)
{
QString filename("f");
filename.append(QString::number(i));
filename.append(".dat");
filenames << filename;
}
QFuture<void> future = QtConcurrent::map(filenames, boost::bind(doSomething, 50));

return 0;
}

This passes in the integer 50 to the doSomething function.

The Qt documentation gives an example that doesn't use boost:bind where it takes each item in the Sequence and calls a function:



void scale(QImage &image)
{
image = image.scaled(100, 100);
}

QList<QImage> images = ...;
QFuture<void> future = QtConcurrent::map(images, scale);

In this case, scale is called with the object in the images list (a QImage). In order to pass additional arguments, the documentation says we can combine QtConcurrent::map with boost::bind.

Can somebody tell me how to implement a function that includes the object in the list (QString in my case) with additional parameters?

I wanted this to work:


void doSomething(QString filename, int value)
{
}

but of course it doesn't even compile, indicating that

`void (*)(QString, int)' is not a class, struct, or union type

fanat9
2nd December 2009, 14:47
Well...

Actually, return from boost::bind is a function object(functor) and QtConcurrent::map() accept such functor's. Boost is heavy, complex library, so I'm personally prefer not to use it when I can. So, take a look on documentation on functor's - maybe you can live without the boost.

But if you have to...
you use boost:bind() to transform your function to a function that takes only one argument and pass result to QtConcurrent::map():



void doSomething(QString &filename, int value)
{
}




QList<QStrings> list = ...
QFuture<void> future = QtConcurrent::map(list, boost::bind(doSomething, 10));