Problem in using the QtConcurrent
-
There are actually 96 overloads of QtConcurrent::run and that's just a nasty way of compiler letting you know that none of them has parameter types matching what you fed it with (2 error lines per overload).
You are passing as an argument a result of
addCustomer
function and I doubt that's what you meant.
You should instead pass it an object pointer, a method pointer and a list of parameters. Therun
function will call the method in another thread for you. So, assumingaddCustomer
is a method of classFoo
that takes a string parameter, you would call it like this:Foo* someInstance = ... QtConcurrent::run(someInstance, &Foo::addCustomer, QString("Hello!"));
If
addCustomer
is a free standing function (I doubt that but lets say it is) then you call it without an object pointer:QtConcurrent::run(&addCustomer, QString("Hello!"));
-
hi my bro thanks for your response ,Do not fix problem
185 Errormyclass.cpp void MyClass::addCustomer(QString NameVaFamily,QString Mob1,QString Mob2,QString Tell,QString TarikhSabt,QString Tozihat,QString Address) { ............ } void MyClass::test() { QFuture<void> f = QtConcurrent::run(&addCustomer,QString("param"),QString("param"),QString("param"),QString("param"),QString("param"),QString("asd"),QString("param")); f.waitForFinished(); }
-
Please read what Chris Kawa wrote again!
What you are now trying only works if addCustomer is a function and not a method.If it is a method you have to do it like this:
Foo* someInstance = ... QtConcurrent::run(someInstance, &Foo::addCustomer, QString("Hello!"));
-
Since
addCustomer
is a method of classMyClass
and you're calling it inside anotherMyClass
method you need the syntax from my first example, not the second one:QFuture<void> f = QtConcurrent::run(this, &MyClass::addCustomer,QString("param"),QString("param"),QString("param"),QString("param"),QString("param"),QString("asd"),QString("param"));
-
What are the errors you get now (they are not visible in the screenshot)?
-
The error points to line 65, which is empty. Something did not build. Please clean and rebuild your solution.
The bigger problem though is your usage of QtConcurrent. Since you wait on the future (
f.waitForFinished()
), you are blocking the main thread waiting on asynchronous call to finish. This is actually slower than simply calling the function directly without QtConcurrent.It makes no sense to start an asynchronous call and immediately wait for it to finish.
The whole point of QtConcurrent is to be able to do other things while the call is processed in another thread.