[Qt-interest] QtConcurrent and QFutureWatcher
Andre Somers
andre at familiesomers.nl
Thu Sep 23 16:02:22 CEST 2010
Op 23-9-2010 14:35, Marius Roets schreef:
> Hi Everybody,
>
> I have this piece of code in a widget, when a button is pressed:
>
> MyObject m;
> QFutureWatcher<void> futureWatcher;
> connect(&futureWatcher, SIGNAL(finished()), this,
> SLOT(longExecuteCompleted()));
> futureWatcher.setFuture(QtConcurrent::run(m, &MyObject::longExecute));
>
> It seems to work as expected, except that longExecuteCompleted() never
> gets called, and I can see from the log files that longExecute() did
> complete. Is there anything I could missing in MyObject, longExecute()
> or futureWatcher for this to work.
The issue is, that your futureWatcher goes out of scope before the
execution of your longExecute is finished. That means that the
futureWatcher is destroyed, and the connection deleted. Put your future
watcher somewhere where it will last long enough to actually signal the
completion of your task, like on the heap (don't forget to delete it
when you're done using it) or as a member of your object.
André
More information about the Qt-interest-old
mailing list