gpt4 book ai didi

python - 带边框半径的 QMovie

转载 作者:行者123 更新时间:2023-12-01 01:17:05 25 4
gpt4 key购买 nike

如何在 PyQt5 中使用 QMovie 应用边框半径或以其他方式实现圆角效果?它似乎对QSS没有反应。虽然我不认为它是相关的,但无论如何,这是我当前的代码,以给出我尝试过的内容:

image = QLabel()
image.setObjectName("rant-image")
movie = QMovie("image_cache/" + img_name)
image.setMovie(movie)
movie.start()

使用 QSS:

QLabel#rant-image{
border-radius: 5px;
}

我还尝试通过子类化 QWidget 来在每个 PaintEvent 中绘制当前像素图,但没有出现任何内容,并且像素图的尺寸为 0:

invisible_pen = QPen()
invisible_pen.setWidth(0)
invisible_pen.setStyle(Qt.NoPen)


class RoundedMovie(QWidget):

def __init__(self, movie, parent=None):
QWidget.__init__(self, parent)
self.movie = movie

def setMovie(self, movie):
self.movie = movie

def paintEvent(self, event):
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing, True)
pixmap = self.movie.currentPixmap()
brush = QBrush(pixmap)
rect = QRect(0, 0, pixmap.width() - 10, pixmap.height() - 10)
painter.setPen(invisible_pen)
painter.setBrush(brush)
painter.drawRoundedRect(rect, 5, 5)

我也知道上面的实现不起作用,因为 PaintEvent 发生的频率不足以按预期播放电影

最佳答案

一个可能的解决方案是实现QProxyStyle:

from PyQt5 import QtCore, QtGui, QtWidgets

class RoundPixmapStyle(QtWidgets.QProxyStyle):
def __init__(self, radius=10, *args, **kwargs):
super(RoundPixmapStyle, self).__init__(*args, **kwargs)
self._radius = radius

def drawItemPixmap(self, painter, rectangle, alignment, pixmap):
painter.save()
pix = QtGui.QPixmap(pixmap.size())
pix.fill(QtCore.Qt.transparent)
p = QtGui.QPainter(pix)
p.setBrush(QtGui.QBrush(pixmap))
p.setPen(QtCore.Qt.NoPen)
p.drawRoundedRect(pixmap.rect(), self._radius, self._radius)
p.end()
super(RoundPixmapStyle, self).drawItemPixmap(painter, rectangle, alignment, pix)
painter.restore()

if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = QtWidgets.QLabel(alignment=QtCore.Qt.AlignCenter)
proxy_style = RoundPixmapStyle(radius=20, style=w.style())
w.setStyle(proxy_style)
movie = QtGui.QMovie("foo.gif")
w.setMovie(movie)
movie.start()
w.resize(640, 480)
w.show()
sys.exit(app.exec_())

enter image description here

关于python - 带边框半径的 QMovie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54230005/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com