Qt——功能:高清头像(锯齿小)

github:https://github.com/coldfirehello/QtDemo.git

 

最近UI小哥哥检查界面效果,觉得头像显示不够清晰。

当前效果图:

竞品效果图:

原图:

 

 

1.当前效果图:


实现方法:通过样式表实现圆形头像和缩放效果。

实现代码:

QLabel styleSheetLabel;
styleSheetLabel.setFixedSize(40, 40);
styleSheetLabel.setStyleSheet(QString("QLabel{border-radius:20px;border-image:url(%1);}").arg(filePath));

分析:

       原图确实很清晰,但是通过样式表处理后,图片比较模糊。

 

2.第一次优化效果图:

实现方法:使用QPainter在QBitmap上绘制圆形头像,用QPixmap实现缩放后头像。


实现代码:

头像缩放及圆角处理:
QPixmap pixmapToRound(const QPixmap &src, int radius)
{
    if (src.isNull())
    {
        return QPixmap();
    }

    QSize size(2*radius, 2*radius);
    QBitmap mask(size);
    QPainter painter(&mask);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setRenderHint(QPainter::SmoothPixmapTransform);
    painter.fillRect(0, 0, size.width(), size.height(), Qt::white);
    painter.setBrush(QColor(0, 0, 0));
    painter.drawRoundedRect(0, 0, size.width(), size.height(), radius, radius);
    QPixmap image = src.scaled(size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    image.setMask(mask);
    return image;
}

测试代码:
QLabel pixmapLabel;
pixmapLabel.setFixedSize(40, 40);
QPixmap pixmap(filePath);
pixmapLabel.setPixmap(pixmapToRound(pixmap, 20));

UI小哥哥:头像是清晰了很多,但是放大几倍后觉得圆角处理的像“啃过”似的(锯齿严重),不够圆滑。竞品效果很圆滑,你再想想办法吧。

对过来放大后效果:

我:那我再试试……

 

3.最终版效果:


放大后效果:

实现方法:使用QPainter在QPixmap上绘制缩放后的头像,用QPainter的裁剪方法(setClipPath)和QPainterPath实现缩放后头像。


实现代码:

 

UI小哥哥:我觉得效果没什么问题了,你是怎么实现的。

我:裁剪出一个圆形头像。

#include "hdIcon.h"
#include <QPainter>

HDIcon::HDIcon(QWidget *parent) : QLabel(parent)
{

}

HDIcon::~HDIcon()
{

}

void HDIcon::setIconInfo(const QString &filePath, const QSize &scaledSize, int radius)
{
    QPixmap pixmap = pixmapToRound(filePath, scaledSize, radius);

    if(!pixmap.isNull())
    {
        setPixmap(pixmap);
    }
}

QPixmap HDIcon::pixmapToRound(const QString &filePath, const QSize &scaledSize, int radius)
{
    QPixmap headPixmap(scaledSize);

    QPixmap pixmap(filePath);

    if(pixmap.isNull() && !scaledSize.isValid() && radius < 0)
    {
        headPixmap = QPixmap();
    }
    else
    {
        headPixmap.fill(Qt::transparent);

        QPainter painter(&headPixmap);
        QPainterPath painterPath;
        QRect rect(0, 0, scaledSize.width(), scaledSize.height());
        painterPath.addRoundedRect(rect, radius, radius);
        painter.setRenderHint(QPainter::Antialiasing);
        painter.setRenderHint(QPainter::SmoothPixmapTransform);
        painter.setClipPath(painterPath);
        painter.drawPixmap(rect, pixmap.scaled(scaledSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
    }

    return headPixmap;
}

测试代码:
HDIcon hdIcon;
hdIcon.setIconInfo(filePath, QSize(40, 40), 20);

小结:

圆形图像效果对比
实现方法圆形效果清晰度
样式表放大后不圆滑(锯齿严重)模糊
QBitmap、QPainter、QPixmap放大后不圆滑(锯齿严重)清晰
QPainter、QPixmap、QPainterPath放大后圆滑清晰

 

喜欢的点个好看吧,谢谢各位大佬捧场!

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值