主机网站建设制作,美橙互联网站建设,搞一个网站花多少钱,鞍山做网站企业Qt6 在 QFutureInterface 基础上进一步封装出了 QPromise 类#xff0c;搭配 QFuture 使用#xff0c;逻辑类似 std::promise std::future#xff0c;线程内使用 promise 设置进度和结果#xff0c;外部通过关联的 future 判断状态和获取结果。
#include futures…Qt6 在 QFutureInterface 基础上进一步封装出了 QPromise 类搭配 QFuture 使用逻辑类似 std::promise std::future线程内使用 promise 设置进度和结果外部通过关联的 future 判断状态和获取结果。#include future std::promiseT p; // 获取与 promise 关联的 future std::futureT f p.get_future(); // 线程 A 中设置值 p.set_value(value); // 线程 B 中阻塞获取 promise 设置的值 T result f.get();使用 QFuture 的优势在于搭配 QFutureWatcher 使用时可以获取 started / finished 等相关信号这是 QFutureInterface 内部生成的事件被 QFutureWatcher 接收后发出的。当需要后台执行任务时界面上一般会有相应的进度提示借助 QPromise / QFuture 相关接口可以很容易的实现这个功能。在此基础上做一点小小的封装就得到了一个简易的任务管理类。封装时一个麻烦的地方是QFutureWatcher 需要在关联 QFuture 之前关联信号槽这样我们的 run / start 开始任务接口就需要把所有的槽和槽接收者作为参数。不过这样写起来有点麻烦就干脆拆成了两部分先生成 QFutureWatcher关联好信号槽后再开始任务。另一个问题是 QPromise / QFuture 的模板类型参数我使用了 QObject*懒得自定义基类主要还是考虑到后续进行 QML 版封装用模板不方便子类化任务返回类型实现起来更顺手一点。后续扩展如果要在 QML 中使用任务接口和返回的 future 对象就需要封装。TaskManager 实现代码#pragma once #include QThreadPool #include QFuture #include QFutureWatcher #include QPromise #include functional #include list // 任务管理 class TaskManager : public QObject { Q_OBJECT Q_DISABLE_COPY_MOVE(TaskManager) private: explicit TaskManager(QObject *parent nullptr); public: ~TaskManager(); static TaskManager *instance(); // 任务结果暂时为QObject*可以用一个自定义基类包含type判断类型 using TPromise QPromiseQObject*; using TWatcher QFutureWatcherQObject*; // 创建任务future watcher // 如果将槽函数作为run参数传入代码有点乱拆分为两步先创建watcher关联信号槽再run任务 auto create() - std::shared_ptrTWatcher; // 执行任务繁忙时任务排队 void run(const std::functionvoid (TPromise ) task, std::shared_ptrTWatcher watcher); // 判断是否繁忙 bool busy() const; // 取消所有任务 void cancel(); // 等待全部任务结束 void wait(); signals: void taskStarted(); void taskFinished(); private: // 任务线程池 QThreadPool taskPool; // 内部管理任务列表 std::liststd::shared_ptrTWatcher taskList; // 任务列表锁 std::mutex listMutex; }; TaskManager::TaskManager(QObject *parent) : QObject{parent} { // 线程池最大线程数 taskPool.setMaxThreadCount(QThread::idealThreadCount()); // 线程空闲时销毁判定时间单位ms默认半分钟 taskPool.setExpiryTimeout(1000 * 60); } TaskManager::~TaskManager() { cancel(); wait(); } TaskManager *TaskManager::instance() { static TaskManager manager; return manager; } auto TaskManager::create() - std::shared_ptrTWatcher { return std::make_sharedTWatcher(); } void TaskManager::run(const std::functionvoid (TPromise ) task, std::shared_ptrTWatcher watcher) { if (!task || !watcher) return; TPromise promise{}; auto future promise.future(); watcher-setFuture(future); { // 添加到任务列表 std::lock_guardstd::mutex guard(listMutex); Q_UNUSED(guard) taskList.push_back(watcher); // qDebug() push task; } // 丢进线程池泡澡 taskPool.start([this, task, watcher, promise std::move(promise)]() mutable { promise.start(); task(promise); promise.finish(); { // 任务结束从列表移除 std::lock_guardstd::mutex guard(listMutex); Q_UNUSED(guard) taskList.remove(watcher); // qDebug() remove task; } }); } bool TaskManager::busy() const { // 活跃线程数达到上限 return (taskPool.activeThreadCount() taskPool.maxThreadCount()); } void TaskManager::cancel() { // 移除未启动的任务 taskPool.clear(); // 取消所有任务 std::lock_guardstd::mutex guard(listMutex); Q_UNUSED(guard) for (auto handle : taskList) { handle-cancel(); } } void TaskManager::wait() { // 等待最后一个线程开始 taskPool.waitForDone(-1); }测试代码void MainWindow::onRun() { // 先创建future watcher关联信号槽 auto pwatcher TaskManager::instance()-create(); auto watcher pwatcher.get(); // 列表更新 auto table ui-tableWidget; int row table-rowCount(); table-insertRow(row); QString time QTime::currentTime().toString(hh:mm:ss.zzz); auto title new QLabel(time); table-setCellWidget(row, 0, title); auto bar new QProgressBar(); table-setCellWidget(row, 1, bar); auto btn new QPushButton(Cancel); table-setCellWidget(row, 2, btn); connect(btn, QPushButton::clicked, [pwatcher]{ pwatcher-cancel(); }); // 测试对象释放 connect(btn, QPushButton::destroyed, this, [time]{ qDebug() free time; }); // 状态更新 connect(watcher, TaskManager::TWatcher::progressTextChanged, this, [](const QString text){ // qDebug() progress text changed text; }); connect(watcher, TaskManager::TWatcher::progressRangeChanged, this, [this, bar](int min, int max){ // qDebug() progress range changed min max; bar-setRange(min, max); bar-setValue(min); }); connect(watcher, TaskManager::TWatcher::progressValueChanged, this, [this, bar](int value){ // qDebug() progress changed value; bar-setValue(value); }); connect(watcher, TaskManager::TWatcher::started, this, [time](){ qDebug() started time; }); connect(watcher, TaskManager::TWatcher::finished, this, [this, table, time, btn, pwatcher](){ qDebug() finished time; int index -1; for (int row 0; row table-rowCount(); row) { QPushButton *item static_castQPushButton *(table-cellWidget(row, 2)); if (item item btn) { index row; break; } } if (index 0) return; ui-tableWidget-removeRow(index); }); connect(watcher, TaskManager::TWatcher::canceled, this, [this, time](){ qDebug() canceled time; }); connect(watcher, TaskManager::TWatcher::resultReadyAt, this, [this, time, pwatcher](int index){ qDebug() result time index pwatcher-resultAt(index); }); // 执行任务并通过关联的future watcher更新ui auto task [](TaskManager::TPromise promise){ promise.setProgressRange(0, 10); for (int i 0; i 10 !promise.isCanceled(); i) { promise.setProgressValue(i); QThread::sleep(1); } if (promise.isCanceled()) return; promise.setProgressValue(10); promise.addResult(nullptr); }; TaskManager::instance()-run(task, pwatcher); } void MainWindow::onCancel() { TaskManager::instance()-cancel(); }完整代码https://github.com/gongjianbo/MyTestCode/tree/master/Qt6/TestQt_20251220_Future