可以取消并报告进度的QFuture [英] QFuture that can be cancelled and report progress

查看:175
本文介绍了可以取消并报告进度的QFuture的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

QFuture类具有诸如cancel()progressValue()等的方法.显然,可以通过QFutureWatcher对其进行监视.但是,QtConcurrent::run()的文档为:

The QFuture class has methods such as cancel(), progressValue(), etc. These can apparently be monitored via a QFutureWatcher. However, the documentation for QtConcurrent::run() reads:

请注意,由 QtConcurrent :: run()不支持 取消,暂停或进度 报告.返回的QFuture可以 仅用于查询 运行/完成状态和退货 函数的值.

Note that the QFuture returned by QtConcurrent::run() does not support canceling, pausing, or progress reporting. The QFuture returned can only be used to query for the running/finished status and the return value of the function.

我徒劳地寻找实际上什么方法 可以创建一个QFuture,该方法可以取消并报告一个长时间运行的操作的进度. (看起来QtConcurrent::map()和类似的功能可以,但是我只有一个长期运行的方法.)

I have looked in vain for what method actually can create a QFuture that can be cancelled and report progress for a single long-running operation. (It looks like maybe QtConcurrent::map() and similar functions can, but I just have a single, long-running method.)

(对于熟悉.Net的人,类似BackgroundWorker类.)

(For those familiar with .Net, something like the BackgroundWorker class.)

有哪些可用选项?

推荐答案

对于长期运行的单个任务, QThread 可能是您最好的选择.它没有内置的进度报告或取消功能,因此您必须自己动手.但是对于简单的进度更新,它并不难.要取消任务,请检查可以在任务循环中调用线程设置的标志.

For a long running single task, QThread is probably your best bet. It doesn't have build-in progress reporting or canceling features so you will have to roll your own. But for simple progress update it's not that hard. To cancel the task, check for a flag that can be set from calling thread in your task's loop.

要注意的一件事是,如果您覆盖QThread::run()并将其放置在此处,则无法从那里发出信号,因为QThread对象不是在运行它的线程内创建的,并且您不能从中拉出QObject.正在运行的线程.在问题.

One thing to note is if you override QThread::run() and put your task there, you can't emit signal from there since the QThread object is not created within the thread it runs in and you can't pull the QObject from the running thread. There is a good writeup on this issue.

这篇关于可以取消并报告进度的QFuture的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆