阴影效果在 QMainWindow 中不起作用

Ang*_*rez 1 python pyqt pyqt5

我有以下代码,其中我尝试在 QMainWindow 后面显示阴影效果,为此我使用QGraphicsDropShadowEffect\n 来获取阴影。

\n\n

但是运行代码时不会暂停任何内容。

\n\n

我已经尝试修改颜色和偏移量,但它也不起作用

\n\n

在此输入图像描述

\n\n

代码。吡啶

\n\n
from PyQt5.QtWidgets import QMainWindow,QApplication,QGraphicsDropShadowEffect\nfrom PyQt5 import QtCore,QtGui\nfrom PyQt5 import uic\n\n\nclass CInicial(QMainWindow):\n    def __init__(self):\n        QMainWindow.__init__(self)\n        uic.loadUi("ConfiguracionInicial.ui",self)\n\n        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)\n        self.setAttribute(QtCore.Qt.WA_TranslucentBackground)\n\n        self.shadow = QGraphicsDropShadowEffect(self)\n        self.shadow.setBlurRadius(99)\n        self.shadow.setColor(QtGui.QColor(99,255,255))\n        self.shadow.setOffset(4)\n        self.setGraphicsEffect(self.shadow)\n\n\n\napp = QApplication([])\nci = CInicial()\nci.show()\napp.exec_()\n
Run Code Online (Sandbox Code Playgroud)\n\n

。用户界面

\n\n
<?xml version="1.0" encoding="UTF-8"?>\n<ui version="4.0">\n <class>MainWindow</class>\n <widget class="QMainWindow" name="MainWindow">\n  <property name="geometry">\n   <rect>\n    <x>0</x>\n    <y>0</y>\n    <width>683</width>\n    <height>482</height>\n   </rect>\n  </property>\n  <property name="windowTitle">\n   <string>MainWindow</string>\n  </property>\n  <property name="styleSheet">\n   <string notr="true">background-color:#5E5858;</string>\n  </property>\n  <widget class="QWidget" name="centralwidget">\n   <widget class="QFrame" name="fContenedor1">\n    <property name="geometry">\n     <rect>\n      <x>0</x>\n      <y>0</y>\n      <width>691</width>\n      <height>41</height>\n     </rect>\n    </property>\n    <property name="styleSheet">\n     <string notr="true">background:qlineargradient(spread:pad, x1:0.498, y1:1, x2:0.472, y2:0, stop:0 rgba(46, 46, 48, 255), stop:1 rgba(137, 137, 137, 255));</string>\n    </property>\n    <property name="frameShape">\n     <enum>QFrame::StyledPanel</enum>\n    </property>\n    <property name="frameShadow">\n     <enum>QFrame::Raised</enum>\n    </property>\n    <widget class="QLabel" name="label">\n     <property name="geometry">\n      <rect>\n       <x>5</x>\n       <y>9</y>\n       <width>151</width>\n       <height>21</height>\n      </rect>\n     </property>\n     <property name="font">\n      <font>\n       <pointsize>10</pointsize>\n       <weight>75</weight>\n       <bold>true</bold>\n      </font>\n     </property>\n     <property name="styleSheet">\n      <string notr="true">color:white;\nbackground:none;</string>\n     </property>\n     <property name="text">\n      <string>Configuraci\xc3\xb3n Inicial</string>\n     </property>\n    </widget>\n   </widget>\n  </widget>\n </widget>\n <resources/>\n <connections/>\n</ui>\n
Run Code Online (Sandbox Code Playgroud)\n

eyl*_*esc 5

当应用阴影效果时,它会在父窗口小部件中绘制,因此对于您的 CInicial 来说,它不会产生任何效果。一种解决方案是创建一个父窗口小部件并通过布局设置初始化:

from PyQt5 import QtCore, QtGui, QtWidgets, uic

class Container(QtWidgets.QWidget):
    def __init__(self, window, parent=None):
        super(Container, self).__init__(parent)
        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
        self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
        lay = QtWidgets.QVBoxLayout(self)
        lay.addWidget(window)
        lay.setContentsMargins(10, 10, 10, 10)
        shadow = QtWidgets.QGraphicsDropShadowEffect(self,
            blurRadius=9.0,
            color=QtGui.QColor(99, 255, 255),
            offset=QtCore.QPointF(8.0, 8.0)
        )
        window.setGraphicsEffect(shadow)

class CInicial(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(CInicial, self).__init__(parent)
        uic.loadUi("ConfiguracionInicial.ui",self)

if __name__ == '__main__':
    import sys 
    app = QtWidgets.QApplication(sys.argv)
    w = CInicial()
    container = Container(w)
    container.resize(640, 480)
    container.show()
    sys.exit(app.exec_())
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述