Search code examples
pyqtpyside6

How to make round edges for the main window in PyQt?


The question is extremely simple, but I am not succeeding at it at the moment:

What is the simplest way to make round edges to the main window in PyQt/PySide code? I have tried lots of things that can be found online but none works. Here is th simplest example (that is supposed to work in my opinion, but doesn't):

from PySide6.QtWidgets import QApplication, QMainWindow

import sys

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setStyleSheet("border-radius: 10px;")
        self.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()

    sys.exit(app.exec())

Solution

  • It is not possible to apply the border-radius property of QSS (Qt style Sheets) to QMainWindow.

    [evidence1]Qt Style Sheet Reference|Qt Widgets 6.4.2

    See the border-radius in List of Properties chapter. border-radius property doesn't support QtMainWindow.

    https://doc.qt.io/qt-6/stylesheet-reference.html#brush

    This property is supported by QAbstractItemView subclasses, QAbstractSpinBox subclasses, QCheckBox, QComboBox, QFrame, QGroupBox, QLabel, QLineEdit, QMenu, QMenuBar, QPushButton, QRadioButton, QSplitter, QTextEdit, and QToolTip.

    [evidence2]Qt Forum|How is it possible to make rounded edges for QMainWindow?

    For reference, Qt Forum also posted that border-radius cannot be applied to QSS in the top level window (QMainWindow). https://forum.qt.io/topic/127952/how-is-it-possible-to-make-rounded-edges-for-qmainwindow/2

    [Alternative proposal]

    If QWidget doesn't lack the functionality you need, you can use it instead of QMainWindow. The following code creates a round QWidget.

    from PySide6.QtWidgets import QApplication, QMainWindow
    from PySide6.QtWidgets import QWidget
    from PySide6.QtCore import Qt
    
    import sys
    
    class MainWindow(QMainWindow):
        def __init__(self):
            super().__init__()
    
            w = 200
            h = 100
    
            #main window
            self.resize(w, h)
            #remove frame
            self.setWindowFlag(Qt.FramelessWindowHint)
            #make the main window transparent
            self.setAttribute(Qt.WA_TranslucentBackground)
    
            #round widget
            self.round_widget = QWidget(self)
            self.round_widget.resize(w, h)
    
            self.round_widget.setStyleSheet(
                """
                background:rgb(255, 255, 255);
                border-radius: 50px;
                """
            )
    
            #self.setStyleSheet("border-radius: 10px;".format(radius))
            self.show()
    
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        window = MainWindow()
    
        sys.exit(app.exec())
    

    The code creates following window.

    enter image description here