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();
}

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

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

相关推荐
用户805533698035 天前
不止三件套:QObject 属性系统全关键字与运行时反射!
c++·qt
xcyxiner5 天前
DicomViewer (vcpkg Windows和ubuntu编译)7
qt
Quz10 天前
QML Hello World 入门示例
qt
xcyxiner13 天前
DicomViewer (dcmtk读取dcm文件)5
qt
xcyxiner14 天前
DicomViewer (后台线程处理文件)4
qt
xcyxiner14 天前
DicomViewer (添加模型类)3
qt
xcyxiner15 天前
DicomViewer (目录调整) 2
qt
xcyxiner15 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt
桥田智能17 天前
桥田智能 QT-650S:面向白车身焊装的 800kg 重载快换解决方案
开发语言·qt·系统架构
森G17 天前
75、服务器源码解析---------云视频服务项目
linux·服务器·网络·c++·qt