QFurture是异步进行的,可以开1个或多个线程。返回值可以是任意类型的。当调用result(), resultAt(), results()返回值无效时QFuture将会等待result返回正常为止。可以使用isResultReadAt()函数去判断是否有数据。QFuture返回值可以是多个,使用resultCount()函数可以得到其数量。

还有很多函数就不一一介绍了。直接在代码中演示吧!

下面是一个官方例子:

runfunction.pro

QT += concurrent widgets
CONFIG += cmdlineSOURCES += main.cpptarget.path = $$[QT_INSTALL_EXAMPLES]/qtconcurrent/runfunction
INSTALLS += target

main.cpp

#include <QDebug>
#include <QThread>
#include <QString>
#include <qtconcurrentrun.h>
#include <QApplication>using namespace QtConcurrent;void hello(QString name)
{qDebug() << "Hello" << name << "from" << QThread::currentThread();
}int main(int argc, char **argv)
{QApplication app(argc, argv);QFuture<void> f1 = run(hello, QString("Alice"));QFuture<void> f2 = run(hello, QString("Bob"));f1.waitForFinished();f2.waitForFinished();
}

程序运行截图如下:

下面是处理大量数据的例子:

源码如下:

FutureDemo.pro

QT -= gui
QT += concurrentCONFIG += c++11 console
CONFIG -= app_bundle# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0SOURCES += \main.cpp# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

main.cpp

#include <QCoreApplication>
#include <QtConcurrent>
#include <QTimer>
#include <QTime>
#include <QEventLoop>
#include <QVector>
#include <QMapIterator>
#include <QString>
#include <QDebug>
#include <QMap>using namespace QtConcurrent;QVector<int> MyTest(QVector<int> vec){qDebug() << "deal with vec !  " << QThread::currentThread();QVector<int> ret;for(int i = 0; i < vec.size(); i++){ret << vec[i] - vec[i];}qDebug() << "deal with vec !  finished" << QThread::currentThread();return ret;
}int main(int argc, char *argv[])
{QCoreApplication a(argc, argv);qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));QVector<int> oriData;for(int i = 0; i < 1000000; i++){oriData.append(qrand() % 50000);}qDebug() << "随机数生成完成!";QVector<int> overData;QList<QFuture<QVector<int>>> list;for(int i = 0; i < 1000000; i+= 10000){QFuture<QVector<int>> future = run(MyTest, oriData.mid(i, 10000));list.append(future);}QEventLoop loop;QTimer::singleShot(2 * 1000, &loop, &QEventLoop::quit);loop.exec();for(int i = 0; i < list.size(); i++){list[i].waitForFinished();overData.append(list[i]);}qDebug() << "over";return a.exec();
}

运行截图如下:

变量

Qt文档阅读笔记-QFuture官方解析及实例相关推荐

  1. Qt文档阅读笔记-QSet官方解析及实例

    目录 官方解析 博主栗子 官方解析 QSet类是一个模板类,他是一个哈希表集合. QSet<T>是Qt的一个普通容器类.QSet存储的值是不指明顺序的,QSet对这些值提供了快速检索的功能 ...

  2. Qt文档阅读笔记-QtWebApp官方解析与实例(使用QtWebApp搭建HTTP服务器)

    目录 官方解析 博主例子 官方解析 QtWepApp是一个C++的http服务器,受到了java Servlets的启发,因为是Qt写的,所以有跨平台的支持. QtWebApp包含如下的组成部分:   ...

  3. Qt文档阅读笔记-QHostInfo官方解析与实例(根据Host获取IP)

    官方解析 QHostInfo提供了一个静态方法获取主机名: QHostInfo中有一个查找机制,可以根据IP找主机名,也可能工具主机名找IP,可以通过调用QHostInfo::lookupHost这个 ...

  4. Qt文档阅读笔记-QTcpServer官方解析与实例(使用QSocket创建简单的HTTP服务器)

    目录 官方解析 博主例子(做一个简单的HTTP服务器) 本例子中HTTP协议关键点 官方解析 QTcpServer类,提供TCP服务的基础: 这个类接受TCP连接,可以指定一个端口,也可以让其自动一个 ...

  5. Qt文档阅读笔记-QWebPage官方解析与实例

    目录 官方解析 博主例子 源码下载地址 官方解析 QWebPage提供一个视图对象和一个web页面: QWebPage提供了web页面的内容,各种设置(是否支持JavaScript等)和连接,它与QW ...

  6. Qt文档阅读笔记-QWebView官方解析与实例

    目录 背景 官方解析 博主例子 背景 最近发现某Qt项目,出现的效果杠杆的,在看某一小功能的时候,发现有个echart的东西,百度了发现,真的是一个新大陆,Qt加web编程,贼吉尔可怕. 在此发现使用 ...

  7. Qt文档阅读笔记-Q_CLASSINFO官方解析与实例

    官方解析 Q_CLASSINFO 这个宏为类提供额外的信息,要想获取这个Q_CLASSINFO这个信息要使用QObject::metaObject().Qt在Active Qt,Qt D-BUS以及Q ...

  8. Qt文档阅读笔记-stackUnder官方解析与实例

    目录 官方解析 博主例子 官方解析 这里可以配合raise()和lower()这两个函数来使用! 博主例子 用2个label,点击谁谁就浮在界面的最上面,很简单的代码,程序运行截图如下: 源码如下: ...

  9. Qt文档阅读笔记-QPropertyAnimation官方解析及实例

    目录 官方解析 博主例子 官方解析 QPropertyAnimation QPropertyAnimation类为Qt属性提供动画. QPropertyAnimation类可以修改Qt属性,从而达到动 ...

最新文章

  1. 【node测试系列】几款前端测试断言库(Assertions lib)的选型总结
  2. 微信小程序 获取用户信息 官网例程 详解
  3. 学习笔记(15):Python网络编程并发编程-进程理论
  4. java boolean例子_Java Field setBoolean()用法及代码示例
  5. solr mysql数据注入_(solr系列:四)将mysql数据库中的数据导入到solr中
  6. 【Leetocde | 24 】152. 乘积最大子序列
  7. android studio 2.3 instant run,android studio 2.3 instant run not working
  8. centos7.0 安装java1.8,tomcat
  9. oracle有硬件吗,Oracle 10G数据库中软硬件环境有哪些要求?
  10. tqdm模块不能单行输出问题
  11. Win7系统用键盘替代鼠标的小技巧
  12. 易辅客栈第六套零基础开发商业脚本_网页游戏篇
  13. 高校公寓管理系统java下载_Spring+SpringMVC+Mybatis高校宿舍管理系统.zip
  14. 数据迁移方案-云迁移
  15. 找工作必做事项-剑指offer
  16. 箱形图的优缺点,python绘制箱形图
  17. Viddy上视频营销最成功的6家公司
  18. Python爬虫-02 request模块爬取妹子图网站
  19. 小米、街电、携程等名企找人了,免费内推
  20. 选择合适的 Go 字符串拼接方式

热门文章

  1. ecshop常用二次开发修改
  2. client 连接 host —— 虚拟机
  3. 几个高效做事的法则,让你的一天有 25 小时
  4. 50+企业数字化转型、管理的方法论,这本书到底有什么干货?
  5. 让我们拭目以待的jinbiguandan
  6. 为踏实上进的【飞鸽传书】开发者而感动
  7. 【狂转】某个N人的访谈记录
  8. 《福布斯》:微软的印度未来
  9. 用VC写Assembly代码(6)--附录2[windows进程中的内存结构]
  10. 查找最接近的元素c语言,查找最接近的元素