Issue
I have the following code in which I try to show a shadow effect behind the QMainWindow, for which I use QGraphicsDropShadowEffect
to be able to get the shadow.
But when running the code does not suspend anything.
I already tried modifying the color and the offset but it did not work either
Code. 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_()
. 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>
Solution
When the shadow effect is applied it is painted in the parent widget, so in the case of your CInicial it will have no effect. One solution is to create a parent widget and set the Initialize through a layout:
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_())
Answered By - eyllanesc
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.