使用QtConcurrent::mapped()
and aQFutureWatcher
使所有处理异步发生。
QFutureWatcher
finished()
当所有处理完成时发出信号。结果由一个QFuture
由QFutureWatcher
. 最重要的是,用户界面永远不会冻结。
my_project.pro:
# must include "concurrent" to use QtConcurrent::mapped()
QT += concurrent
...
映射处理器.h:
#include <QObject>
#include <QFutureWatcher>
#include <vector>
class MappedProcessor : public QObject
{
Q_OBJECT
public:
explicit MappedProcessor(QObject *parent = nullptr);
public slots:
void processValues();
private slots:
void handleProcessingFinished();
private:
static double myFunction(double value);
QFutureWatcher<double> m_futureWatcher;
};
映射处理器.cpp:
#include "MappedProcessor.h"
#include <QtConcurrent/QtConcurrent>
#include <QList>
#include <QThread>
MappedProcessor::MappedProcessor(QObject *parent) : QObject(parent)
{
connect(&m_futureWatcher, &QFutureWatcher<double>::finished,
this, &MappedProcessor::handleProcessingFinished);
}
void MappedProcessor::processValues()
{
// create some values to process
std::vector<double> parameters;
for (auto i = 0; i < 16; ++i)
parameters.push_back(i);
auto sequence = QVector<double>::fromStdVector(parameters);
auto future = QtConcurrent::mapped(sequence, myFunction);
m_futureWatcher.setFuture(future);
}
void MappedProcessor::handleProcessingFinished()
{
// convert the results to std::vector<double>
auto theResults = m_futureWatcher.future().results().toVector().toStdVector();
qDebug() << "Received" << theResults.size() << "results";
}
// pretend to be a long running calculation...
double MappedProcessor::myFunction(double value)
{
QThread::msleep(10000);
return value;
}