I see that Qt has future class that is direct analog for boost::future but what is qt for boost::promise?
Constructing my own QFuture as shown in the accepted answer did not work for me. At first it seemed like it was working, but in my testing I realized it was not blocking the caller. Whoops! So I dug into the code a little further and found that QFutureInterface is what you want to use as your 'promise'. Like boost::promise, QFutureInterface is what you interact with in your worker thread, and it is a factory for QFutures.
So here's what I've been doing in Qt 4.8 (not sure if this is applicable to later versions).
QFutureInterface<QVariant> promise;
promise.reportStarted();
...
promise.reportResult(someVariant);
promise.reportFinished();
Then in the client thread, assuming you have access to the QFutureInterface 'promise'
QVariant result = promise.future().result();
The future()
call is a factory method for creating a QFuture bound to your QFutureInterface. You should be able to get the QFuture and call result()
on it later if you wanted.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With