Qt处理图片:设置图片圆角样式,支持全圆角和部分圆角

全圆角效果:
在这里插入图片描述
部分圆角效果:
在这里插入图片描述
/**给图片添加圆角样式
*srcPixMap:待处理的图片
*rect:图片尺寸
*radius:圆角大小
***/

QPixmap getRoundRectPixmap(QPixmap srcPixMap, QRect rect, int radius)
{
	//不处理空数据或者错误数据
	if (srcPixMap.isNull()) {
		return srcPixMap;
	}

	int imageWidth = rect.width();
	int imageHeight = rect.height();

	//处理大尺寸的图片,保证图片显示区域完整
	QPixmap newPixMap = srcPixMap.scaled(imageWidth, (imageHeight == 0 ? imageWidth : imageHeight),Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
	
	QPixmap destImage(imageWidth, imageHeight);
	destImage.fill(Qt::transparent);
	QPainter painter(&destImage);
	// 抗锯齿
	painter.setRenderHints(QPainter::Antialiasing, true);
	// 图片平滑处理
	painter.setRenderHints(QPainter::SmoothPixmapTransform, true);
	// 将图片裁剪为圆角
	QPainterPath path;
	path.addRoundedRect(rect, radius, radius);
	painter.setClipPath(path);
	painter.drawPixmap(0, 0, imageWidth, imageHeight, newPixMap);
	return destImage;
}

猜你喜欢

转载自blog.csdn.net/qq_36651243/article/details/106388069
今日推荐