Qt官方案例map知识点分析

12 篇文章 1 订阅

此案例实际是在讲QtConcurrent的使用方法,但里面也有许多知识点

#include <QImage>
#include <QList>
#include <QThread>
#include <QDebug>
#include <QGuiApplication>
#include <qtconcurrentmap.h>

#include <functional>

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    const int imageCount = 20;

    // Create a list containing imageCount images.
    //创建一个QImage的list,这没啥好说的,看QImage文档就可以
    QList<QImage> images;
    for (int i = 0; i < imageCount; ++i)
        images.append(QImage(1600, 1200, QImage::Format_ARGB32_Premultiplied));

	//std::function和c里的函数指针差不多,但用法比函数指针多,这里是用法之一:lambda表达式,括号内是传入的参数,->返回值
	//函数内容是缩小了图片并返回
    std::function<QImage(const QImage&)> scale = [](const QImage &image) -> QImage
    {
        qDebug() << "Scaling image in thread" << QThread::currentThread();
        return image.scaled(QSize(100, 100), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    };

    // Use QtConcurrentBlocking::mapped to apply the scale function to all the
    // images in the list.
    //进入线程,为每个images调用一次scale,手册上说是函数是阻塞的,QtConcurrent::blockingMappend运行结束后才会继续下一步.但QtConcurrent里每个线程是独立运行
    QList<QImage> thumbnails = QtConcurrent::blockingMapped(images, scale);

    return 0;
}

以下是运行结果:

Scaling image in thread QThread(0x1b687f58)
Scaling image in thread QThread(0x1b542030, name = "Thread (pooled)")
Scaling image in thread QThread(0x1b5515d0, name = "Thread (pooled)")
Scaling image in thread QThread(0x1b542070, name = "Thread (pooled)")
Scaling image in thread QThread(0x1b551790, name = "Thread (pooled)")
Scaling image in thread QThread(0x1b5516f0, name = "Thread (pooled)")
Scaling image in thread QThread(0x1b551650, name = "Thread (pooled)")
Scaling image in thread QThread(0x1b687f58)
Scaling image in thread QThread(0x1b542030, name = "Thread (pooled)")
Scaling image in thread QThread(0x1b5515d0, name = "Thread (pooled)")
Scaling image in thread QThread(0x1b542070, name = "Thread (pooled)")
Scaling image in thread QThread(0x1b551790, name = "Thread (pooled)")
Scaling image in thread QThread(0x1b5515d0, name = "Thread (pooled)")
Scaling image in thread QThread(0x1b551650, name = "Thread (pooled)")
Scaling image in thread QThread(0x1b687f58)
Scaling image in thread QThread(0x1b542030, name = "Thread (pooled)")
Scaling image in thread QThread(0x1b5516f0, name = "Thread (pooled)")
Scaling image in thread QThread(0x1b542070, name = "Thread (pooled)")
Scaling image in thread QThread(0x1b5515d0, name = "Thread (pooled)")
Scaling image in thread QThread(0x1b551650, name = "Thread (pooled)")
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值