阴影效果在QMainWindow中不起作用

时间:2019-02-23 18:48:44

标签: python pyqt pyqt5

我有以下代码,其中我尝试在QMainWindow后面显示阴影效果,为此我使用了QGraphicsDropShadowEffect 才能得到阴影。

但是运行代码时不会挂起任何东西。

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

enter image description here

代码。 y

from PyQt5.QtWidgets import QMainWindow,QApplication,QGraphicsDropShadowEffect
from PyQt5 import QtCore,QtGui
from PyQt5 import uic


class CInicial(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        uic.loadUi("ConfiguracionInicial.ui",self)

        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
        self.setAttribute(QtCore.Qt.WA_TranslucentBackground)

        self.shadow = QGraphicsDropShadowEffect(self)
        self.shadow.setBlurRadius(99)
        self.shadow.setColor(QtGui.QColor(99,255,255))
        self.shadow.setOffset(4)
        self.setGraphicsEffect(self.shadow)



app = QApplication([])
ci = CInicial()
ci.show()
app.exec_()

。 Ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>683</width>
    <height>482</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <property name="styleSheet">
   <string notr="true">background-color:#5E5858;</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QFrame" name="fContenedor1">
    <property name="geometry">
     <rect>
      <x>0</x>
      <y>0</y>
      <width>691</width>
      <height>41</height>
     </rect>
    </property>
    <property name="styleSheet">
     <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>
    </property>
    <property name="frameShape">
     <enum>QFrame::StyledPanel</enum>
    </property>
    <property name="frameShadow">
     <enum>QFrame::Raised</enum>
    </property>
    <widget class="QLabel" name="label">
     <property name="geometry">
      <rect>
       <x>5</x>
       <y>9</y>
       <width>151</width>
       <height>21</height>
      </rect>
     </property>
     <property name="font">
      <font>
       <pointsize>10</pointsize>
       <weight>75</weight>
       <bold>true</bold>
      </font>
     </property>
     <property name="styleSheet">
      <string notr="true">color:white;
background:none;</string>
     </property>
     <property name="text">
      <string>Configuración Inicial</string>
     </property>
    </widget>
   </widget>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

1 个答案:

答案 0 :(得分:2)

应用阴影效果时,它会在父窗口小部件中绘制,因此对于您的CInicial而言将无效。一种解决方案是创建父窗口小部件并通过布局设置Initialize:

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_())

enter image description here