想把QLabel中传入的方形图片切成原型显示,尝试用样式表写圆角能完成纯背景色切成圆形显示,但是不能将图片切成圆形。
于是上网搜寻答案,找到了解决我疑惑的帖子
贴代码
QPixmap L::PixmapToRound(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(), 99, 99);
QPixmap image = src.scaled(size);
image.setMask(mask);
return image;
}
//使用方法 图片56*56
QPixmap pixmap_userIcon;
pixmap_userIcon.load(":/png/images/t_head.png");
QPixmap fitpixmap_userIcon = pixmap_userIcon.scaled(56, 56, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
fitpixmap_userIcon = PixmapToRound(fitpixmap_userIcon, 28);
m_ui.label_photo->setPixmap(fitpixmap_userIcon);