这个例子对数据处理有很大的用处,在此记录下。
官方对应这个例子解析如下:
QtConcurrent Map exapmle展示了使用QtConcurrent API的同步(阻塞)接口对图片进行拉伸。这个程序是控制台程序。
一共有两个文件:
main.cpp
#include <QImage>
#include <QList>
#include <QThread>
#include <QDebug>
#include <QGuiApplication>
#include <qtconcurrentmap.h>
QImage scale(const QImage &image)
{
qDebug() << "Scaling image in thread" << QThread::currentThread();
return image.scaled(QSize(100, 100), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
}
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
const int imageCount = 20;
// Create a list containing imageCount images.
QList<QImage> images;
for (int i = 0; i < imageCount; ++i)
images.append(QImage(1600, 1200, QImage::Format_ARGB32_Premultiplied));
// Use QtConcurrentBlocking::mapped to apply the scale function to all the
// images in the list.
QList<QImage> thumbnails = QtConcurrent::blockingMapped(images, scale);
return 0;
}
map.pro
TEMPLATE = app
TARGET = mapdemo
QT += concurrent
CONFIG += console
CONFIG -= app_bundle
SOURCES += main.cpp
target.path = $$[QT_INSTALL_EXAMPLES]/qtconcurrent/map
INSTALLS += target
程序运行截图如下:
这里可以看到有很多进程去处理。
下面对上面的关键代码进行解释:
T QtConcurrent::blockingMapped(const Sequence &sequence, MapFunction function)
对sequence中的每一个调用下MapFunction,此函数的返回值为MapFunction的返回值的集合。
注意:这个函数是会被阻塞的,但所有项都执行完function后才会向下继续运行。
QImage::Format_ARGB32_Premultiplied:一种图片存储格式;
Qt::IgnoreAspectRatio:放缩自由,不保留高度。
Qt::SmoothTransformation:使用双线性滤波,对图片进行平滑出处理。