Qt将图片绘制成圆形

cpp 复制代码
QPixmap roundedPixmap(const QPixmap& src, QSize size, int radius) {
    QPixmap scaled = src.scaled(size, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
    QPixmap dest(size);
    dest.fill(Qt::transparent);

    QPainter painter(&dest);
    painter.setRenderHint(QPainter::Antialiasing);
    QPainterPath path;
    path.addRoundedRect(0, 0, size.width(), size.height(), radius, radius);
    painter.setClipPath(path);
    painter.drawPixmap(0, 0, scaled);

    return dest;
}

QPixmap rounded = roundedPixmap(QPixmap("xxx.jpg"), ui->portrait_label->size(), 20); // 设置圆角半径

ui->portrait_label->setPixmap(rounded);

相关推荐
AI视觉网奇4 分钟前
ue 5.5 c++ mqtt 订阅/发布 json
网络·c++·json
程序员-King.10 分钟前
day159—动态规划—打家劫舍(LeetCode-198)
c++·算法·leetcode·深度优先·回溯·递归
txinyu的博客12 分钟前
解析muduo源码之 StringPiece.h
开发语言·网络·c++
墨雪不会编程21 分钟前
C++【string篇4】string结尾篇——字符编码表、乱码的来源及深浅拷贝
android·开发语言·c++
CSDN_RTKLIB1 小时前
【map应用】组合键统计
c++·stl
txinyu的博客1 小时前
解析muduo源码之 TimeZone.h & TimeZone.cc
linux·服务器·网络·c++
小-黯1 小时前
QT编译MySQL驱动教程(Windows/Linux)
windows·qt·mysql
爱吃生蚝的于勒1 小时前
【Linux】零基础学习命名管道-共享内存
android·linux·运维·服务器·c语言·c++·学习
陳10301 小时前
C++:继承
开发语言·c++
txinyu的博客1 小时前
解析muduo源码之 atomic.h
服务器·c++