日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2025/3/15 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Qt文档阅读笔记-QFuture官方解析及实例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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官方解析及实例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。