Reputation: 26256
I have a program with a QThread
, which contains a network client. The client is supposed to take an object, process it, upload it to a server, get the response, and report back to the main thread. I did this with std::promise
, and used its future
. I made std::promise
a member of the object emitted after having taken its future.
A minimal sample code is the following:
The object I'm emitting:
struct FileToUpload
{
std::string fileData;
std::string filename;
std::promise<int> promise;
}
The part I'm using for emitting:
FileToUpload theFile;
auto uploadFuture = theFile.promise.get_future();
emit uploadFile(&theFile); //passing pointer because promise is non-copyable
auto uploadSuccess = uploadFuture.get();
Is there a Qt way to do the same?
I only found a QFuture
class that can be used with QtConcurrent. I couldn't find a single example that explains how to use this with QThread
. What are my options to do this with Qt correctly?
Upvotes: 2
Views: 821
Reputation: 301
yes, you may use QFuture without QConcurrent. But the API is not documented. You may take a look with my wrapper library, AsyncFuture, at:
https://github.com/benlau/asyncfuture
It has a clear example to use QFuture like a promise object.
Upvotes: 2