Qt中的异步相关类

Qt中的异步相关类

今天在学习别人的项目时,看到别人包含了QFuture类,我没有见过,于是记录一下。

直接在AI助手中搜索QFuture,得到的时Qt中异步相关的类。于是直接查询一下Qt异步中相关的类。

在Qt中,异步编程是一个重要的概念,它允许开发者在不阻塞主线程的情况下执行耗时的任务。

  1. Qt Concurrent
    • 提供了并行处理迭代容器的map、filter和reduce算法,类似于函数式编程中的概念。
    • 包括QFutureQFutureWatcherQFutureSynchronizer等类,用于访问和监控异步计算的结果。
  2. QFuture
    • 表示一个异步操作的结果,可以附加继续操作(continuations)。
    • 通过QFuture::then()方法,可以在一个异步操作完成后继续执行另一个操作。
    • 支持错误处理,通过QFuture::onFailed()方法可以为特定错误类型附加错误处理程序。
  3. QFutureWatcher
    • 用于监控QFuture对象的状态,如完成、失败或取消。
    • QFuture的状态发生变化时,QFutureWatcher可以发出信号。
  4. QThreadPool
    • 用于管理线程池,可以提交任务到线程池中异步执行。
  5. Qt::runFunction
    • 一个模板函数,用于在后台线程中异步执行函数,并返回一个QFuture对象。
  6. Qt::connect
    • 可以与QFuture一起使用,将信号连接到槽上,以便在异步操作完成时执行槽函数。
  7. qt-async
    • 一个第三方库,提供了异步值(async values )和异步小部件(async widgets),用于异步操作的结果、错误和进度的表示。
  8. QtAsyncSql
    • 一个第三方库,提供了异步和线程化的SQL查询支持。
    • 包括AsyncQuery类,用于执行异步SQL查询。
    • 提供了ConnectionManager类,用于维护数据库连接。
  9. QtAsyncRunner
    • 一个抽象接口,用于启动异步函数,允许在Qt应用程序中轻松地将计算密集型函数提交到线程中执行。

1.使用QFutureQFutureWatcher进行异步操作

c++ 复制代码
#include <QCoreApplication>
#include<QFuture>
#include<QFutureWatcher>
#include<QtConcurrent/QtConcurrentRun>
#include<QDebug>

void AsyncTask(int nNumber)
{
    QThread::sleep(2); // 模拟耗时操作
    qDebug() << "Task Complete With Number:" << nNumber;
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    // 使用QtConcurrent::run将函数提交到线程池
    QFuture<void> future = QtConcurrent::run(AsyncTask,42);
    // 创建一个QFutureWatcher来监控任务
    QFutureWatcher<void> watcher;
          QObject::connect(&watcher,&QFutureWatcher<void>::finished,&a,&QCoreApplication::quit);
    // 将watcher与future关联
    watcher.setFuture(future);
    
    QtConcurrent::run(AsyncTask,42);

    return a.exec();
}

使用QThreadPoolQt::runFunction进行异步操作

c++ 复制代码
#include <QCoreApplication>
#include <QThreadPool>
#include <QtConcurrent/QtConcurrentRun>
#include <QDebug>

void myFunction() {
    qDebug() << "Function is running in a separate thread.";
}

int main(int argc, char *argv[]) {
    QCoreApplication a(argc, argv);

    // 将函数提交到线程池
    QtConcurrent::run(&myFunction);

    // 启动事件循环,等待异步任务完成
    return a.exec();
}

3. 使用QFuture链式调用(Continuations)

c++ 复制代码
#include <QCoreApplication>
#include <QFuture>
#include <QtConcurrent/QtConcurrentRun>
#include <QDebug>

QString processResult(int number) {
    return QString("Processed %1").arg(number);
}

int main(int argc, char *argv[]) {
    QCoreApplication a(argc, argv);

    // 异步执行任务并获取结果
    QFuture<int> future = QtConcurrent::run([]() -> int {
        QThread::sleep(2); // 模拟耗时操作
        return 42;
    });

    // 使用then()方法添加一个continuation
    QFuture<QString> processedFuture = future.then(&processResult);

    // 等待处理结果
    QString result = processedFuture.result();
    qDebug() << result;

    // 启动事件循环,等待异步任务完成
    return a.exec();
}

上述程序在同一程序运行结果:

好了,相关介绍就到这里。

相关推荐
dot to one1 小时前
Qt 中 QWidget涉及的常用核心属性介绍
开发语言·c++·qt
码农新猿类1 小时前
初入OpenCV
qt·opencv·计算机视觉
洛克希德马丁3 小时前
QLineEdit增加点击回显功能
c++·qt·ui
向日葵xyz4 小时前
Qt5与现代OpenGL学习(十一)OpenGL Widget鼠标控制直线旋转
开发语言·qt·学习
小宋加油啊6 小时前
Mac QT水平布局和垂直布局
开发语言·qt·macos
伐尘18 小时前
【Qt】编译 Qt 5.15.x For Windows 基础教程 Visual Studio 2019 MSVC142 x64
windows·qt·visual studio
吃面不喝汤6618 小时前
破解 Qt QProcess 在 Release 模式下的“卡死”之谜
开发语言·qt
charlie1145141911 天前
逐步理解Qt信号与槽机制
数据库·qt
yaso_zhang1 天前
当生产了~/qt-arm/bin/qmake,可以单独编译其他-源码的某个模块,如下,编译/qtmultimedia
qt
code bean1 天前
【Qt/C++】深入理解 Lambda 表达式与 `mutable` 关键字的使用
开发语言·c++·qt