Antialiasing( add smoothness) when masking a window for round-corner in PyQt5

Viewed 588

I am making an window where i want to have round corners,I know it can be very easy by setting

        self.mainframe=QFrame(self)
        self.mainframe.setStyleSheet("background:blue;border-radius:25px")
        self.setCentralWidget(self.mainframe)
        self.setWindowFlags(Qt.FramelessWindowHint)
        self.setAttribute(Qt.WA_TranslucentBackground)

This is the result

It has nicely rounded, I mean corners are smooth(Antialiased).
But I dont want to go with this for a reason.I have a second option that i set a mask using QPainterPath in resizeEvent.

Here is the code

def resizeEvent(self, event):
        path = QPainterPath()
        print(self.rect())
        path.addRoundedRect(QRectF(self.rect()),25, 25,Qt.AbsoluteSize)
        reg = QRegion(path.toFillPolygon().toPolygon())
        self.setMask(reg)  

enter image description here

But its corners are not smooth means Antialiased.

Here is my complete code:

import sys
from PyQt5.QtCore import Qt,  QRectF
from PyQt5.QtGui import QPainterPath, QRegion
from PyQt5.QtWidgets import QApplication, QMainWindow, QFrame

class Example(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.resize(600,400)
        self.mainframe=QFrame(self)
        self.mainframe.setStyleSheet("background:blue;border-radius:25px")
        self.setCentralWidget(self.mainframe)
        # self.setWindowFlags(Qt.FramelessWindowHint)
        # self.setAttribute(Qt.WA_TranslucentBackground)
    def resizeEvent(self, event):
        path = QPainterPath()
        print(self.rect())
        path.addRoundedRect(QRectF(self.rect()),25, 25,Qt.AbsoluteSize)
        reg = QRegion(path.toFillPolygon().toPolygon())
        self.setMask(reg)     

   
if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Example()
    window.show()
    sys.exit(app.exec_())    

Is There any way to achieve that.I mean to look like 1st one?

0 Answers
Related