Proper use of QtConcurrent::map / mapped



  • Hi,

    I would like to make use of QtConcurrent::map to iterate over a QStringList.

    However I'm a bit lost.

    void ThisClass::startScanning()
    {
    QStringList stuff;
    stuff << "hey";
    stuff << "woohah";
    QFuture<void> future=QtConcurrent::map(stuff,&ThisClass::doStuff);
    }

    void ThisClass::doStuff(QString file)
    {
    //do stuff with file
    }

    Compiling this returns the error

    error: no match for call to '(QtConcurrent::MemberFunctionWrapper1<void, ThisClass, QString>) (QString&)'

    I'm fairly new to C++, can anybody help?

    Thanks in advance.



  • Hi,
    If you refer to "Using Member Functions" section of the "QtConcurrent documentation":http://qt-project.org/doc/qt-4.8/qtconcurrentmap.html .
    It states:
    QtConcurrent::map(), QtConcurrent::mapped(), and QtConcurrent::mappedReduced() accept pointers to member functions. The member function class type must match the type stored in the sequence

    So in your case your QList should contain "ThisClass" instances.
    You can maybe achieve your goal using function objects to wrap your "This class" instance:
    @
    struct WrapperObject
    {
    WrapperObject(ThisClass *instance)
    : m_instance(instance) { }

    typedef void result_type;
    
    void operator()(const QString &string)
    {
         m_instance->doStuff(string);
    }
    
    ThisClass *m_instance;
    

    };

    QStringList stuff;
    stuff << “hey”;
    stuff << “woohah”;
    QFuture<void> future = QtConcurrent::maped(stuff, WrapperObject(pMyInstance));
    

    @



  • Thanks for your reply, uh, I'm trying to understand what you're doing here, at any rate this won't compile either since the pMyInstance object is not known?

    Isn't there a solution that doesn't rape the elegance of my code? :p



  • Hi,
    I just took the example of the docs and adapted a little bit to jour example. I will try to think of an alternative solution.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.