1

How would I apply a border radius, or otherwise achieve a rounded-corner effect, with a QMovie in PyQt5? It doesn't seem to react to QSS. Although I do not believe it to be relevant, here is my current code anyways, to give an idea what I have tried:

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

with QSS:

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

I have also tried painting the current pixmap each paintEvent by subclassing a QWidget, but nothing appears, and the pixmap has dimensions of 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)

I also know the above implementation would not work because a paintEvent will not occur often enough to play the movie as expected

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
AlgoRythm
  • 1,196
  • 10
  • 31

1 Answers1

3

A possible solution is to implement a 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

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • How could I implement a minimum width? – AlgoRythm Jan 17 '19 at 20:57
  • @AlgoRythm What do you mean with a minimum width? These properties must be established in the QLabel – eyllanesc Jan 17 '19 at 20:59
  • If I set a fixed size for the QLabel which is larger than the gif, the gif is drawn in the middle left with blank space all around it. – AlgoRythm Jan 17 '19 at 21:02
  • 2
    @AlgoRythm use `w = QtWidgets.QLabel(alignment=QtCore.Qt.AlignCenter, scaledContents=True)` – eyllanesc Jan 17 '19 at 21:05
  • That works! Is there a simple way to apply anti-aliasing? I saw in another answer while researching this question that I would need to use ImageReader and essentially draw the gif myself frame by frame – AlgoRythm Jan 17 '19 at 21:07
  • 1
    @AlgoRythm add `p.setRenderHint(QtGui.QPainter.Antialiasing)` after `p = QtGui.QPainter(pix)` – eyllanesc Jan 17 '19 at 21:11