gpt4 book ai didi

c++ - 如何从 QtConcurrent::run 函数(或类似函数)向 QFutureWatcher 传达 progressText?

转载 作者:行者123 更新时间:2023-12-04 21:40:25 24 4
gpt4 key购买 nike

如果我使用 QtConcurrent::run 启动一些异步执行函数,并使用 QFutureWatcher 监控返回的 future ,如果我可以在异步执行函数中做些什么来传达一些进度文本,这将导致 QFutureWatcher发射它的 progressTextChanged信号?

即我想做的是:

void fn() {
???->setProgressText("Starting);
...
???->setProgressText("halfway");
...
???->setProgressText("done!");
}

QFutureWatcher watcher;
connect(&watcher, SIGNAL(progressTextChanged(const QString&)), &someGuiThing, SLOT(updateProgress(const QString&)));
connect(&watcher, SIGNAL(finished(), &someGuiThing, SLOT(doStuff()));
QFuture<void> future=QConcurrent::run(fn);
watcher.setFuture(future);

然而 , 大问题 , QtConcurrent::run documentation清楚地说明

Note that the QFuture returned by QtConcurrent::run() does not support canceling, pausing, or progress reporting. The QFuture returned can only be used to query for the running/finished status and the return value of the function.



那么我能做的最简单的事情是什么,它会让我在功能上等同于上述尝试做的事情?我要放弃吗 QtConcurrent::run ? QFuture ?两个都? (然后回到 QThread 和排队连接?)

最佳答案

QtConcurrent 函数返回的 QFuture,如 QtConcurrent::mappedReduced()具有由 progressValue()、progressMinimum()、progressMaximum() 和 progressText() 函数提供的进度信息。不像 QtConcurrent::run()它不会自动提供这样的东西。
QtConcurrent::run()不自动提供进度信息,如 QtConcurrent::mappedReduced() .但是您可以使用信号拥有自己的进度报告机制。我不认为有任何其他方式是直接的。

关于c++ - 如何从 QtConcurrent::run 函数(或类似函数)向 QFutureWatcher 传达 progressText?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23438044/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com