QtConncurrent - calling function within the class
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:
Code:
void runQuery(){
...
}
void getProducts(){
QtConcurrent::run(runQuery);
}
I get an error:
Code:
no matching function to call to 'run(<unknown type>)'
Could you help me out please?
Re: QtConncurrent - calling function within the class
Look into QThread, not QtConcurrent.
Re: QtConncurrent - calling function within the class
Quote:
Originally Posted by
mgoetz
Look into QThread, not QtConcurrent.
Still don't know how to use it in my class. I will appreciate some tips and hints.
Re: QtConncurrent - calling function within the class
Code:
#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
Re: QtConncurrent - calling function within the class
Quote:
Originally Posted by
caduel
Code:
#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?
Re: QtConncurrent - calling function within the class
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.)