AngryDuck
AngryDuck

Reputation: 4607

QFuture Help, Threading C++ Qt

So i have found out that some issues on my UI program are happening because im not making use of threading.

I had a look and from what i can see in the documentation QFuture seemed like what i needed, am i right in thinking that this is used when you have a function and you just want to run it in a thread?

right so assuming im correct on that i go through the documentation and type code shown in all the examples and basically it just does not work at all...

UI.cpp

#include <QFuture>
#include <QtConcurrent/QtConcurrentRun>
#include "something.h"
void UI::button_click()
{
     something s;
     QFuture<int> result = QtConcurrent::run(s.heavyFunction());

     result.waitForFinished();
}

something.cpp

int something::heavyFunction()
{
     // heavy code here
     return 42;
}

ok obviously this isnt real code but theres nothing different here than in my actual code but i get the following error

error C2780: 'QFuture<T> QtConcurrent::run(T (__cdecl *)(Param1),const Arg1 &)' : expects 2 arguments - 1 provided

and it says this error about 7 times basically saying 1,2,3,4,5,6,7 arguments expected 1 provided?

i got this code from a number of examples so what am i missing any ideas?

Upvotes: 3

Views: 8296

Answers (1)

juanchopanza
juanchopanza

Reputation: 227400

QtConcurrent::run takes a function and arguments to be passed to that function. In the case of a member function, you would need something like

something s;
QFuture<int> result = QtConcurrent::run(&s, &something::heavyFunction);

Member functions such as something::heavyFunction take an implicit parameter of type T*, where T is the type that has the member function (something in this case). QtConcurrent::run seems to deal with this by taking the instance as first parameter, and the member function as second. This seems unnatural and counter intuitive, and at odds with e.g. std::async.

Upvotes: 7

Related Questions