I’m currently working on a PyQt5 application where I have a translucent widget with rounded corners inside a translucent window. However, despite setting the border-radius property for the widget, the corners are not appearing rounded as expected.
I expect the translucent widget to have rounded corner but for whatever reson it won’t work.
I’m using pyqt 5.15.10 and python 3.11
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
from PyQt5.QtCore import Qt, QPoint
from PyQt5.QtGui import QFont
class TranslucentWidget(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.setGeometry(0, 0, 500, 600)
self.setStyleSheet(
"background-color: rgba(255, 255, 255, 150);"
"border-radius: 20px;"
)
class TranslucentWindow(QWidget):
def __init__(self):
super().__init__()
self.initUI()
self.oldPos = self.pos()
def initUI(self):
self.setFixedSize(500, 600) # Set fixed width and height
self.setWindowTitle('Translucent Window')
self.setStyleSheet(
"background: transparent;"
"border-radius: 20px;" # Set border radius for the window
)
self.setWindowOpacity(0.9) # Set window opacity to 0.9 to make it transparent
# Remove the title bar
self.setWindowFlags(Qt.FramelessWindowHint)
# Load Roboto font
font = QFont()
font.setFamily("Roboto")
# Create translucent widget
self.translucentWidget = TranslucentWidget(self)
# Create minimize button
self.minimizeButton = QPushButton('_', self.translucentWidget)
self.minimizeButton.setGeometry(430, 10, 30, 30)
self.minimizeButton.setFont(font) # Set font to Roboto
self.minimizeButton.setStyleSheet(
"background-color: white;" # Set background color to opaque white
"border-radius: 15px;"
)
self.minimizeButton.clicked.connect(self.showMinimized)
# Create close button
self.closeButton = QPushButton('X', self.translucentWidget)
self.closeButton.setGeometry(465, 10, 30, 30)
self.closeButton.setFont(font) # Set font to Roboto
self.closeButton.setStyleSheet(
"background-color: white;" # Set background color to opaque white
"border-radius: 15px;"
)
self.closeButton.clicked.connect(self.close)
def mousePressEvent(self, event):
self.oldPos = event.globalPos()
def mouseMoveEvent(self, event):
delta = QPoint (event.globalPos() - self.oldPos)
self.move(self.x() + delta.x(), self.y() + delta.y())
self.oldPos = event.globalPos()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = TranslucentWindow()
window.show()
sys.exit(app.exec_())
Setting the border-radius property in the stylesheet of the TranslucentWidget class.
Setting the border-radius property for both the window and the widget
Sudharsan R is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.