如何使用Qt多线程进行并行列表处理?

How to use Qt multithreading for parallel list processing?

我正在使用 qt 制作分析大量数据的软件。数据由带有订单时间、开始位置和结束位置等信息的单个“Uber”订单组成,我需要能够评估数据,例如绘制随时间变化的需求图。

为此,我必须检查数据的每条记录并根据时间戳将其汇总到新数据table,这需要很长时间所以我最初的解决方案是使用QtConcurrent::filterReduced 得到我的总和。

但是,过滤函数不能接受额外的参数来根据我想要的时间间隔过滤数据。

我的问题是,对于此类问题是否有另一种快速简便的解决方案?或者我是否需要为此使用 QThread 的低级 API,如果需要,请问 examples/tutorials 如何实现?

您可以传递一个包含“参数”的函数对象,而不是传递一个函数。 像这样(这里 T 是你的数据类型):

struct FilterWithTime
{
    FilterWithTime(const QString &filterPredicate)
    : m_filterPredicate(filterPredicate) { }

    typedef bool result_type;

    bool operator()(const T &value)
    {
        ... test value against filterPredicate
    }

    QString m_filterPredicate;
};

QtConcurrent::filterReduced<ResultType>(your-list-of-T, FilterWithTime(QString("10-12"), YourTransformationObject()));

注意使用 ResultType 的显式实例化!!