第一种,使用lambda表达式
1 QFuture<void> future = QtConcurrent::run([=]() {
2 // Code in this block will run in another thread
3 });
4 ...
第二种,使用成员函数
调用QByteArray的成员函数split()
1 // call 'QList<QByteArray> QByteArray::split(char sep) const' in a separate thread
2 QByteArray bytearray = "hello world";
3 QFuture<QList<QByteArray> > future = QtConcurrent::run(bytearray, &QByteArray::split, ',');
4 ...
5 QList<QByteArray> result = future.result();
调用非成员函数:
1 // call 'void QImage::invertPixels(InvertMode mode)' in a separate thread
2 QImage image = ...;
3 QFuture<void> future = QtConcurrent::run(&image, &QImage::invertPixels, QImage::InvertRgba);
4 ...
5 future.waitForFinished();
6 // At this point, the pixels in 'image' have been inverted