我有一个带有 的程序QThread
,其中包含一个网络客户端。客户端应该获取一个对象,对其进行处理,将其上传到服务器,获取响应,然后向主线程报告。我用它做了这个std::promise
,并使用了它的future
. 我制作std::promise
了一个在获取其未来后发出的对象的成员。
一个最小的示例代码如下:
我发出的对象:
struct FileToUpload
{
std::string fileData;
std::string filename;
std::promise<int> promise;
}
我用于发射的部分:
FileToUpload theFile;
auto uploadFuture = theFile.promise.get_future();
emit uploadFile(&theFile); //passing pointer because promise is non-copyable
auto uploadSuccess = uploadFuture.get();
有没有 Qt 方法可以做到这一点?
我只找到了一个QFuture
可以与 QtConcurrent 一起使用的类。我找不到解释如何将其与QThread
. 我有哪些选择可以正确使用 Qt?