我正在制作一个窗口,我希望有圆角,我知道通过设置可以很容易地实现。
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)
它有很好的圆角,我的意思是角是光滑的(反碱)。
但我不想这样做,因为有一个原因。我有第二种选择,我用掩码来设置绘画者路径 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)
但它的角落是不光滑的。有偏见.
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()