如何在 QPixmap 周围绘制边框/轮廓

问题描述

我正在尝试在 QPixmap 周围添加黑色边框。 我愚蠢地认为缩小它会做到这一点,但事实并非如此,正如您在此处看到的:

enter image description here

我尝试了很多东西,QGraphicsPixmapItem()QGraphicsDropShadowEffect(),但我想我错过了一些东西。我需要创建一个投影效果或其他东西,然后以某种方式将其绘制回 QPixmap 下。还是只向外填充几个像素?

这是我一直在使用的一些代码:

icon = "iconpath/icon.png"
base_pixmap = QtGui.QPixmap(icon)
color = QtGui.QColor("green")

# set black outline
mask = base_pixmap.createMaskFromColor(QtCore.Qt.transparent,QtCore.Qt.MaskInColor)
base_pixmap.fill(QtGui.QColor("black"))

# set mask
base_pixmap.setMask(mask)

# Take icon,edit size,add to pixmap
pixmap_image = QtGui.QPixmap(icon)

mask = pixmap_image.createMaskFromColor(QtCore.Qt.transparent,QtCore.Qt.MaskInColor)
pixmap_image.fill(color)

# set mask
pixmap_image.setMask(mask)

painter = QtGui.QPainter(self)
margin = 10  # shrink image to fit inside border
height_percent = (float(base_pixmap.height()) / 100) * float(margin)
width_percent = (float(base_pixmap.width()) / 100) * float(margin)

painter.drawPixmap(QtCore.QRect(width_percent / 2,height_percent / 2,self.width() - width_percent,self.height() - height_percent),pixmap_image)

painter.end()

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...