1

I have a QLabel displaying a rectangular image. The corners of the image should be cut to be circular.

I use PyQt5 and have no idea on how to do it. I tried to set a painter and draw a roundedrect, but it shows on top of my image. I could use some help.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
b_dani
  • 19
  • 1
  • 5

2 Answers2

2

Here is one way to do this. This works by drawing a rounded rect on an initially transparent pixmap using the original pixmap as the brush for the painter.

label = QtWidgets.QLabel()

# original
pixmap = QtGui.QPixmap('example.jpg')
radius = 30

# create empty pixmap of same size as original 
rounded = QtGui.QPixmap(pixmap.size())
rounded.fill(QtGui.QColor("transparent"))

# draw rounded rect on new pixmap using original pixmap as brush
painter = QtGui.QPainter(rounded)
painter.setRenderHint(QtGui.QPainter.Antialiasing)
painter.setBrush(QtGui.QBrush(pixmap))
painter.setPen(QtCore.Qt.NoPen)
painter.drawRoundedRect(pixmap.rect(), radius, radius)

# set pixmap of label
label.setPixmap(rounded)
Heike
  • 24,102
  • 2
  • 31
  • 45
1

Here is an example to show rounded image inside QLabel by using Qt Style Sheets.

from PyQt5.QtWidgets import QApplication, QLabel                                                                                       
                                                                                                                                       
app = QApplication([])                                                                                                                 

label=QLabel("\n\                                                                                                                      
  QLable StyleSheet Demo \n\                                                                                                           
  QLable StyleSheet Demo \n\                                                                                                           
  QLable StyleSheet Demo \n\                                                                                                           
  QLable StyleSheet Demo \n\                                                                                                           
  QLable StyleSheet Demo \n\                                                                                                           
  QLable StyleSheet Demo \n\                                                                                                           
  ")                                                                                                                                   
label.setStyleSheet(" \                                                                                                                
  border-image: url('demo.jpg'); \                                                                                                     
  background-color: black; \                                                                                                           
  border-radius: 50%; \                                                                                                                
  ")                                                                                                                                   
label.setMargin(20);                                                                                                                   
label.setScaledContents(True);                                                                                                         
label.show()                                                                                                                           
app.exec_()                                                                                                                            

Here is what it looks like:

enter image description here

Dharman
  • 30,962
  • 25
  • 85
  • 135
tarle
  • 66
  • 4