Unable to set "QMainWindow" shadow with "FramelessWindowHint" window type
-
Hello,
I am unable to apply a shadow effect to my customQMainWindow
which hasFramelessWindowHint
flag, what ever I do, its never there.
Does anyone know how to bring back the window shadow effect in windows?I am using:
Windows 11
Python 3.11.3
PyQt6==6.5.1Here is a reproduceable snippet:
import sys from PyQt6.QtWidgets import ( QGraphicsDropShadowEffect, QApplication, QMainWindow, QPushButton, QWidget, QVBoxLayout, ) from PyQt6.QtGui import QColor from PyQt6.QtCore import Qt, QCoreApplication class MainWindow(QMainWindow): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.setWindowTitle('Test') self.setWindowFlag(Qt.WindowType.FramelessWindowHint) self.setAttribute(Qt.WidgetAttribute.WA_TranslucentBackground) # Shadow? shadow = QGraphicsDropShadowEffect(self) shadow.setBlurRadius(20) shadow.setColor(QColor("red")) self.setGraphicsEffect(shadow) # Central Widget central_widget = QWidget(self) central_widget.setFixedSize(300, 300) central_widget.setObjectName('central_widget') central_widget.setStyleSheet(""" QWidget#central_widget{ background-color: lightblue; } """) central_widget.setLayout(QVBoxLayout()) central_widget.layout().setAlignment(Qt.AlignmentFlag.AlignHCenter) # Just for convenience button = QPushButton("Close", central_widget) button.setFixedSize(100, 20) button.clicked.connect(QCoreApplication.instance().quit) central_widget.layout().addWidget(button) self.setCentralWidget(central_widget) self.show() if __name__ == '__main__': app = QApplication(sys.argv) window = MainWindow() sys.exit(app.exec())
-
Hello,
I am unable to apply a shadow effect to my customQMainWindow
which hasFramelessWindowHint
flag, what ever I do, its never there.
Does anyone know how to bring back the window shadow effect in windows?I am using:
Windows 11
Python 3.11.3
PyQt6==6.5.1Here is a reproduceable snippet:
import sys from PyQt6.QtWidgets import ( QGraphicsDropShadowEffect, QApplication, QMainWindow, QPushButton, QWidget, QVBoxLayout, ) from PyQt6.QtGui import QColor from PyQt6.QtCore import Qt, QCoreApplication class MainWindow(QMainWindow): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.setWindowTitle('Test') self.setWindowFlag(Qt.WindowType.FramelessWindowHint) self.setAttribute(Qt.WidgetAttribute.WA_TranslucentBackground) # Shadow? shadow = QGraphicsDropShadowEffect(self) shadow.setBlurRadius(20) shadow.setColor(QColor("red")) self.setGraphicsEffect(shadow) # Central Widget central_widget = QWidget(self) central_widget.setFixedSize(300, 300) central_widget.setObjectName('central_widget') central_widget.setStyleSheet(""" QWidget#central_widget{ background-color: lightblue; } """) central_widget.setLayout(QVBoxLayout()) central_widget.layout().setAlignment(Qt.AlignmentFlag.AlignHCenter) # Just for convenience button = QPushButton("Close", central_widget) button.setFixedSize(100, 20) button.clicked.connect(QCoreApplication.instance().quit) central_widget.layout().addWidget(button) self.setCentralWidget(central_widget) self.show() if __name__ == '__main__': app = QApplication(sys.argv) window = MainWindow() sys.exit(app.exec())
-
@mpergand
thanks for the reply, so if I understand correctly I should make the window a bit bigger thant the central_widget and then the shadow should affect the central_widget ?that seems to do the trick, adding a
self.setContentsMargins(20, 20, 20, 20)
on the QMainWindow, adds margin inside making it bigger, and then the shadow appear on the central widget, altho need to check if it doesnt break anything else :Dedit1: it offset all the geometry by the margin number - a bit messy but need to take it into consideration for the moving part
-
that seems to do the trick, adding a
self.setContentsMargins(20, 20, 20, 20)
on the QMainWindow, adds margin inside making it bigger, and then the shadow appear on the central widget, altho need to check if it doesnt break anything else :Dedit1: it offset all the geometry by the margin number - a bit messy but need to take it into consideration for the moving part
-
Too many undesirable effects, for example, the border are not click throu so its not convenient, I guess I ll have to live without a border, too bad its not possible.
@bashtian
Have a try with:Qt::WA_TransparentForMouseEvents
When enabled, this attribute disables the delivery of mouse events to the widget and its children. Mouse events are delivered to other widgets as if the widget and its children were not present in the widget hierarchy; mouse clicks and other events effectively "pass through" them. This attribute is disabled by default. -
@bashtian
Have a try with:Qt::WA_TransparentForMouseEvents
When enabled, this attribute disables the delivery of mouse events to the widget and its children. Mouse events are delivered to other widgets as if the widget and its children were not present in the widget hierarchy; mouse clicks and other events effectively "pass through" them. This attribute is disabled by default.