'QLabel with image in round shape
I want to display image with round shape in PyQt5/PySide2 application.
Below is the code i tried.
self.statusWidget = QLabel()
img = QImage(":/image.jpg").scaled(49, 49, Qt.IgnoreAspectRatio, Qt.SmoothTransformation)
self.statusWidget.setPixmap(QPixmap.fromImage(img))
self.statusWidget.setStyleSheet("border-radius:20px")
I got below output.
And I want Qlabel like below.
Solution 1:[1]
User avatar QLabel
The best way to make a circular avatar
This method uses the setClipPath QPainter method in conjunction with QPainterPath to crop the image.
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap, QPainter, QPainterPath
from PyQt5.QtWidgets import QLabel, QWidget, QHBoxLayout, QApplication
class Label(QLabel):
def __init__(self, *args, antialiasing=True, **kwargs):
super(Label, self).__init__(*args, **kwargs)
self.Antialiasing = antialiasing
self.setMaximumSize(50, 50)
self.setMinimumSize(50, 50)
self.radius = 25
self.target = QPixmap(self.size())
self.target.fill(Qt.transparent)
p = QPixmap("E:/_Qt/img/qt-logo.png").scaled(
50, 50, Qt.KeepAspectRatioByExpanding, Qt.SmoothTransformation)
painter = QPainter(self.target)
if self.Antialiasing:
painter.setRenderHint(QPainter.Antialiasing, True)
painter.setRenderHint(QPainter.HighQualityAntialiasing, True)
painter.setRenderHint(QPainter.SmoothPixmapTransform, True)
path = QPainterPath()
path.addRoundedRect(
0, 0, self.width(), self.height(), self.radius, self.radius)
painter.setClipPath(path)
painter.drawPixmap(0, 0, p)
self.setPixmap(self.target)
class Window(QWidget):
def __init__(self, *args, **kwargs):
super(Window, self).__init__(*args, **kwargs)
layout = QHBoxLayout(self)
layout.addWidget(Label(self))
layout.addWidget(Label(self, antialiasing=False))
self.setStyleSheet("background: blue;")
if __name__ == "__main__":
app = QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | S. Nick |