Qt开发:QtConcurrent介绍和使用

文章目录

一、QtConcurrent 简介

QtConcurrent 是 Qt 提供的一个高级并发编程模块,属于 QtConcurrent 命名空间,旨在简化多线程任务的执行。它支持并行执行算法(如 map、filter、reduce),还支持异步任务运行和结果管理,不需要显式管理 QThread。

模块介绍

头文件包含:

cpp 复制代码
#include <QtConcurrent>

模块依赖:

cpp 复制代码
QT += concurrent

常用功能:

二、常用功能分类

2.1 异步运行一个函数(无返回值)

cpp 复制代码
QtConcurrent::run([]() {
    qDebug() << "后台线程中执行任务:" << QThread::currentThread();
});

2.2 异步运行一个带参数的函数(有返回值)

cpp 复制代码
int add(int a, int b) {
    return a + b;
}

QFuture<int> future = QtConcurrent::run(add, 3, 5);

// 后续获取结果
int result = future.result();  // 阻塞直到完成
qDebug() << "结果是:" << result;

2.3 绑定类成员函数

cpp 复制代码
class Worker {
public:
    int multiply(int x, int y) {
        return x * y;
    }
};

Worker worker;
QFuture<int> future = QtConcurrent::run(&worker, &Worker::multiply, 4, 6);

2.4 容器并行处理(map)

并发修改容器元素(原地修改)

cpp 复制代码
QList<int> numbers = {1, 2, 3, 4, 5};

auto doubleIt = [](int &n) {
    n *= 2;
};

QtConcurrent::map(numbers, doubleIt);

// 输出: 2 4 6 8 10

并发映射容器到新容器(mapped)

cpp 复制代码
QList<int> numbers = {1, 2, 3};

auto square = [](int n) {
    return n * n;
};

QList<int> squares = QtConcurrent::mapped(numbers, square).results();
// 输出: 1 4 9

并发过滤(filtered)

cpp 复制代码
QStringList names = {"Alice", "Bob", "Eve"};

auto isShort = [](const QString &name) {
    return name.length() <= 3;
};

QStringList shortNames = QtConcurrent::filtered(names, isShort).results();
// 输出: Bob, Eve

2.5 使用 QFutureWatcher 监听结果(推荐与 UI 配合)

cpp 复制代码
QFutureWatcher<QString> *watcher = new QFutureWatcher<QString>(this);

connect(watcher, &QFutureWatcher<QString>::finished, this, [=]() {
    QString result = watcher->result();
    qDebug() << "计算完成,结果为:" << result;
});

QFuture<QString> future = QtConcurrent::run([]() {
    QThread::sleep(2);
    return QString("Hello from thread");
});

watcher->setFuture(future);

三、线程池控制

QtConcurrent 默认使用全局 QThreadPool,可通过如下方式调整:

cpp 复制代码
QThreadPool::globalInstance()->setMaxThreadCount(8);

或者使用局部线程池:

cpp 复制代码
QThreadPool pool;
QtConcurrent::run(&pool, someFunction);

四、取消任务

可通过 QFuture 中的 cancel() 方法取消任务:

cpp 复制代码
QFuture<void> future = QtConcurrent::run([]{
    for (int i = 0; i < 100; ++i) {
        if (QThread::currentThread()->isInterruptionRequested()) {
            return;
        }
        QThread::msleep(100);
    }
});

// 取消任务
future.cancel();

五、典型应用场景

六、完整示例:并发下载图片

效果目标:

核心类:ImageDownloader

cpp 复制代码
// ImageDownloader.h

#ifndef IMAGEDOWNLOADER_H
#define IMAGEDOWNLOADER_H

#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QSemaphore>

class ImageDownloader : public QObject
{
    Q_OBJECT
public:
    explicit ImageDownloader(QObject *parent = nullptr);

    void setMaxConcurrent(int count);  // 设置最大并发数
    void downloadImages(const QStringList &urls, const QString &saveDir);

signals:
    void imageDownloaded(const QString &url, const QString &filePath);
    void downloadFailed(const QString &url, const QString &error);
    void allFinished();

private:
    void downloadOne(const QString &url, const QString &saveDir, int retryCount = 3);

    QSemaphore m_semaphore;   // 控制并发数
    int m_maxConcurrent = 5;
};

#endif // IMAGEDOWNLOADER_H
cpp 复制代码
// ImageDownloader.cpp

#include "ImageDownloader.h"
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QNetworkRequest>
#include <QThread>
#include <QDebug>
#include <QtConcurrent>

ImageDownloader::ImageDownloader(QObject *parent)
    : QObject(parent), m_semaphore(m_maxConcurrent)
{}

void ImageDownloader::setMaxConcurrent(int count)
{
    m_maxConcurrent = count;
    m_semaphore = QSemaphore(m_maxConcurrent);
}

void ImageDownloader::downloadImages(const QStringList &urls, const QString &saveDir)
{
    for (const QString &url : urls) {
        QtConcurrent::run([=]() {
            m_semaphore.acquire(); // 限制并发
            downloadOne(url, saveDir);
            m_semaphore.release();
        });
    }

    // 可选:全部下载完再发信号(略)
}

void ImageDownloader::downloadOne(const QString &urlStr, const QString &saveDir, int retryCount)
{
    QNetworkAccessManager manager;
    QNetworkRequest request(QUrl(urlStr));

    QNetworkReply *reply = manager.get(request);
    QEventLoop loop;

    QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
    loop.exec();

    if (reply->error() == QNetworkReply::NoError) {
        QByteArray data = reply->readAll();

        QUrl url(urlStr);
        QString fileName = QFileInfo(url.path()).fileName();
        QString fullPath = QDir(saveDir).filePath(fileName);

        QFile file(fullPath);
        if (file.open(QIODevice::WriteOnly)) {
            file.write(data);
            file.close();
            emit imageDownloaded(urlStr, fullPath);
        } else {
            emit downloadFailed(urlStr, "文件保存失败");
        }
    } else {
        if (retryCount > 0) {
            qDebug() << "下载失败,重试:" << urlStr;
            downloadOne(urlStr, saveDir, retryCount - 1);
        } else {
            emit downloadFailed(urlStr, reply->errorString());
        }
    }

    reply->deleteLater();
}

使用示例:

cpp 复制代码
ImageDownloader *downloader = new ImageDownloader(this);
downloader->setMaxConcurrent(5); // 最多5张图同时下载

QStringList urls = {
    "https://example.com/a.jpg",
    "https://example.com/b.jpg",
    "https://example.com/c.jpg"
};

QString savePath = QDir::currentPath() + "/images";

connect(downloader, &ImageDownloader::imageDownloaded, this, [](const QString &url, const QString &path){
    qDebug() << "下载成功:" << url << " -> " << path;
});

connect(downloader, &ImageDownloader::downloadFailed, this, [](const QString &url, const QString &error){
    qWarning() << "下载失败:" << url << error;
});

downloader->downloadImages(urls, savePath);
相关推荐
郝学胜-神的一滴2 小时前
Qt 高级开发 011: 跨线程信号槽实战
开发语言·c++·qt·程序人生·开源软件·用户界面
小短腿的代码世界3 小时前
QHttpEngine深度解析:Qt嵌入式HTTP服务端的工业级架构与性能调优
qt·http·架构
学习,学习,在学习3 小时前
Qt 串口通讯架构
开发语言·c++·qt·架构·qt5
Shadow(⊙o⊙)4 小时前
qt信号和槽链接的接入与断开
开发语言·前端·c++·qt·学习
sycmancia14 小时前
Qt——编辑交互功能的实现
开发语言·qt
qq_4017004119 小时前
Qt 项目中使用 QSS 的全面总结
开发语言·qt
小短腿的代码世界19 小时前
信号路由风暴:Qt算法交易系统的高频信号分发架构
qt·算法·架构
郝学胜-神的一滴1 天前
Qt 高级开发 010: 从跨界面传值到自定义信号
开发语言·c++·qt·程序人生·用户界面
Hua-Jay1 天前
OpenCV联合C++/Qt 学习笔记(二十三)----图像校正及单目位姿估计
c++·笔记·qt·opencv·学习·计算机视觉
mirror_zAI1 天前
C++ 仿 QQ 聊天室项目:Qt 客户端 + epoll 服务端 + Reactor 架构(含源码)
c++·qt·架构