阴影效果在QMainWindow中不起作用

问题描述 投票:1回答:1

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

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

我已经尝试修改颜色和偏移但它也没有用

码。 PY

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

。洋葱

<?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>
python pyqt pyqt5
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.