Home > Enterprise >  (PyQt5) How can I apply blur effect only on the background of my mainwindow?
(PyQt5) How can I apply blur effect only on the background of my mainwindow?

Time:01-24

this is what i got

CodePudding user response:

BlurWindow uses system features to set the background of a top level window.

Your problem is that you're applying it to the wrong widget, which is a child widget, not a top level one. The top level has no "glass effect" set, so the result is that it won't have any effect applied on it.

The solution is simple: apply the effect to the top level window.

import sys
from PyQt5.QtWidgets import *
from PyQt5.uic import loadUi
from BlurWindow.blurWindow import blur

class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        loadUi(r'D:\Workspace\Qt Designer\blur bg\blurtest.ui', self)
        self.setStyleSheet("""
            MainWindow {
                background-color: rgba(0, 0, 0, 0);
            }
        """)


app = QApplication(sys.argv)
mainwindow = MainWindow()
widget = QtWidgets.QStackedWidget()
widget.addWidget(mainwindow)
widget.setFixedHeight(600)
widget.setFixedWidth(800)

blur(widget.winId()) # <---
widget.show()

sys.exit(app.exec_())

Note that:

  • QMainWindow is not supposed to be used as a child widget. You should switch to a basic QWidget (or other container widgets like QFrame), meaning that you should create a new "widget" in Designer and copy/paste the content of your previous window to it, otherwise loadUi() will throw an exception;
  • you should never apply generic style sheet properties to parent widgets, as you would get unexpected results (especially with complex widgets, like scroll areas); you should always use proper selectors (as I did above);
  • Related