我有一个 QWidget,我已将其设置为具有透明背景,并通过 paintEvent() 绘制了背景。
我已经向这个添加了子小部件,并且在渲染时似乎自定义paintEvent()无法绘制完整的背景,或者子小部件以某种方式覆盖它并阻止成功渲染。
通过 uic.loadUi('path.ui', self) 添加子小部件
示例代码:
def __init__(self):
super().__init__()
# QGridLayout inside with 12 cells as shown on screenshot
uic.loadUi(r'path.ui', self)
self.setAttribute(QtCore.Qt.WidgetAttribute.WA_TranslucentBackground, True)
self.setAttribute(QtCore.Qt.WidgetAttribute.WA_AlwaysStackOnTop, True)
self.setWindowFlags(QtCore.Qt.WindowFlags.Window)
self.setWindowOpacity(1.0)
self.setWindowFlags(QtCore.Qt.WindowFlags.FramelessWindowHint)
self.setBorderMargin(5)
self.setCornerMargin(20)
self.resize(600, 400)
self.layout().setSpacing(20)
self.layout().setVerticalSpacing(20)
self.layout().setContentsMargins(15, 15, 15, 15)
def paintEvent(self, event):
painter = QPainter(self)
painter.setRenderHint(QPainter.RenderHints.Antialiasing)
pen = QPen(QColor(8553090), 1)
painter.setPen(pen)
brush = QBrush(QColor(14079702))
painter.setBrush(brush)
path = QPainterPath()
rect = event.rect()
rect.adjust(2, 2, -2, -2)
#path.addRoundedRect(QRectF(rect), 35, 35)
path.addRoundedRect(QRectF(rect), 35, 35)
painter.fillPath(path, painter.brush())
painter.strokePath(path, painter.pen())
super().paintEvent(event)
def keyPressEvent(self, event):
if event.keyCombination().key() == QtCore.Qt.Key.Key_Escape:
self.close()