最近在学习Qt多线程,发现QtConcurrent代码简单粗暴,尝试着用。
发现并行计算的函数好像不能使类方法:
#include <QtWidgets>
#include <QtConcurrent>
using namespace QtConcurrent;
const int iterations = 20;
void spin(int &iteration)
{
const int work = 1000 * 1000 * 40;
volatile int v = 0;
for (int j = 0; j < work; ++j)
++v;
qDebug() << "iteration" << iteration << "in thread" << QThread::currentThreadId();
}
int main(int argc, char **argv)
{
QApplication app(argc, argv);
// Prepare the vector.
QVector<int> vector;
for (int i = 0; i < iterations; ++i)
vector.append(i);
// Create a progress dialog.
QProgres

本文介绍了在Qt中使用QtConcurrent进行并行计算的尝试。通过一个简单的示例展示了如何使用QtConcurrent::map进行任务分配,并在main函数中创建QProgressDialog跟踪进度。然而,遇到的问题是当尝试将并行计算函数替换为类方法时,出现了编译错误,提示缺少参数。作者将继续研究这个问题以解决类方法在QtConcurrent中的应用。
最低0.47元/天 解锁文章
3766

被折叠的 条评论
为什么被折叠?



