Results 1 to 2 of 2

Thread: QtConcurrent and boost::bind

  1. #1
    Join Date
    Oct 2009
    Posts
    11
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default QtConcurrent and boost::bind

    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.

    Qt Code:
    1. #include <boost/bind.hpp>
    2. #include <qtconcurrentmap.h>
    3.  
    4. void doSomething(int value)
    5. {
    6. // Want to open a file corresponding to a QString here,
    7. // but how does it get passed in?
    8. }
    9.  
    10. int main(int argc, char *argv[])
    11. {
    12. QStringList filenames;
    13. for (int i=0; i<100; i++)
    14. {
    15. QString filename("f");
    16. filename.append(QString::number(i));
    17. filename.append(".dat");
    18. filenames << filename;
    19. }
    20. QFuture<void> future = QtConcurrent::map(filenames, boost::bind(doSomething, 50));
    21.  
    22. return 0;
    23. }
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. void scale(QImage &image)
    2. {
    3. image = image.scaled(100, 100);
    4. }
    5.  
    6. QList<QImage> images = ...;
    7. QFuture<void> future = QtConcurrent::map(images, scale);
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. void doSomething(QString filename, int value)
    2. {
    3. }
    To copy to clipboard, switch view to plain text mode 

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

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

  2. #2
    Join Date
    Jan 2006
    Location
    Boston, MA
    Posts
    40
    Thanks
    1
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QtConcurrent and boost::bind

    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():

    Qt Code:
    1. void doSomething(QString &filename, int value)
    2. {
    3. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. QList<QStrings> list = ...
    2. QFuture<void> future = QtConcurrent::map(list, boost::bind(doSomething, 10));
    To copy to clipboard, switch view to plain text mode 

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.