PDA

View Full Version : QtConncurrent - calling function within the class



jacek_
28th October 2009, 14:47
Hello everyone.

I need to call a function from another function in the same class. As it requires much time to be finished (running mysql query on external server), I want it to be in another thread. My code is sth like that:

void runQuery(){
...
}

void getProducts(){
QtConcurrent::run(runQuery);
}


I get an error:


no matching function to call to 'run(<unknown type>)'

Could you help me out please?

mgoetz
28th October 2009, 15:15
Look into QThread, not QtConcurrent.

jacek_
28th October 2009, 15:20
Look into QThread, not QtConcurrent.
Still don't know how to use it in my class. I will appreciate some tips and hints.

caduel
28th October 2009, 15:38
#include <boost/bind.hpp>

void YourClassName::getProducts(){
QtConcurrent::run( boost::bind(&YourClassName:runQuery, this) );
}

obviously, you need to take care that runQuery() and your class are "thread safe" because they will run concurrently...

For details on Boost and Boost.Bind see www.boost.org

HTH

jacek_
28th October 2009, 16:51
#include <boost/bind.hpp>

void YourClassName::getProducts(){
QtConcurrent::run( boost::bind(&YourClassName:runQuery, this) );
}

obviously, you need to take care that runQuery() and your class are "thread safe" because they will run concurrently...

For details on Boost and Boost.Bind see www.boost.org

HTH

Thanks for input, but the solution isn't working. It compiles without errors and warnings, program works, but not properly. It doesn't get results from a database. Isn't a new instance of my class being created by QtConcurrent::run( boost::bind(&YourClassName:runQuery, this) );? What can be the couse?

caduel
28th October 2009, 17:37
no, boost::bind creates a function object by binding (here runQuery()) a member function to an object (here this). So it exeutes runQuery() on the very object you call getProducs() on.

You can also pass YourClassName() instead of this, then you create a temporary that is used for executing. (Also note that you might need a separe db connection if you are using the db from the main thread, too.)