I can't seem to be able to associate QtConcurrent::run()
with a method (function member of a class) only with a simple function. How can I do this?
With a regular function I cannot emit signals and its a drag. Why would anyone find this a better alternative to QThread
is beyond me and would like some input.
Yes, this is possible (and quite easy).
Here is an example (from the Qt documentation):
Basically, all you have to do is pass a pointer to the object as the first argument and the address of the method as the second argument (followed by any other arguments).
See: https://doc.qt.io/qt-5/qtconcurrentrun.html
The problem is that when you use a pointer to member function, you need to somehow provide the
this
parameter also (i.e., the object on which the member function should be called).The syntax for this is quite difficult if you haven't used it before. It might be good to read http://www.parashift.com/c++-faq-lite/pointers-to-members.html .
Say you have a class
Dog
and a functionDog::walkTheDog(int howlong_minutes)
. Then you ought to be able to usestd::bind1st
andstd::mem_fun
to make it suitable forQtConcurrent::run
:std::bind1st(std::mem_fun(&Dog::walkTheDog), &dog)
returns a function-like object which has bound the member function to a particular dog. From that point you can use it much like you could use a standalone function.