一、说明
QFutureWatcher provides information and notifications about a QFuture. Use the setFuture() function to start watching a particular QFuture.
二、代码
1 #include "widget.h"
2 #include "ui_widget.h"
3 #include <QtConcurrent>
4 #include <QFuture>
5 #include <QFutureWatcher>
6 #include <QTimer>
7 #include <QDateTime>
8 Widget::Widget(QWidget *parent) 9 : QWidget(parent) 10 , ui(new Ui::Widget) 11 { 12 ui->setupUi(this); 13
14 QFutureWatcher<void> *pwatcher = new QFutureWatcher<void>; 15 connect(pwatcher, SIGNAL(finished()), 16 this, SLOT(on_finished())); 17
18 QFuture<void> f1 = QtConcurrent::run(hello,QString("demo1")); 19 QFuture<void> f2 = QtConcurrent::run([=]{ 20 hello("demo2"); 21 //延时
22 _sleep(3000); 23 }); 24
25 pwatcher->setFuture(f2); 26 } 27
28 Widget::~Widget() 29 { 30 delete ui; 31 } 32
33 void Widget::hello(const QString &str) 34 { 35 qDebug()<<"Widget hello"<<str<<"from"<<QThread::currentThread(); 36 } 37
38 void Widget::on_finished() 39 { 40 qDebug()<<"Widget QFuture f2 on_finished"; 41 }