[Qt-interest] concurrent-run of a static function
K. Frank
kfrank29.c at gmail.com
Sun May 16 23:41:26 CEST 2010
Hello Gabriele -
Warning: I do not really understand this. I will attempt to answer, in order to
try to learn something myself. I ask the forbearance of the list, and I hope
experts will chime in to fix all the things I will get wrong. (Also, I have not
tested my answer with code.)
On Sun, May 16, 2010 at 1:36 PM, Gabriele Kahlout
<gabriele at mysimpatico.com> wrote:
> ...
> Back to the question:
>
> I was referring to:
>
> // call 'void QImage::invertPixels(InvertMode mode)' in a separate thread
> QImage image = ...;
> QFuture<void> future = QtConcurrent::run(image, &QImage::invertPixels,
> QImage::InvertRgba);
My understanding (from the Qt documentation) is that there are two versions of
QtConcurrent::run(...). One takes a function (more precisely, a
function pointer)
(which I assume can be either a free function (not a member of a class) or a
static member function of a class. (I also assume that one can pass a functor
(function object) to what is conceptually this version of QtConcurrent::run().)
The second version takes a pointer to an object as its first argument, and a
pointer-to-member-function as its second argument. (I assume that we get
two versions because QtConcurrent::run() is overloaded on the type of its
first argument (or possibly on the types of its first and second arguments).
(Please see my question about this, below.)
> ...
> I tried several modifications to use a static function in palce of the
> invertPixels, but failed. Since the doc doesn't explicit that one cannot use
> static functions, I wondered. BTW the class is a singleton, so I may not use
> an instance to call the static method.
I'm guessing as to what you are trying to do, but I think you are trying to
simply replace the pointer-to-member-function, &QImage::invertPixels,
with a (pointer to a) static (free or static member) function, but still have
it as the second argument. According to the documentation, this won't
work.
Instead, you need to use the first (function pointer) version of
QtConcurrent::run().
Something like this.
void doSomeImageWork (QImage *imageToWorkOn, arg2, arg3, ...);
QFuture<void> future =
QtConcurrent::run (doSomeImageWork, &image, arg2, arg3, ...);
Also, let's say, for the sake of argument, that doSomeImageWork were
a _static_ member function of QImage. You would still have to call it
this way:
QFuture<void> future =
QtConcurrent::run (QImage::doSomeImageWork, &image, arg2, arg3, ...);
Because QImage::doSomeImageWork is a static member function, it is not
associated with any specific instance of QImage (said another way, it doesn't
have access to a "this" pointer), so you need to tell it with an argument
(&image) which image (which instance of QImage) it is supposed to work on.
To All: Please help me fix what I've gotten wrong here. I am trying to learn
Qt, so I am using this as an opportunity to learn something about QtConcurrent.
As for the question I mentioned earlier: How does Qt decide which version of
QtConcurrent::run() to use? As far as I know, it would have to be by
overloading
on the argument types. So what happens if I pass a function object (which is
an instance of a class) as the first argument, and a pointer-to-member-function
as the second argument? Which version gets called (or do I get an error, etc.)?
Thanks for any insight into this.
> ...
> Regards,
> K. Gabriele
Good luck.
K. Frank
More information about the Qt-interest-old
mailing list